Skip to content

Instantly share code, notes, and snippets.

@adrianhumphrey111
Created September 28, 2017 16:43
Show Gist options
  • Save adrianhumphrey111/349337104bda999ea3ff00c4ab3fb879 to your computer and use it in GitHub Desktop.
Save adrianhumphrey111/349337104bda999ea3ff00c4ab3fb879 to your computer and use it in GitHub Desktop.
var BinarySearchTree = function (value) {
var obj = Object.create(BinarySearchTree.prototype);
obj.value = value;
obj.left = null;
obj.right = null;
return obj;
};
BinarySearchTree.prototype.insert = function (value) {
//if this is the first node, add it as root node
var node = BinarySearchTree(value);
if (!this.value) {
this.value = value;
return;
}
else {
currentNode = this;
while (true) {
if (node.value < currentNode.value) {
if (!currentNode.left) {
currentNode.left = node;
break;
} else {
currentNode = currentNode.left;
}
} else {
if (!currentNode.right) {
currentNode.right = node;
break;
} else {
currentNode = currentNode.right;
}
}
}
}
}
BinarySearchTree.prototype.contains = function (value) {
currentNode = this;
while (true) {
if (value < currentNode.value) {
if (!currentNode.left) {
return false;
} else {
if (currentNode.left.value === value) {
return true;
}
currentNode = currentNode.left;
}
} else {
if (!currentNode.right) {
return false
} else {
if (currentNode.right.value === value) {
return true;
}
currentNode = currentNode.right;
}
}
}
}
BinarySearchTree.prototype.depthFirstLog = function (cb) {
var recurse = function(tree){
cb(tree.value);
if(tree.left){
recurse(tree.left);
}
if (tree.right) {
recurse(tree.right);
}
}
recurse(this);
}
BinarySearchTree.prototype.breadthFirstLog = function (cb) {
var arr = [];
//Keep track if this is the main tree passed in.
var firstTree = true;
arr.push(this)
cb(this.value);
//Traverse the main tree, return once array gets to 0, but not on the first call
var recurse = function (tree) {
while (arr.length > 0 || firstTree) {
firstTree = false;
if (tree.left) {
arr.push(tree.left);
cb(tree.left.value);
}
if (tree.right) {
arr.push(tree.right);
cb(tree.right.value);
}
if (arr.length === 0) {
return;
}
recurse(arr.shift());
}
}
recurse(arr.shift());
}
const hasPathToSum = function(node, targetSum) {
var sum = 0;
var found = false;
var root = node;
var recurse = function(tree){
if( sum === targetSum ){
found = true;
return;
}
if(tree.left && (!found)){
sum+=tree.left.value
recurse(tree.left);
if(!found){
sum-=(tree.left.value)
}
}
//If we get back up to the root node, reset sum to value of the
//root node
if(tree === root && (!found)){
sum = root.value;
}
if (tree.right && !found) {
sum+=tree.right.value
recurse(tree.right);
if(!found){
sum-=(tree.right.value)
}
}
}
sum = root.value
recurse(node);
return found;
};
/*
5
2 7
1 3 6 10
4 14
12
*/
var binarySearchTree = BinarySearchTree(5);
binarySearchTree.insert(2);
binarySearchTree.insert(3);
binarySearchTree.insert(7);
binarySearchTree.insert(6);
binarySearchTree.insert(1);
binarySearchTree.insert(4);
binarySearchTree.insert(10);
binarySearchTree.insert(14);
binarySearchTree.insert(12);
console.log(hasPathToSum(binarySearchTree, 14)); //true //5,2,3,4 = 14
console.log(hasPathToSum(binarySearchTree, 48)); //true //5,7,10,14,12 = 48
console.log(hasPathToSum(binarySearchTree, 10)); //true 5,2,3 = 10
console.log(hasPathToSum(binarySearchTree, 15)); //false
console.log(hasPathToSum(binarySearchTree, 11)); //false
console.log(hasPathToSum(binarySearchTree, 8)); //true 5,2,1 = 8
console.log(hasPathToSum(binarySearchTree, 100)); //false
console.log(binarySearchTree);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment