Javascript Data Structure Tree Binary Tree 4

Description

Javascript Data Structure Tree Binary Tree 4


function Node(data) {
  this.data = data;//from   www . j  a  v a2  s  .  c  o  m
  this.leftChild = null;
  this.rightChild = null;
}

function BinaryTree() {
  this.root = null;
};

BinaryTree.prototype.print = function() {
  console.log(this.root);
}

BinaryTree.prototype.isBinaryTreeEmpty = function() {
  if (this.root) {
    return false;
  } else {
    return true;
  }
};

var treeDepth = function(node) {
  var i, j;

  if (node === null) {
    return 0;
  }

  if (node.leftChild) {
    i = treeDepth(node.leftChild);
  } else {
    i = 0;
  }

  if (node.rightChild) {
    j = treeDepth(node.rightChild);
  } else {
    j = 0;
  }

  return i > j ? i+1 : j+1;
};

BinaryTree.prototype.binaryTreeDepth = function() {
  return treeDepth(this.root);
}

var insertChild = function(node, childName, newNode) {
  if (!node[childName]) {
    node[childName] = newNode;
  } else {
    insert(node[childName], newNode);
  }
};

var insert = function(node, newNode) {
  if (newNode.data < node.data) {
    insertChild(node, 'leftChild', newNode);
  } else {
    insertChild(node, 'rightChild', newNode);
  }
};

BinaryTree.prototype.insertNode = function(data) {
  var node = new Node(data);

  if (!this.root) {
    this.root = node;
  } else {
    insert(this.root, node);
  }
};


var checkCallback = function(callback) {
  if (!callback || typeof callback !== 'function') {
    throw ('Callback function error.');
  }
};

var preOrderTraverseNode = function(node, callback) {
  checkCallback(callback);
  if (node) {
    callback(node);
    preOrderTraverseNode(node.leftChild, callback);
    preOrderTraverseNode(node.rightChild, callback);
  }
};

BinaryTree.prototype.preOrderTraverse = function(callback) {
  preOrderTraverseNode(this.root, callback);
};


var inOrderTraverseNode = function(node, callback) {
  checkCallback(callback);

  if (node) {
    inOrderTraverseNode(node.leftChild, callback);
    callback(node);
    inOrderTraverseNode(node.rightChild, callback);
  }
};

BinaryTree.prototype.inOrderTraverse = function(callback) {
  inOrderTraverseNode(this.root, callback);
};


var postOrderTraverseNode = function(node, callback) {
  checkCallback(callback);

  if (node) {
    postOrderTraverseNode(node.leftChild, callback);
    postOrderTraverseNode(node.rightChild, callback);
    callback(node);
  }
};

BinaryTree.prototype.postOrderTraverse = function(callback) {
  postOrderTraverseNode(this.root, callback);
};

BinaryTree.prototype.levelOrderTraverse = function(callback) {
  var queue = [],
      e = null;
  checkCallback(callback);

  if (this.root) {
    queue.push(this.root);

    while(queue.length > 0) {
      e = queue.shift();
      callback(e);
      
      if (e.leftChild) {
        queue.push(e.leftChild);
      }

      if (e.rightChild) {
        queue.push(e.rightChild);
      }
    }
  }
};

var binaryTree = new BinaryTree();

binaryTree.insertNode(10);
binaryTree.insertNode(8);
binaryTree.insertNode(9);
binaryTree.insertNode(11);
binaryTree.insertNode(12);
binaryTree.insertNode(13);
binaryTree.insertNode(2);

console.log('tree:');
binaryTree.print()

console.log('binaryTreeDepth:', binaryTree.binaryTreeDepth());
// binaryTree.print();

console.log('preOrderTraverse');
binaryTree.preOrderTraverse(function(node) {
  console.log(node.data);
});

console.log('inOrderTraverse');
binaryTree.inOrderTraverse(function(node) {
  console.log(node.data);
});

console.log('postOrderTraverse');
binaryTree.postOrderTraverse(function(node) {
  console.log(node.data);
});

console.log('levelOrderTraverse');
binaryTree.levelOrderTraverse(function(node) {
  console.log(node.data);
});

module.exports = binaryTree;



PreviousNext

Related