Last active
August 29, 2015 14:22
-
-
Save JessyGreben/fb7a9cfa8e59fdc174ae to your computer and use it in GitHub Desktop.
Binary Search Tree - Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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