- BTree
- toString
- toJSON
- toArray
- toFlatArray
- insert
- insertLeftMost
- insertRightMost
- delete
- insertAt
- traverseBFS
- traverseDFS
- each
- forEach
- entries
- map
- filter
- reverse
- indexOf
- includes
- exists
- has
- sort
- find
- getIndexFromPath
- getPathFromIndex
- fromArray
- BTreeNode
BTree main class
attr(BTreeRootAttrStruct | BTreeValueAttrStruct | T) Can be of type object, string, number. In case of object root/value property is expected to be value of root node.
new BTree(10);
new BTree({ root: 10 });
new BTree({ value: 10 });Depth of the binary tree.
Type: number
Returns an iterable of key, value pairs for every entry in the tree.
var tree = new BTree(10);
for (const node of tree) {
console.log(node.value); // 10
}Reduces each node values using reduceFunction and returns final value.
reduceFunctioninitialValueT2 Optional, Accumulator/Initial value. (optional, default0)
var tree = BTree.fromArray([10, 20, 30, 40]);
var sum = tree.reduce((acc, node) => acc + node); // => 100Returns T2 Returns reduceed value
Returns string value of given tree.
var tree = new BTree(10);
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.toString(); // "10102030"Returns JSON Form.
var tree = new BTree(10);
tree.insert(20);
tree.toJSON(); // {value:10,lNode:{value:20,lNode:null,rNode:null},rNode:null}Returns BTreeNodeStruct Returns json form of a given tree.
Returns array value.
var tree = new BTree(10);
tree.insert(20);
tree.toArray(); // => [{value:10,...},{value:20,...}]Returns Array<BTreeNode> Returns array form of given tree.
Returns array of values of the tree.
var tree = new BTree(10);
tree.insert(20);
tree.toFlatArray(); // => [10,20]Returns Array<T> Returns array form of given tree.
Inserts the given value to the tree where first free left child node is found.
valany any type of value to be added to tree node.
var tree = new BTree(10);
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.toString(); // "10102030"Returns BTreeNode Returns newly created BTreeNode.
Inserts the given value to the tree where first free left child node is found.
valT any type of value to be added to tree node.
Returns BTreeNode<T> Returns newly created BTreeNode.
Inserts the given value to the tree where first free right child node is found.
valT any type of value to be added to tree node.
Returns BTreeNode<T> Returns newly created BTreeNode.
Deletes given value from tree. Travarsal = Root -> L -> R.
valT Value to be removed.
Returns BTreeNode<T> Returns removed BTreeNode.
Inserts given element at given location. If location is already taken then it does not insert any value.
valT value to insert.indexnumber index at which to append new element to.
tree.insertAt(20,2);- Throws any UnreachableError
Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
Returns void no value.
Depth first search, Executes given callback functions with parameters BTreeNode and path index for each node in DFS fashion.
Returns void no value.
Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
Returns void no value.
Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
Returns void no value.
Returns an iterable of key, value pairs for every entry in the tree.
var tree = new BTree(10);
for (const [index, node] of tree.entries()) {
console.log(index, node.value); // 0, 10
}Returns IterableIterator<[number, BTreeNode<T>]> Iterable for iterations.
Maps current tree values to a new tree with modifying the values using given callback function. Uses BFS.
var tree = BTree.fromArray([10, 20, 30, 40]);
var tree2 = tree.map(n => n * 2);
var arr2 = tree2.toArray(); // [{value:20,...},{value:40,...},{value:60,...},{value:80,...}]Returns BTree<T> A new BTree
Filters each item based on given filter function
var tree = BTree.fromArray([10, 20, 30, 40]);
var tree2 = tree.filter(n => !!(n % 4 === 0 || n === 10));
var arr2 = tree2.toArray(); // [{value:10,...},{value:20,...},{value:40,...}]- Throws any FilteredRootError, Error when root node gets filtered out.
Returns BTree<T> New filtered instance of tree.
Reverses the current Binary Tree, Left Node becomes Right node and vise versa. Does not return new instance, returns current tree instance.
var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.reverse().toArray(); // => [10, 30, 20, 70, 60, 50, 40, 80]Returns BTree<T> Returns current tree instance.
Returns first index of a value matched, if it is not present, it returns -1.
valueT Any value to find.
var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.indexOf(30); // => 3
tree.indexOf(51); // => -1Returns number Returns index of given item.
Checks if given item exists or not, returns boolean.
valueT Any value to check if it exists or not.
var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.includes(30); // true
tree.includes(51); // falseReturns boolean Returns true if it is present, otherwise false.
Checks if given item exists or not, returns boolean.
valueT Any value to check if it exists or not.
var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.exists(30); // true
tree.exists(51); // falseReturns boolean Returns true if it is present, otherwise false.
Checks if given item exists or not, returns boolean.
valueT Any value to check if it exists or not.
var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.has(30); // true
tree.has(51); // falseReturns boolean Returns true if it is present, otherwise false.
Sorts the tree based on compare function, Has option to sort only at children level.
compareFncFunction Function used to determine the order of the elements. It is expected to return a negative value if first argument is less than second argument, zero if they're equal and a positive value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.```ts (a, b) => a - b)atOnlyFirstChildLevelboolean Optiona, Flag to specify if first child of each node should sorted. Default isfalse.
var tree = BTree.fromArray([10, 200, 100, 50, 60, 90, 5, 3]);
tree.sort().toFlatArray(); // => [3,5,10,50,60,90,100,200]Returns void Returns undefined.
Prints entire tree on the console, useful for logging and checking status.
var tree = BTree.fromArray([1, 2, 3]);
tree.print();
1 (1)
|- 2 (2)
|- 3 (3)Returns void Returns undefined.
Returns the first matched tree node. Traverses using BFS.
itemT any value to find inside the tree.
Returns (BTreeNode<T> | null) Returns the first matched tree node, if not found, returns null.
Returns index value from given path.
pathArray<("U"|"L"|"R")> Array for U L or R, which represents the quickest path to get to a node.
Returns number Returns index value.
Returns Path equivalent to the given index.
indexnumber Index number from which path to be calculated.
Returns Array<("U" | "L" | "R")> Path equivalent to the given index.
Converts given values into a Binary Tree.
arrArray<T2> Any array of values.
var tree = BTree.fromArray([10,20,30,40]);Returns BTree<T2> Newly generated tree.
Binary Tree node class, contains 2 child nodes and single value.
var node = new BTreeNode({ value: 15 });
var node3 = new BTreeNode({ value: 30 });
var node2 = new BTreeNode({ value: 20, rNode: node, lNode: node3 });
console.log(node2.lNode.value); // 30Validates a BTree node, it must have a valid value (no undefined nor null).
new BTreeNode(); // throws error saying `A BTree node must have a valid value, cannot be null or undefined`
var node = new BTreeNode({ value: 10 });
console.log(node.validate()); // trueReturns boolean
Converts current node and all children nodes in json format.
var node = new BTreeNode({ value: 10 });
var lNode = new BTreeNode({ value: 15, lNode: lNode });
console.log(node.toJSON()); // {value:15,lNode: {value: 10,lNode:null,rNode:null},rNode:null}Returns BTreeNodeStruct
Converts current node and all children nodes in json format and append them together. Goes in direction of U -> L -> R
var node = new BTreeNode({ value: 10 });
var lNode = new BTreeNode({ value: 15, lNode: node });
console.log(node.toString()); // "1015"Returns string
Returns depth of its children. Goes in direction of L -> R
var node = new BTreeNode({ value: 10 });
var lNode = new BTreeNode({ value: 15, lNode: node });
var l2Node = new BTreeNode({ value: 15, lNode: lNode });
console.log(l2Node.getDepth()); // 3Returns number