Constructor
new TreeNode(data)
Parameters:
Name | Type | Description |
---|---|---|
data |
object | that is to be stored in a node |
- Source:
Methods
childNodes() → {array}
Returns an array of child nodes
- Source:
Returns:
- array of child nodes
- Type
- array
data(data) → {object|array|string|number|null}
Sets or gets the data belonging to this node. Data is what user sets using `insert` and `insertTo` methods.
Parameters:
Name | Type | Description |
---|---|---|
data |
object | array | string | number | null | data which is to be stored |
- Source:
Returns:
- data belonging to this node
- Type
- object | array | string | number | null
depth() → {number}
Depth of the node. Indicates the level at which node lies in a tree.
- Source:
Returns:
- depth of node
- Type
- number
distanceToRoot() → {array}
Finds distance of node from root node
- Source:
Returns:
- array of instances of TreeNode
- Type
- array
export(criteria) → {object}
Exports the node data in format specified. It maintains herirachy by adding
additional "children" property to returned value of `criteria` callback.
Parameters:
Name | Type | Description |
---|---|---|
criteria |
TreeNode~criteria | Callback function that receives data in parameter and MUST return a formatted data that has to be exported. A new property "children" is added to object returned that maintains the heirarchy of nodes. |
- Source:
Returns:
- TreeNode.
- Type
- object
Example
var rootNode = tree.insert({
key: '#apple',
value: { name: 'Apple', color: 'Red'}
});
tree.insert({
key: '#greenapple',
value: { name: 'Green Apple', color: 'Green'}
});
tree.insertToNode(rootNode, {
key: '#someanotherapple',
value: { name: 'Some Apple', color: 'Some Color' }
});
// Export the tree
var exported = rootNode.export(function(data){
return { name: data.value.name };
});
// Result in `exported`
{
"name": "Apple",
"children": [
{
"name": "Green Apple",
"children": []
},
{
"name": "Some Apple",
"children": []
}
]
}
getAncestry() → {Array}
Gets an array of all ancestor nodes including current node
- Source:
Returns:
- array of ancestor nodes
- Type
- Array
matchCriteria(callback)
Indicates whether this node matches the specified criteria. It triggers a callback criteria function that returns something.
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | Callback function that specifies some criteria. It receives TreeNode#_data in parameter and expects different values in different scenarios. `matchCriteria` is used by following functions and expects: 1. Tree#searchBFS - {boolean} in return indicating whether given node satisfies criteria. 2. Tree#searchDFS - {boolean} in return indicating whether given node satisfies criteria. 3. Tree#export - {object} in return indicating formatted data object. |
- Source:
parentNode() → {TreeNode}
Returns a parent node of current node
- Source:
Returns:
- parent of current node
- Type
- TreeNode
siblings() → {array}
get sibling nodes.
- Source:
Returns:
- array of instances of TreeNode
- Type
- array