Home Reference Source Repository

Function

Static Public Summary
public

createTree(config: {id: string}): {id: string, children: Array}

creates a new tree root plain data object suitable for use in state

version 1.0.0
public

createTreeNode(config: {id: string, data: *}): {id: string, children: Array}

creates a new tree node plain data object suitable for use in state

version 1.0.0
public

deleteTreeNode(config: {tree: object, id: string}})

deletes a tree node specified by id from a tree all children of the deleted node will be deleted as well

version 1.0.0
public

findTreeNode(config: {tree: object, id: string}): {id: string, children: Array}

searches a tree for a tree node specified by id

version 1.0.0
public

insertTreeNode(config: {parent: object, node: object, offset: number})

inserts a tree node into a tree as a child of the specified parent using the specified offset

version 1.0.0
public

printTree(config: {tree: object, showData: boolean, showChildCount: boolean, linear: boolean}})

prints the tree to the console

version 1.0.0
public

walkTreeBFS(config: function(node: {id: string, children: Array}))

walk through the tree using a breadth-first search (BFS) and call visitor function for each tree node

version 1.0.0
public

walkTreeDFS(config: function(node: {id: string, children: Array}))

walk through the tree using a depth-first search (DFS) and call visitor function for each tree node

version 1.0.0

Static Public

public createTree(config: {id: string}): {id: string, children: Array} version 1.0.0 source

creates a new tree root plain data object suitable for use in state

Params:

NameTypeAttributeDescription
config {id: string}

destructured initialization object

config.id string
  • optional
  • default: 'tree'

id to give the tree, should be unique

Return:

{id: string, children: Array}

tree root plain data object

Return Properties:

NameTypeAttributeDescription
id string

node identifier

data *
  • nullable: true

node data

children Array<{id: string, children: Array}>

list of child tree node plain data objects

Example:

How to use

const tree = createTree({ id: 'my-tree' })

public createTreeNode(config: {id: string, data: *}): {id: string, children: Array} version 1.0.0 source

creates a new tree node plain data object suitable for use in state

Params:

NameTypeAttributeDescription
config {id: string, data: *}

destructured initialization object

config.id string
  • optional
  • default: 'tree'

id to give the tree, should be unique

config.data *
  • optional
  • default: undefined

any data to associate with the tree node

Return:

{id: string, children: Array}

tree node plain data object

Return Properties:

NameTypeAttributeDescription
id string

node identifier

data *
  • nullable: true

node data

children Array<{id: string, children: Array}>

list of child tree node plain data objects

Example:

How to use

const node = createTreeNode({ id: 'my-tree-node', data: { name: 'My Tree Node' } })

public deleteTreeNode(config: {tree: object, id: string}}) version 1.0.0 source

deletes a tree node specified by id from a tree all children of the deleted node will be deleted as well

Params:

NameTypeAttributeDescription
config {tree: object, id: string}}

destructured initialization object

config.tree {id: string, children: Array}

the tree root to search for the node to be deleted

config.id string

id of the node to delete

Example:

How to use

deleteTreeNode({ tree, id: 'target-tree-node' })

public findTreeNode(config: {tree: object, id: string}): {id: string, children: Array} version 1.0.0 source

searches a tree for a tree node specified by id

Params:

NameTypeAttributeDescription
config {tree: object, id: string}

destructured initialization object

config.tree {id: string, children: Array}

the tree root to search

config.id string

id of the node to find

Return:

{id: string, children: Array} (nullable: true)

found node or null if not found

Example:

How to use

const node = findTreeNode({ tree, id: 'target-tree-node' })

public insertTreeNode(config: {parent: object, node: object, offset: number}) version 1.0.0 source

inserts a tree node into a tree as a child of the specified parent using the specified offset

the offset allows you to specify the position in the children list where the new node will be inserted

  • a negative offset will insert the new node at the beginning of the children list
  • a positive offset will insert the new node at the end of the children list
  • an offset of zero is treated as a positive offset

Params:

NameTypeAttributeDescription
config {parent: object, node: object, offset: number}

destructured initialization object

config.parent {id: string, children: Array}

the tree node which serves as the parent of the new node

config.node {id: string, children: Array}

the new node to insert

config.offset number
  • optional
  • default: 0

injection offset position

Example:

How to use

insertTreeNode({ parent, node, offset })

public printTree(config: {tree: object, showData: boolean, showChildCount: boolean, linear: boolean}}) version 1.0.0 source

prints the tree to the console

Params:

NameTypeAttributeDescription
config {tree: object, showData: boolean, showChildCount: boolean, linear: boolean}}

destructured initialization object

config.tree {id: string, children: Array}

the tree root to start printing

Example:

How to use

// print the tree in a linear fashion where each depth is on a new line
printTree({ tree, linear: true })

// print the tree in an expanded hierarchial display showing branches of the tree
printTree({ tree, linear: false })

// print the tree in an expanded hierarchial display showing branches of the tree
// and display the number of children each node contains
printTree({ tree, showChildCount: true })

// print the tree in an expanded hierarchial display showing branches of the tree
// and display the data of each node using JSON.stringify
printTree({ tree, showData: true })

See:

public walkTreeBFS(config: function(node: {id: string, children: Array})) version 1.0.0 source

walk through the tree using a breadth-first search (BFS) and call visitor function for each tree node

Params:

NameTypeAttributeDescription
config function(node: {id: string, children: Array})

destructured initialization object

config.tree {id: string, children: Array}

the tree to walk

config.visitor function(node: {id: string, children: Array})

function to call for each tree node in the tree

Example:

How to use

walkTreeBFS({ tree, visitor (node) { console.log('visiting node', node.id) } })

See:

public walkTreeDFS(config: function(node: {id: string, children: Array})) version 1.0.0 source

walk through the tree using a depth-first search (DFS) and call visitor function for each tree node

Params:

NameTypeAttributeDescription
config function(node: {id: string, children: Array})

destructured initialization object

config.tree {id: string, children: Array}

the tree to walk

config.visitor function(node: {id: string, children: Array})

function to call for each tree node in the tree

Example:

How to use

walkTreeDFS({ tree, visitor (node) { console.log('visiting node', node.id) } })

See: