Javascript Data Structure Tutorial - Javascript Binary Tree








A tree is a nonlinear data structure that is used to store data in a hierarchical manner.

Tree data structures are used to store hierarchical data, such as the files in a file system.

A tree is made up of a set of nodes connected by edges.

Special types of trees, called binary trees, restrict the number of child nodes to no more than two.

A tree can be broken down into levels.

The root node is at level 0, its children are at level 1, those nodes' children are at level 2, and so on.

Example

A binary search tree is made up of nodes.

The definition for the Node class is:

function Node(data, left, right) { 
    this.data = data; 
    this.left = left; 
    this.right = right; 
    this.show = show; 
} 
function show() { 
    return this.data; 
} 

The Node object stores both data and links to other nodes (left and right).


function Node(data, left, right) { 
    this.data = data; // w  w w .ja v  a  2  s. c om
    this.left = left; 
    this.right = right; 
    this.show = show; 
} 
function show() { 
    return this.data; 
} 

function BST() { 
    this.root = null; 
    this.insert = insert; 
    this.inOrder = inOrder; 
    this.preOrder = preOrder;
    this.postOrder = postOrder;
    this.getMin = getMin;
    this.getMax = getMax;
    this.find = find;
    this.remove = remove;
    this.removeNode = removeNode;
} 
function insert(data) { 
    var n= new Node(data, null, null); 
    if (this.root == null){ 
        this.root = n; 
    } else { 
        var current = this.root; 
        var parent; 
        while (true){ 
            parent = current; 
            if (data < current.data) { 
                current = current.left; 
                if (current == null){ 
                    parent.left = n; 
                    break; 
                } 
            } else { 
                current = current.right; 
                if (current == null){ 
                    parent.right = n; 
                    break; 
                } 
            } 
        } 
    } 
} 
function inOrder(node) { 
    if (!(node == null)) { 
        inOrder(node.left); 
        console.log(node.show() + " "); 
        inOrder(node.right); 
    } 
} 
function preOrder(node) { 
    if (!(node == null)) { 
        console.log(node.show() + " "); 
        preOrder(node.left); 
        preOrder(node.right); 
    } 
} 

function postOrder(node) { 
    if (!(node == null)) { 
        postOrder(node.left); 
        postOrder(node.right); 
        console.log(node.show() + " "); 
    } 
} 

function getMin() { 
    var current = this.root; 
    while (!(current.left == null)) { 
        current = current.left; 
    } 
    return current.data; 
} 
function getMax() { 
    var current = this.root; 
    while (!(current.right == null)) { 
        current = current.right; 
    } 
    return current.data; 
} 

function find(data) { 
    var current = this.root; 
    while (current.data != data) { 
        if (data < current.data) { 
            current = current.left; 
        } else { 
            current = current.right; 
        } 
        if (current == null){ 
            return null; 
        } 
    } 
    return current; 
} 
function remove(data) { 
    root = removeNode(this.root, data); 
} 
function removeNode(node, data) { 
    if (node == null){ 
        return null; 
    } 
    if (data == node.data) { 
        // node has no children 
        if (node.left == null && node.right == null){ 
            return null; 
        } 
        // node has no left child 
        if (node.left == null){ 
            return node.right; 
        } 
        // node has no right child 
        if (node.right == null){ 
            return node.left; 
        } 
        // node has two children 
        var tempNode = getSmallest(node.right); 
        node.data = tempNode.data; 
        node.right = removeNode(node.right, tempNode.data); 
        return node; 
    } else if (data < node.data) { 
        node.left = removeNode(node.left, data); 
        return node; 
    } else { 
        node.right = removeNode(node.right, data); 
        return node; 
    } 
} 
var nums = new BST(); 
nums.insert(23); 
nums.insert(45); 
nums.insert(16); 
nums.insert(37); 
nums.insert(3); 
nums.insert(99); 
nums.insert(22); 
console.log("Inorder traversal: "); 
inOrder(nums.root); 

var min = nums.getMin(); 

console.log("The minimum value of the BST is: " + min); 
console.log("\n"); 
var max = nums.getMax(); 
console.log("The maximum value of the BST is: " + max); 


inOrder(nums.root); 
console.log("\n"); 
var value = 99; 
var found = nums.find(value); 
if (found != null){ 
    console.log("Found " + value + " in the BST."); 
} else { 
    console.log(value + " was not found in the BST."); 
} 

The code above generates the following result.





Example 2

The following code shows how to create a tree data structure.


var Tree = function(value) {
  this.value = value;//  ww w .j a  va  2  s .c  o m
  this.children = [];
};

Tree.prototype.addChild = function(value) {
  this.children.push(new Tree(value));
};

Tree.prototype.contains = function(value) {
  if (this.value === value) {
    return true;
  } else if (this.children.length) {
    var child = this.children;
    for (var i = 0, l = child.length; i < l; i++) {
      return child[i].contains(value) && true;
    }
  }
  return false;
};