Skip to content

Latest commit

 

History

History
738 lines (465 loc) · 13.9 KB

File metadata and controls

738 lines (465 loc) · 13.9 KB

Table of Contents

BTree

BTree main class

Parameters

  • 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.

Examples

new BTree(10);
new BTree({ root: 10 });
new BTree({ value: 10 });

depth

Depth of the binary tree.

Type: number

iterator

Returns an iterable of key, value pairs for every entry in the tree.

Examples

var tree = new BTree(10);
for (const node of tree) {
 console.log(node.value); // 10
}

reduce

Reduces each node values using reduceFunction and returns final value.

Parameters

  • reduceFunction
  • initialValue T2 Optional, Accumulator/Initial value. (optional, default 0)

Examples

var tree = BTree.fromArray([10, 20, 30, 40]);
var sum = tree.reduce((acc, node) => acc + node); // => 100

Returns T2 Returns reduceed value

toString

Returns string value of given tree.

Examples

var tree = new BTree(10);
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.toString(); // "10102030"

toJSON

Returns JSON Form.

Examples

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.

toArray

Returns array value.

Examples

var tree = new BTree(10);
tree.insert(20);
tree.toArray(); // => [{value:10,...},{value:20,...}]

Returns Array<BTreeNode> Returns array form of given tree.

toFlatArray

Returns array of values of the tree.

Examples

var tree = new BTree(10);
tree.insert(20);
tree.toFlatArray(); // => [10,20]

Returns Array<T> Returns array form of given tree.

insert

Inserts the given value to the tree where first free left child node is found.

Parameters

  • val any any type of value to be added to tree node.

Examples

var tree = new BTree(10);
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.toString(); // "10102030"

Returns BTreeNode Returns newly created BTreeNode.

insertLeftMost

Inserts the given value to the tree where first free left child node is found.

Parameters

  • val T any type of value to be added to tree node.

Returns BTreeNode<T> Returns newly created BTreeNode.

insertRightMost

Inserts the given value to the tree where first free right child node is found.

Parameters

  • val T any type of value to be added to tree node.

Returns BTreeNode<T> Returns newly created BTreeNode.

delete

Deletes given value from tree. Travarsal = Root -> L -> R.

Parameters

  • val T Value to be removed.

Returns BTreeNode<T> Returns removed BTreeNode.

insertAt

Inserts given element at given location. If location is already taken then it does not insert any value.

Parameters

  • val T value to insert.
  • index number index at which to append new element to.

Examples

tree.insertAt(20,2);
  • Throws any UnreachableError

traverseBFS

Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.

Returns void no value.

traverseDFS

Depth first search, Executes given callback functions with parameters BTreeNode and path index for each node in DFS fashion.

Returns void no value.

each

Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.

Returns void no value.

forEach

Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.

Returns void no value.

entries

Returns an iterable of key, value pairs for every entry in the tree.

Examples

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.

map

Maps current tree values to a new tree with modifying the values using given callback function. Uses BFS.

Examples

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

filter

Filters each item based on given filter function

Examples

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.

reverse

Reverses the current Binary Tree, Left Node becomes Right node and vise versa. Does not return new instance, returns current tree instance.

Examples

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.

indexOf

Returns first index of a value matched, if it is not present, it returns -1.

Parameters

  • value T Any value to find.

Examples

var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.indexOf(30); // => 3
tree.indexOf(51); // => -1

Returns number Returns index of given item.

includes

Checks if given item exists or not, returns boolean.

Parameters

  • value T Any value to check if it exists or not.

Examples

var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.includes(30); // true
tree.includes(51); // false

Returns boolean Returns true if it is present, otherwise false.

exists

Checks if given item exists or not, returns boolean.

Parameters

  • value T Any value to check if it exists or not.

Examples

var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.exists(30); // true
tree.exists(51); // false

Returns boolean Returns true if it is present, otherwise false.

has

Checks if given item exists or not, returns boolean.

Parameters

  • value T Any value to check if it exists or not.

Examples

var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
tree.has(30); // true
tree.has(51); // false

Returns boolean Returns true if it is present, otherwise false.

sort

Sorts the tree based on compare function, Has option to sort only at children level.

Parameters

  • compareFnc 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.```ts (a, b) => a - b)
  • atOnlyFirstChildLevel boolean Optiona, Flag to specify if first child of each node should sorted. Default is false.

Examples

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.

print

Prints entire tree on the console, useful for logging and checking status.

Examples

var tree = BTree.fromArray([1, 2, 3]);
tree.print();
1 (1)
|- 2 (2)
|- 3 (3)

Returns void Returns undefined.

find

Returns the first matched tree node. Traverses using BFS.

Parameters

  • item T any value to find inside the tree.

Returns (BTreeNode<T> | null) Returns the first matched tree node, if not found, returns null.

getIndexFromPath

Returns index value from given path.

Parameters

  • path Array<("U" | "L" | "R")> Array for U L or R, which represents the quickest path to get to a node.

Returns number Returns index value.

getPathFromIndex

Returns Path equivalent to the given index.

Parameters

  • index number Index number from which path to be calculated.

Returns Array<("U" | "L" | "R")> Path equivalent to the given index.

fromArray

Converts given values into a Binary Tree.

Parameters

  • arr Array<T2> Any array of values.

Examples

var tree = BTree.fromArray([10,20,30,40]);

Returns BTree<T2> Newly generated tree.

BTreeNode

Binary Tree node class, contains 2 child nodes and single value.

Examples

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

validate

Validates a BTree node, it must have a valid value (no undefined nor null).

Examples

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

Returns boolean

toJSON

Converts current node and all children nodes in json format.

Examples

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

toString

Converts current node and all children nodes in json format and append them together. Goes in direction of U -> L -> R

Examples

var node = new BTreeNode({ value: 10 });
var lNode = new BTreeNode({ value: 15, lNode: node });
console.log(node.toString()); // "1015"

Returns string

getDepth

Returns depth of its children. Goes in direction of L -> R

Examples

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()); // 3

Returns number