Tree

Tree

Represents the tree in which data nodes can be inserted

Constructor

new Tree()

Source:

Methods

compress(criteria)

Returns a new compressed tree. While compressing it considers nodes that satisfies given criteria and skips the rest of the nodes, making tree compressed.
Parameters:
Name Type Description
criteria Tree~criteria Callback function that checks whether node satifies certain criteria. MUST return boolean.
Source:
Returns:
Tree - A new compressed tree.

currentNode() → {TreeNode}

Returns a current node in a tree
Source:
Returns:
- current node of the tree.
Type
TreeNode

distanceBetween({@link, {@link) → {Number}

Finds a distance between two nodes
Parameters:
Name Type Description
{@link TreeNode} fromNode - Node from which distance is to be calculated
{@link TreeNode} toNode - Node to which distance is to be calculated
Source:
Returns:
- distance(number of hops) between two nodes.
Type
Number

export(criteria) → {object}

Exports the tree data in format specified. It maintains herirachy by adding additional "children" property to returned value of `criteria` callback.
Parameters:
Name Type Description
criteria Tree~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:
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 = tree.export(function(data){
 return { name: data.value.name };
});

// Result in `exported`
{
 "name": "Apple",
 "children": [
   {
     "name": "Green Apple",
     "children": []
   },
   {
     "name": "Some Apple",
     "children": []
  }
 ]
}

findCommonParent({@link, {@link)

Finds a common parent between nodes
Parameters:
Name Type Description
{@link TreeNode} fromNode
{@link TreeNode} toNode
Source:
Returns:
TreeNode - common parent

import(data, childProperty, criteria) → {object}

Imports the JSON data into a tree using the criteria provided. A property indicating the nesting of object must be specified.
Parameters:
Name Type Description
data object JSON data that has be imported
childProperty string Name of the property that holds the nested data.
criteria Tree~criteria Callback function that receives data in parameter and MUST return a formatted data that has to be imported in a tree.
Source:
Returns:
- Tree.
Type
object
Example
var data = {
  "trailId": "h2e67d4ea-f85f40e2ae4a06f4777864de",
  "initiatedAt": 1448393492488,
  "snapshots": {
     "snapshotId": "b3d132131-213c20f156339ea7bdcb6273",
     "capturedAt": 1448393495353,
     "thumbnail": "data:img",
     "children": [
      {
       "snapshotId": "yeb7ab27c-b36ff1b04aefafa9661243de",
       "capturedAt": 1448393499685,
       "thumbnail": "data:image/",
       "children": [
         {
           "snapshotId": "a00c9828f-e2be0fc4732f56471e77947a",
           "capturedAt": 1448393503061,
           "thumbnail": "data:image/png;base64",
           "children": []
         }
       ]
     }
    ]
  }
};

 // Import
 // This will result in a tree having nodes containing `id` and `thumbnail` as data
 tree.import(data, 'children', function(nodeData){
   return {
     id: nodeData.snapshotId,
     thumbnail: nodeData.thumbnail
    }
 });

insert(data) → {object}

Creates a TreeNode that contains the data provided and insert it in a tree. New node gets inserted to the `_currentNode` which updates itself upon every insertion and deletion.
Parameters:
Name Type Description
data object data that has to be stored in tree-node.
Source:
Returns:
- instance of TreeNode that represents node inserted.
Type
object
Example
// Insert single value
tree.insert(183);

// Insert array of values
tree.insert([34, 565, 78]);

// Insert complex data
tree.insert({
  key: '#berries',
  value: { name: 'Apple', color: 'Red'}
});

insertTo(criteria, data) → {object}

Inserts node to a particular node present in the tree. Particular node here is searched in the tree based on the criteria provided.
Parameters:
Name Type Description
criteria function Callback function that specifies the search criteria for node to which new node is to be inserted. Criteria callback here receives TreeNode#_data in parameter and MUST return boolean indicating whether that data satisfies your criteria.
data object that has to be stored in tree-node.
Source:
Returns:
- instance of TreeNode that represents node inserted.
Type
object
Example
// Insert data
tree.insert({
  key: '#apple',
  value: { name: 'Apple', color: 'Red'}
});

// New Data
var greenApple = {
 key: '#greenapple',
 value: { name: 'Green Apple', color: 'Green' }
};

// Insert data to node which has `key` = #apple
tree.insertTo(function(data){
 return data.key === '#apple'
}, greenApple);

insertToNode(node, data) → {object}

Inserts node to a particular node present in the tree. Particular node here is an instance of TreeNode
Parameters:
Name Type Description
node function TreeNode to which data node is to be inserted.
data object that has to be stored in tree-node.
Source:
Returns:
- instance of TreeNode that represents node inserted.
Type
object
Example
// Insert data
var node = tree.insert({
  key: '#apple',
  value: { name: 'Apple', color: 'Red'}
});

// New Data
var greenApple = {
 key: '#greenapple',
 value: { name: 'Green Apple', color: 'Green' }
};

// Insert data to node
tree.insertToNode(node, greenApple);

isEmpty() → {boolean}

Checks whether tree is empty.
Source:
Returns:
whether tree is empty.
Type
boolean

pruneAllNodes()

Empties the tree. Removes all nodes from tree.
Source:
Returns:
Tree empty tree.

remove(node, trim)

Removes a node from tree and updates `_currentNode` to parent node of node removed.
Parameters:
Name Type Description
node object TreeNode that has to be removed.
trim boolean indicates whether to remove entire branch from the specified node.
Source:

rootNode() → {TreeNode}

Returns a root node of the tree.
Source:
Returns:
- root node of the tree.
Type
TreeNode

traverser()

Getter function that returns Traverser.
Source:
Returns:
Traverser for the tree.

trimBranchFrom(node)

Remove an entire branch starting with specified node.
Parameters:
Name Type Description
node object TreeNode from which entire branch has to be removed.
Source: