Skip to content

Instantly share code, notes, and snippets.

@adrianhumphrey111
Created September 14, 2017 17:56
Show Gist options
  • Save adrianhumphrey111/7746132288e2ecbbad24b8d99304bfe7 to your computer and use it in GitHub Desktop.
Save adrianhumphrey111/7746132288e2ecbbad24b8d99304bfe7 to your computer and use it in GitHub Desktop.
Timed Problem for finding level with the largest sum of level.
var BinarySearchTree = function (value) {
var obj = Object.create(BinarySearchTree.prototype);
obj.value = value;
obj.left = null;
obj.right = null;
obj.parent = 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) {
node.parent = currentNode;
currentNode.left = node;
break;
} else {
currentNode = currentNode.left;
}
} else {
if (!currentNode.right) {
node.parent = currentNode;
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);
}
var Node = function (value) {
var node = {};
node.value = value;
node.left = null;
node.right = null;
return node;
};
// //Tree = 5 Depth 0 = 5
// / \
// 2 20 Depth 1 = 22
// / \ / \
// 1 3 6 9 Depth 2 = 19 Should return 1
const findLargestLevel = function(tree) {
//Keep track of depth, reset on each node
var depth = 0;
var deepestLevel = 0;
//An array of arrays, each array at each index representing all the values at that level
var treeLevels = [[]];
//Add value to array of arrays
var addValue = (value, depth) => {
var depth = depth;
treeLevels[depth].push(value);
}
//Callback function for each node to find the depth
var findDepth = (tree) => {
if(tree.parent !== null){ //While this tree
depth++;
findDepth(tree.parent);
}else{ //if the tree's parent is equal to null, depth to the original node
return;
}
}
//Recursive function to traverse tree
var recurse = (tree) => {
//Find depth of node
findDepth(tree);
tree.depth = depth;
//Add empty array dynamically
if(depth > treeLevels.length - 1){
treeLevels.push([]);
}
//Add the trees value to the deepest level array according to the index equaling the depth of the node
addValue(tree.value, tree.depth);
//Reset depth
depth = 0;
//Recurse
if (tree.left) {
recurse(tree.left);
}
if (tree.right) {
recurse(tree.right);
}
}
//Function will find the sum of the the arrays at an index, index representing the level.
var findDeepestLevel = () => {
deepestLevel = 0;
var total = 0;
for (var i = 0; i < treeLevels.length; i++){
var newTotal = treeLevels[i].reduce(function(sum, value) {
return sum + value;
}, 0);
if( newTotal > total){
total = newTotal;
deepestLevel = i;
}
}
}
recurse(tree);
findDeepestLevel();
return deepestLevel;
}
var binarySearchTree = BinarySearchTree(5);
binarySearchTree.insert(2);
binarySearchTree.insert(3);
binarySearchTree.insert(20);
binarySearchTree.insert(6);
binarySearchTree.insert(9);
binarySearchTree.insert(1);
console.log(binarySearchTree);
console.log(findLargestLevel(binarySearchTree)); //Will return 1 for Level 1, which is a sum of 22
var binarySearchTree1 = BinarySearchTree(5);
binarySearchTree1.insert(2);
binarySearchTree1.insert(3);
binarySearchTree1.insert(7);
binarySearchTree1.insert(6);
binarySearchTree1.insert(9);
binarySearchTree1.insert(1);
console.log(binarySearchTree1);
console.log(findLargestLevel(binarySearchTree1)); //Will return 2 for Level 2, which is a sum of 19
var binarySearchTree2 = BinarySearchTree(30);
binarySearchTree2.insert(2);
binarySearchTree2.insert(3);
binarySearchTree2.insert(7);
binarySearchTree2.insert(6);
binarySearchTree2.insert(9);
binarySearchTree2.insert(1);
console.log(binarySearchTree2);
console.log(findLargestLevel(binarySearchTree2)); //Will return 0 for Level 0, which is a sum of 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment