A No Dependency B-Tree in JavaScript

Treebeard

UPDATE: I added functions removeNode, removeBranch and findLargestNodeInSubtree in this commit

I implemented a simple binary search tree in JavaScript.
It has no dependencies and it was developed only with SublimeText and the Chrome browser.

I may add a print function later that shows the tree in a text diagram.
Checkout the code on GitHub and it has an MIT license so use it if you wish!

btree

A no dependency implementation of a b-tree in JavaScript.

The code below will result in a binary search tree that looks like the following image:

Binary Search Tree

var tree, node;

node = new Node(8);  
tree = new Tree(node); // node serves as the root node

node = new Node(8);  
tree.add(node); // increments counter on the node with value 8h

node = new Node(3);  
tree.add(node); // becomes the left child of 8

node = new Node(1);  
tree.add(node); // becomes the left child of 3

node = new Node(6);  
tree.add(node); // becomes the right child of 3

node = new Node(4);  
tree.add(node); // becomes the left child of 6

node = new Node(7);  
tree.add(node); // becomes the right child of 6

node = new Node(10);  
tree.add(node); // becomes the right child of 8

node = new Node(14);  
tree.add(node); // becomes the right child of 10

node = new Node(13);  
tree.add(node); // becomes the left child of 14

tree.depth(); // 4

console.log(tree.root);      // Node {value: 8, left: Node, right: Node, count: 2, parent: null…}  
console.log(tree.root.left); // Node {value: 3, left: Node, right: Node, count: 1, parent: Node…}  
console.log(tree.root.right);      // Node {value: 10, left: null, right: Node, count: 1, parent: Node…}  
/* tree.root.right expanded view
    {
        count: 1
        depth: 2
        isLeftChild: false
        isRightChild: true
        left: null
        parent: Node
        right: Node
        root: Node
        value: 10
    }
*/

tree.isRoot(tree.root);      // true  
tree.isRoot(tree.root.left); // false

tree.search(14) // Node {value: 14, left: Node, right: null, count: 1, parent: Node…}  
tree.search(8)  // Node {value: 8, left: Node, right: Node, count: 2, parent: null…}  
tree.search(55) // null  

Subscribe to our mailing list

* indicates required
Subscribe
X