Last active
August 29, 2015 14:13
-
-
Save bmvakili/aa79cce9b1ccbdd25ace to your computer and use it in GitHub Desktop.
Partial Binary Search Tree and Node implementation
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
/** | |
* Partial Binary Search Tree class. | |
* @author Bijan Vakili | |
*/ | |
var Node = function() { | |
// Private variable to access 'this' context from privileged method | |
var that = this; | |
// Public variables | |
this.left = null; // Node | |
this.right = null; // Node | |
this.value = null; // int | |
// Public/priveleged method | |
this.height = function() { | |
// include current node in calculation | |
var leftHeight = 1; | |
var rightHeight = 1; | |
// recursively get the height of left tree | |
if (that.left) { | |
leftHeight += that.left.height(); | |
} | |
// recursively get the height of right tree | |
if (that.right) { | |
rightHeight += that.right.height(); | |
} | |
// get the maximum of the left or right | |
var thisHeight = Math.max(leftHeight, rightHeight); | |
// if no left or right then do not count this | |
// all initialized nodes would left/right | |
if (!that.right && !that.left) { | |
thisHeight = 0; | |
} | |
return thisHeight; | |
} | |
} | |
var BST = function () { | |
// Private variables | |
// declare and initialize the root node | |
var root = new Node(); // Node | |
// Private method | |
var insert = function(value, parent) { | |
// if the parent has not be initialized, initialize it | |
// this would be the case for the root element on first run | |
if (parent.value === null) { | |
parent.value = value; | |
parent.left = new Node(); | |
parent.right = new Node(); | |
return true; | |
} | |
// if the value is greater, add to the right, otherwise if less, left | |
if (value > parent.value) { | |
insert(value, parent.right); | |
} else if (value < parent.value) { | |
insert(value, parent.left); | |
} else { | |
// Do nothing as BST is a set | |
// A set need not have duplicate values | |
} | |
return true; | |
} | |
// Public methods | |
this.add = function(value) { | |
// call the private method | |
insert(value, root); | |
} | |
// get the root node's height | |
this.height = function() { | |
return root.height(); | |
} | |
} | |
/** Begin Test **/ | |
$(document).ready(function() { | |
// Print function for test | |
var resultQuestion3 = $("#result-question3"); | |
function logIt(x) { | |
resultQuestion3.append(x + "<br />") | |
console.log(x); | |
} | |
/** | |
5 | |
3 12 | |
2 4 9 22 | |
15 55 | |
28 66 | |
**/ | |
var data = "5,3,4,2,12,22,55,66,15,28,9"; | |
var dataArray = data.split(","); | |
var tree = new BST(); | |
for (dataEntry of dataArray) { | |
tree.add(dataEntry); | |
} | |
logIt(""); | |
logIt("Test 1:"); | |
logIt("Entries: " + dataArray); | |
logIt("Height : " + tree.height()); | |
var tree2 = new BST(); | |
dataArray = [1]; | |
tree2.add(dataArray[0]); | |
logIt(""); | |
logIt("Test 2:"); | |
logIt("Entries: " + dataArray); | |
logIt("Height : " + tree2.height()); | |
var tree3 = new BST(); | |
dataArray = []; | |
for (var i = 0; i < 8; i++) { | |
tree3.add(i); | |
dataArray.push(i); | |
} | |
logIt(""); | |
logIt("Test 3:"); | |
logIt("Entries: " + dataArray); | |
logIt("Height : " + tree3.height()); | |
var tree4 = new BST(); | |
dataArray = []; | |
for (var i = 0; i > -7; i--) { | |
tree4.add(i); | |
dataArray.push(i); | |
} | |
logIt(""); | |
logIt("Test 4:"); | |
logIt("Entries: " + dataArray); | |
logIt("Height : " + tree4.height()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment