2.0.0
BTree main class
((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 });
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.
(any)
(T2
= 0
)
Optional, Accumulator/Initial value.
T2
:
Returns reduceed value
var tree = BTree.fromArray([10, 20, 30, 40]);
var sum = tree.reduce((acc, node) => acc + node); // => 100
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.
BTreeNodeStruct
:
Returns json form of a given tree.
var tree = new BTree(10);
tree.insert(20);
tree.toJSON(); // {value:10,lNode:{value:20,lNode:null,rNode:null},rNode:null}
Returns array value.
Array<BTreeNode>
:
Returns array form of given tree.
var tree = new BTree(10);
tree.insert(20);
tree.toArray(); // => [{value:10,...},{value:20,...}]
Returns array of values of the tree.
Array<T>
:
Returns array form of given tree.
var tree = new BTree(10);
tree.insert(20);
tree.toFlatArray(); // => [10,20]
Inserts the given value to the tree where first free left child node is found.
(any)
any type of value to be added to tree node.
BTreeNode
:
Returns newly created BTreeNode.
var tree = new BTree(10);
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.toString(); // "10102030"
Inserts the given value to the tree where first free left child node is found.
(T)
any type of value to be added to tree node.
BTreeNode<T>
:
Returns newly created BTreeNode.
Inserts the given value to the tree where first free right child node is found.
(T)
any type of value to be added to tree node.
BTreeNode<T>
:
Returns newly created BTreeNode.
Deletes given value from tree. Travarsal = Root -> L -> R.
(T)
Value to be removed.
BTreeNode<T>
:
Returns removed BTreeNode.
Inserts given element at given location. If location is already taken then it does not insert any value.
(T)
value to insert.
(number)
index at which to append new element to.
tree.insertAt(20,2);
Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
void
:
no value.
Depth first search, Executes given callback functions with parameters BTreeNode and path index for each node in DFS fashion.
void
:
no value.
Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
void
:
no value.
Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
void
:
no value.
Returns an iterable of key, value pairs for every entry in the tree.
IterableIterator<[number, BTreeNode<T>]>
:
Iterable for iterations.
var tree = new BTree(10);
for (const [index, node] of tree.entries()) {
console.log(index, node.value); // 0, 10
}
Maps current tree values to a new tree with modifying the values using given callback function. Uses BFS.
BTree<T>
:
A new BTree
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,...}]
Filters each item based on given filter function
BTree<T>
:
New filtered instance of tree.
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,...}]
Reverses the current Binary Tree, Left Node becomes Right node and vise versa. Does not return new instance, returns current tree instance.
BTree<T>
:
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 first index of a value matched, if it is not present, it returns -1.
(T)
Any value to find.
number
:
Returns index of given item.
var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.indexOf(30); // => 3
tree.indexOf(51); // => -1
Checks if given item exists or not, returns boolean.
(T)
Any value to check if it exists or not.
boolean
:
Returns true if it is present, otherwise false.
var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.includes(30); // true
tree.includes(51); // false
Checks if given item exists or not, returns boolean.
(T)
Any value to check if it exists or not.
boolean
:
Returns true if it is present, otherwise false.
var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.exists(30); // true
tree.exists(51); // false
Checks if given item exists or not, returns boolean.
(T)
Any value to check if it exists or not.
boolean
:
Returns true if it is present, otherwise false.
var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.has(30); // true
tree.has(51); // false
Sorts the tree based on compare function, Has option to sort only at children level.
(Function)
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.
(a, b) => a - b)
(boolean)
Optiona, Flag to specify if first child of each node should sorted. Default is
false
.
void
:
Returns undefined.
var tree = BTree.fromArray([10, 200, 100, 50, 60, 90, 5, 3]);
tree.sort().toFlatArray(); // => [3,5,10,50,60,90,100,200]
Prints entire tree on the console, useful for logging and checking status.
void
:
Returns undefined.
var tree = BTree.fromArray([1, 2, 3]);
tree.print();
1 (1)
|- 2 (2)
|- 3 (3)
Returns the first matched tree node. Traverses using BFS.
(T)
any value to find inside the tree.
(BTreeNode<T> | null)
:
Returns the first matched tree node, if not found, returns null.
Returns index value from given path.
(Array<("U"
| "L"
| "R"
)>)
Array for U L or R, which represents the quickest path to get to a node.
number
:
Returns index value.
Returns Path equivalent to the given index.
(number)
Index number from which path to be calculated.
Array<("U"
| "L"
| "R"
)>
:
Path equivalent to the given index.
Converts given values into a Binary Tree.
(Array<T2>)
Any array of values.
BTree<T2>
:
Newly generated tree.
var tree = BTree.fromArray([10,20,30,40]);
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); // 30
Validates a BTree node, it must have a valid value (no undefined nor null).
boolean
:
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()); // true
Converts current node and all children nodes in json format.
BTreeNodeStruct
:
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}