From ccfd456223047df460319151c9a48ec12afd47d3 Mon Sep 17 00:00:00 2001 From: Ringo Alcock Date: Wed, 31 Mar 2021 22:20:24 -0700 Subject: [PATCH 1/4] define add() and find() methods --- lib/tree.js | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/lib/tree.js b/lib/tree.js index 6c301d3..4f42aab 100644 --- a/lib/tree.js +++ b/lib/tree.js @@ -12,16 +12,54 @@ class Tree { this.root = null; } - // Time Complexity: ? - // Space Complexity: ? + // Time Complexity: O(log n) -- assuming the tree is balanced, as the number of nodes increases + // the number of times addHelper has to be called will only increase logarithmically + // Space Complexity: O(log n) -- assuming the tree is balanced, the number of calls to addHelper + // that will be on the stack at one time increases logarithmically, rather than linearly add(key, value) { - throw new Error("This method hasn't been implemented yet!"); + const newNode = new TreeNode(key, value); + + if (this.root === null) { + this.root = newNode; + return; + } else { + const currentNode = this.root; + return this.addHelper(newNode, currentNode); + } } - // Time Complexity: ? - // Space Complexity: ? + addHelper(newNode, currentNode) { + if (newNode.key > currentNode.key && currentNode.right === null) { + currentNode.right = newNode; + return; + } else if (newNode.key <= currentNode.key && currentNode.left === null) { + currentNode.left = newNode; + return; + } else if (newNode.key > currentNode.key) { + return this.addHelper(newNode, currentNode.right); + } else { + return this.addHelper(newNode, currentNode.left); + } + } + + // Time Complexity: O(log n) -- assuming the tree is balanced, as tree increases in size, + // the number of times findHelper has to be called will only increase logarithmically + // Space Complexity: O(log n) -- assuming the tree is balanced, the number of calls to findHelper + // on the stack at one time increases logarithmically find(key) { - throw new Error("This method hasn't been implemented yet!"); + const current = this.root; + return this.findHelper(key, current); + } + + findHelper(key, current) { + if (current === null) return null + if (current.key === key) return current.value + + if (current.key < key) { + return this.findHelper(key, current.right); + } else { + return this.findHelper(key, current.left); + } } // Time Complexity: ? From 151abb9bd21d0d990149c1e23e70a2a9d5b83057 Mon Sep 17 00:00:00 2001 From: Ringo Alcock Date: Thu, 1 Apr 2021 12:34:26 -0700 Subject: [PATCH 2/4] define inorder(), preorder(), postorder() methods --- lib/tree.js | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/tree.js b/lib/tree.js index 4f42aab..078c6f3 100644 --- a/lib/tree.js +++ b/lib/tree.js @@ -62,22 +62,52 @@ class Tree { } } - // Time Complexity: ? - // Space Complexity: ? + // Time Complexity: O(n). Method needs to visit every node in the tree + // Space Complexity: O(log n). The number of function calls on the stack will only be as great + // as the maximum height of the tree, which increases logarithmically to # of nodes (assuming balanced) inorder() { - throw new Error("This method hasn't been implemented yet!"); + return this.inorderHelper(this.root, []); } - // Time Complexity: ? - // Space Complexity: ? + inorderHelper(current, inorderList) { + if (current === null) return inorderList; + + const leftBranch = this.inorderHelper(current.left, inorderList); + leftBranch.push({key: current.key, value: current.value}); + const rightBranch = this.inorderHelper(current.right, leftBranch); + return rightBranch; + } + + // Time Complexity: O(n). Method needs to visit every node in the tree + // Space Complexity: O(log n). The number of function calls on the stack will only be as great + // as the maximum height of the tree, which increases logarithmically to # of nodes (assuming balanced) preorder() { - throw new Error("This method hasn't been implemented yet!"); + return this.preorderHelper(this.root, []); } - // Time Complexity: ? - // Space Complexity: ? + preorderHelper(current, preorderList) { + if (current === null) return preorderList; + + preorderList.push({key: current.key, value: current.value}); + const leftBranch = this.preorderHelper(current.left, preorderList); + const rightBranch = this.preorderHelper(current.right, leftBranch); + return rightBranch; + } + + // Time Complexity: O(n). Method needs to visit every node in the tree + // Space Complexity: O(log n). The number of function calls on the stack will only be as great + // as the maximum height of the tree, which increases logarithmically to # of nodes (assuming balanced) postorder() { - throw new Error("This method hasn't been implemented yet!"); + return this.postorderHelper(this.root, []); + } + + postorderHelper(current, postorderList) { + if (current === null) return postorderList; + + const leftBranch = this.postorderHelper(current.left, postorderList); + const rightBranch = this.postorderHelper(current.right, leftBranch); + postorderList.push({key: current.key, value: current.value}); + return postorderList } // Time Complexity: ? From 99b3c7d53e47011532188c9a94d100d5cad7e84d Mon Sep 17 00:00:00 2001 From: Ringo Alcock Date: Thu, 1 Apr 2021 14:43:42 -0700 Subject: [PATCH 3/4] define height() method --- lib/tree.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/tree.js b/lib/tree.js index 078c6f3..a90a6b1 100644 --- a/lib/tree.js +++ b/lib/tree.js @@ -110,10 +110,25 @@ class Tree { return postorderList } - // Time Complexity: ? - // Space Complexity: ? + // Time Complexity: O(n). Method needs to follow every branch to its end + // and therefore visits every node. + // Space Complexity: O(log n). The max number of function calls on the stack will + // only be the maximum height of the tree, which scales logarithmically to the + // size of the tree (if balanced) height() { - throw new Error("This method hasn't been implemented yet!"); + return this.heightHelper(this.root, 0) + } + + heightHelper(currentNode, count) { + if (currentNode === null) return count + + const rightBranch = this.heightHelper(currentNode.right, count + 1); + const leftBranch = this.heightHelper(currentNode.left, count + 1); + if (rightBranch > leftBranch) { + return rightBranch; + } else { + return leftBranch; + } } // Time Complexity: ? From 883a1142c6531e2edfee32ebf390806fce8b93d9 Mon Sep 17 00:00:00 2001 From: Ringo Alcock Date: Fri, 2 Apr 2021 16:31:18 -0700 Subject: [PATCH 4/4] define breadth first search --- lib/tree.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/tree.js b/lib/tree.js index a90a6b1..e72e13f 100644 --- a/lib/tree.js +++ b/lib/tree.js @@ -124,6 +124,7 @@ class Tree { const rightBranch = this.heightHelper(currentNode.right, count + 1); const leftBranch = this.heightHelper(currentNode.left, count + 1); + if (rightBranch > leftBranch) { return rightBranch; } else { @@ -131,10 +132,22 @@ class Tree { } } - // Time Complexity: ? - // Space Complexity: ? + // Time Complexity: O(n). The method needs to visit every node in the tree so the time taken + // will increase linear to the size of the tree increasing. + // Space Complexity: O(n). The size of the queue at any point in time will increase logarithmically + // with the size of the tree, but the results array will increase linear to the size of the tree. bfs() { - throw new Error("This method hasn't been implemented yet!"); + const queue = [ this.root ]; + const result = []; + + for (const node of queue) { + if (node) { + result.push({key: node.key, value: node.value}); + queue.push(node.left, node.right); + } + } + + return result; } // Useful for printing