Last active
November 21, 2017 13:26
-
-
Save nickangtc/8c20215e6562169ee371d2d83c0c44c1 to your computer and use it in GitHub Desktop.
Binary search tree implemented in JavaScript (tree not balanced)
This file contains hidden or 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
| /** | |
| * Binary search tree implementation in JavaScript. | |
| * | |
| * Note: the BST created here is a BST that is not "balanced," | |
| * meaning it has a lot of nodes on one side, | |
| * which degrades the BST's O(log n) time complexity. | |
| * | |
| * Note 2: I'll be adding a balancing function in another snippet. | |
| */ | |
| /** | |
| * Simple function that just logs whatever input it gets. | |
| * We will use this as the simple iterator function used in some methods. | |
| */ | |
| function log(value) { | |
| console.log(value); | |
| } | |
| /** | |
| * BinarySearchTree constructor. | |
| * @param {*} value - data stored in the binary search tree instance | |
| */ | |
| function BinarySearchTree (value) { | |
| this.value = value; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| BinarySearchTree.prototype.insert = function(value) { | |
| if (value <= this.value) { | |
| if (!this.left) this.left = new BinarySearchTree(value); | |
| else this.left.insert(value); | |
| } else if (value > this.value) { | |
| if (!this.right) this.right = new BinarySearchTree(value); | |
| else this.right.insert(value); | |
| } | |
| }; | |
| BinarySearchTree.prototype.contains = function (value) { | |
| if (value === this.value) return true; | |
| if (value < this.value) { | |
| if (!this.left) return false; | |
| else return this.left.contains(value); | |
| } else if (value > this.value) { | |
| if (!this.right) return false; | |
| else return this.right.contains(value); | |
| } | |
| }; | |
| /** | |
| * Traverse a BST with a depth-first approach. | |
| * There are 3 common kinds of traversal: | |
| * (1) in-order | |
| * (2) pre-order | |
| * (3) post-order | |
| * @param {function} iterator | |
| * @param {string} order - order in which to apply iterator (see 1-3 above) | |
| */ | |
| BinarySearchTree.prototype.traverseDepthFirst = function(iterator, order) { | |
| if (order === 'pre-order') iterator(this.value); | |
| if (this.left) this.left.traverseDepthFirst(iterator, order); | |
| if (order === 'in-order') iterator(this.value); | |
| if (this.right) this.right.traverseDepthFirst(iterator, order); | |
| if (order === 'post-order') iterator(this.value); | |
| }; | |
| /** | |
| * Traverse a BST with a breadth-first approach. | |
| * @param {function} iterator | |
| */ | |
| BinarySearchTree.prototype.traverseBreadthFirst = function(iterator) { | |
| var queue = [this]; | |
| while (queue.length > 0) { | |
| var currentBST = queue.shift(); | |
| iterator(currentBST.value); | |
| if (currentBST.left) queue.push(currentBST.left); | |
| if (currentBST.right) queue.push(currentBST.right); | |
| } | |
| }; | |
| BinarySearchTree.prototype.getMinValue = function() { | |
| if (!this.left) return this.value; | |
| return this.left.getMinValue(); | |
| }; | |
| BinarySearchTree.prototype.getMaxValue = function() { | |
| if (!this.right) return this.value; | |
| return this.right.getMaxValue(); | |
| }; | |
| // init and test | |
| var bst = new BinarySearchTree(10); | |
| bst.insert(30); | |
| bst.insert(35); | |
| bst.insert(100); | |
| bst.insert(50); | |
| bst.insert(55); | |
| bst.insert(60); | |
| bst.insert(20); | |
| bst.insert(70); | |
| bst.insert(95); | |
| bst.insert(5); | |
| log('bst.contains():'); | |
| log(bst.contains(20)); | |
| log(bst.contains(51)); | |
| log('\nbst.traverseDepthFirst:'); | |
| bst.traverseDepthFirst(log, 'in-order'); | |
| log('\nbst.traverseBreadthFirst:'); | |
| bst.traverseBreadthFirst(log); | |
| log('\nbst.getMinValue:'); | |
| log(bst.getMinValue()); | |
| log('\nbst.getMaxValue:'); | |
| log(bst.getMaxValue()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment