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 |
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
Example:
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
Example:
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
Example:
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
Example:
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:
Name | Type | Attribute | Description |
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 |
|
injection offset position |
Example:
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
Example:
// 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 })
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
Example:
walkTreeBFS({ tree, visitor (node) { console.log('visiting node', node.id) } })