From e2c68f4587c114fda37a0dc9ee2b5da78b9b04c2 Mon Sep 17 00:00:00 2001 From: KamillaKhabibrakhmanova Date: Wed, 29 Jun 2016 21:43:41 -0400 Subject: [PATCH 1/3] added binary search tree --- DataStructures/BinarySearchTree.js | 123 +++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 DataStructures/BinarySearchTree.js diff --git a/DataStructures/BinarySearchTree.js b/DataStructures/BinarySearchTree.js new file mode 100644 index 0000000..9743a28 --- /dev/null +++ b/DataStructures/BinarySearchTree.js @@ -0,0 +1,123 @@ +var BST = function(val) { + this.val = val; +}; + +BST.prototype.contains = function(val) { + var curr = this; + var found = false; + + while(!found && curr) { + if (val === curr.val) { + found = true; + } + if (val > curr.val){ + curr = curr.right; + } else if (val < curr.val) { + curr = curr.left; + } + } + return found; +}; + +BST.prototype.insert = function(val){ + var valNode = new BST(val); + + while(true) { + if (val < this.val) { + if (!this.left) { + this.left = valNode; + break; + } else { + return this.left.insert(val); + } + } else if (val > this.val) { + if (!this.right) { + this.right = valNode; + break; + } else { + return this.right.insert(val) + } + } else return; + } +}; + +BST.prototype.traverse = function(process){ + function processNode(node) { + if (node.left) { + processNode(node.left) + } + process.call(this, node.val); + if (node.right) { + processNode(node.right); + } + } + processNode(this); +}; + +BST.prototype.size = function(){ + var length = 0; + + this.traverse(function(node){ + length ++; + }); + return length; +}; + +BST.prototype.toArray = function(){ + var array = []; + + this.traverse(function(node){ + array.push(node.val); + }); + return array; +}; + +BST.prototype.remove = function(value) { + var curr = this; + var parent = null; + var found = false; + var direction = null; + while (!found) { + if ( value === curr.val) { + found = true; + } + if (value < curr.val) { + parent = curr; + direction = 'left'; + curr = curr.left; + } else if (value > curr.val) { + parent = curr; + direction = 'right'; + curr = curr.right; + } + } + //no children - just remove node + if (!curr.left && !curr.right) { + parent[direction] = null; + //two children + } else if (curr.left && curr.right) { + var opposite = direction === 'right' ? 'left' : 'right'; + var last = curr[opposite]; + while (last[opposite]) { + last = last[opposite]; + } + curr.val = last.val; + + if (direction === 'left') { + last = curr.right; + while (last.right) { + last = last.right; + } + curr.val = last.val; + } + //one child - replace node with child + } else { + if (direction === 'left') { + if (curr.left) parent.left = curr.left; + else parent.left = curr.right; + } else { + if (curr.left) parent.right = curr.left; + else parent.right = curr.right; + } + } +} \ No newline at end of file From a1352bddddcec31baa6d1d5a2d36e2d9ab5387b7 Mon Sep 17 00:00:00 2001 From: KamillaKhabibrakhmanova Date: Wed, 29 Jun 2016 23:03:44 -0400 Subject: [PATCH 2/3] woops this is the right one --- Chapter4-TreesAndGraphs/4.1-IsBalanced.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Chapter4-TreesAndGraphs/4.1-IsBalanced.js diff --git a/Chapter4-TreesAndGraphs/4.1-IsBalanced.js b/Chapter4-TreesAndGraphs/4.1-IsBalanced.js new file mode 100644 index 0000000..d6d7ac4 --- /dev/null +++ b/Chapter4-TreesAndGraphs/4.1-IsBalanced.js @@ -0,0 +1,22 @@ +/* +Problem: Implement a function to check if a binary tree is balanced. +Here that is defined as a tree that the heights of the two subtrees of any node never differ by more than one. +*/ + +//building off of the binary search tree defined in the structures folder + +BST.prototype.isBalanced = function() { + console.log(this); + function hasRightAndLeft(node){ + if (!node.right && !node.left) return true; + else if (node.right && node.left) { + return true; + } else { + return false; + } + } + + while (this.left && this.right) { + return (hasRightAndLeft(this.left) && hasRightAndLeft(this.right)) + } +}; \ No newline at end of file From 411e4491610159e1ed62baf7791fa61b5c6ab0d5 Mon Sep 17 00:00:00 2001 From: KamillaKhabibrakhmanova Date: Wed, 29 Jun 2016 23:04:52 -0400 Subject: [PATCH 3/3] remove console log --- Chapter4-TreesAndGraphs/4.1-IsBalanced.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter4-TreesAndGraphs/4.1-IsBalanced.js b/Chapter4-TreesAndGraphs/4.1-IsBalanced.js index d6d7ac4..17d16b0 100644 --- a/Chapter4-TreesAndGraphs/4.1-IsBalanced.js +++ b/Chapter4-TreesAndGraphs/4.1-IsBalanced.js @@ -6,7 +6,7 @@ Here that is defined as a tree that the heights of the two subtrees of any node //building off of the binary search tree defined in the structures folder BST.prototype.isBalanced = function() { - console.log(this); + //check if node has both right & left (or neither) to see if balanced function hasRightAndLeft(node){ if (!node.right && !node.left) return true; else if (node.right && node.left) {