Skip to content

Instantly share code, notes, and snippets.

@JessyGreben
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JessyGreben/fb7a9cfa8e59fdc174ae to your computer and use it in GitHub Desktop.
Save JessyGreben/fb7a9cfa8e59fdc174ae to your computer and use it in GitHub Desktop.
Binary Search Tree - Javascript
var BinarySearchTree = function() {
this.root = null;
};
//build a tree
BinarySearchTree.prototype.insert = function(value) {
//create a node object with the data as value
var node = {
value: value,
left: null,
right: null
},
var currentNode;
if (this.root === null) {
this.root = node;
} else {
currentNode = this.root;
while(true) {
if (currentNode.left > value) {
if (currentNode.left === null) {
currentNode.left = node;
break;
}
} else if (currentNode.right < value) {
if (currentNode.right === null) {
currentNode.right = node;
break;
}
} else {
break;
}
}
}
};
BinarySearchTree.prototype.traverse = function() {
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment