From ee8e4fd4ca322ed70b6b6c06f3e58fb83bab7abf Mon Sep 17 00:00:00 2001 From: qqdipps Date: Wed, 4 Sep 2019 09:29:06 -0700 Subject: [PATCH 1/3] implemented methods and added unit tests for hieght --- lib/tree.rb | 78 +++++++++++++++++++++++++++++++++++++++++++---- test/tree_test.rb | 38 +++++++++++++++-------- 2 files changed, 98 insertions(+), 18 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..a15413d 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -19,37 +19,103 @@ def initialize # Time Complexity: # Space Complexity: def add(key, value) - raise NotImplementedError + new_node = TreeNode.new(key, value) + if !@root + @root = new_node + return + end + add_node(new_node, @root) + + end + + def add_node(new_node, curr) + if curr.key >= new_node.key + if !curr.left + curr.left = new_node + return + end + add_node(new_node, curr.left) + else + if !curr.right + curr.right = new_node + return + end + add_node(new_node, curr.right) + end end + # Time Complexity: # Space Complexity: def find(key) - raise NotImplementedError + return if !@root + curr = @root + while curr + if curr.key == key + return curr.value + elsif curr.key > key + curr = curr.left + else + curr = curr.right + end + end + return false end # Time Complexity: # Space Complexity: def inorder - raise NotImplementedError + inorder_traversal(@root, []) + end + + def inorder_traversal(node, node_array) + return node_array if !node + inorder_traversal(node.left, node_array) + visit(node, node_array) + inorder_traversal(node.right, node_array) + end + + def visit(node, arr) + arr.push({key: node.key, value: node.value}) end # Time Complexity: # Space Complexity: def preorder - raise NotImplementedError + preorder_traversal(@root, []) + end + + def preorder_traversal(node, node_array) + return node_array if !node + visit(node, node_array) + preorder_traversal(node.left, node_array) + preorder_traversal(node.right, node_array) end # Time Complexity: # Space Complexity: def postorder - raise NotImplementedError + postorder_traversal(@root, []) + end + + def postorder_traversal(node, node_array) + return node_array if !node + postorder_traversal(node.left, node_array) + postorder_traversal(node.right, node_array) + visit(node, node_array) end # Time Complexity: # Space Complexity: def height - raise NotImplementedError + longest_tree_path(@root) + end + + def longest_tree_path(node) + return 0 if !node + right = longest_tree_path(node.right) + left = longest_tree_path(node.left) + return [right, left].max + 1 end # Optional Method diff --git a/test/tree_test.rb b/test/tree_test.rb index 8811f14..302aae5 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -18,13 +18,12 @@ it "add & find values" do tree.add(5, "Peter") - expect(tree.find(5)).must_equal "Peter" - - tree.add(15, "Ada") - expect(tree.find(15)).must_equal "Ada" - tree.add(3, "Paul") + tree.add(15, "Ada") + expect(tree.find(5)).must_equal "Peter" expect(tree.find(3)).must_equal "Paul" + expect(tree.find(15)).must_equal "Ada" + end it "can't find anything when the tree is empty" do @@ -69,15 +68,30 @@ end end - describe "breadth first search" do - it "will give an empty array for an empty tree" do - expect(tree.bfs).must_equal [] + describe "hieght" do + it "will return 0 if tree is empty" do + expect(tree.height()).must_equal 0 end - it "will return an array of a level-by-level output of the tree" do - expect(tree_with_nodes.bfs).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"}, - {:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"}, - {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] + it "will return the nuber of nodes in the longest path" do + expect(tree_with_nodes.height).must_equal 4 + tree_with_nodes.add(60, "sam") + tree_with_nodes.add(58, "penny") + tree_with_nodes.add(65, "sam") + expect(tree_with_nodes.height).must_equal 6 end + end + + # describe "breadth first search" do + # it "will give an empty array for an empty tree" do + # expect(tree.bfs).must_equal [] + # end + + # it "will return an array of a level-by-level output of the tree" do + # expect(tree_with_nodes.bfs).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"}, + # {:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"}, + # {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] + # end + # end end \ No newline at end of file From 4d51663d9891cd330901e81c525d0172646a6baf Mon Sep 17 00:00:00 2001 From: qqdipps Date: Wed, 4 Sep 2019 17:28:50 -0700 Subject: [PATCH 2/3] finished bfs implementation --- lib/tree.rb | 70 ++++++++++++++++++++++++++--------------------- test/tree_test.rb | 22 +++++++-------- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index a15413d..98c72d7 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -16,8 +16,8 @@ def initialize @root = nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log(n)) where n is the number of nodes + # Space Complexity: O(1) def add(key, value) new_node = TreeNode.new(key, value) if !@root @@ -45,8 +45,8 @@ def add_node(new_node, curr) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log(n)) where n is the number of nodes + # Space Complexity: O(1) def find(key) return if !@root curr = @root @@ -62,51 +62,51 @@ def find(key) return false end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(n) "" def inorder inorder_traversal(@root, []) end - def inorder_traversal(node, node_array) - return node_array if !node - inorder_traversal(node.left, node_array) - visit(node, node_array) - inorder_traversal(node.right, node_array) + def inorder_traversal(node, node_list) + return node_list if !node + inorder_traversal(node.left, node_list) + visit(node, node_list) + inorder_traversal(node.right, node_list) end def visit(node, arr) arr.push({key: node.key, value: node.value}) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(n) "" def preorder preorder_traversal(@root, []) end - def preorder_traversal(node, node_array) - return node_array if !node - visit(node, node_array) - preorder_traversal(node.left, node_array) - preorder_traversal(node.right, node_array) + def preorder_traversal(node, node_list) + return node_list if !node + visit(node, node_list) + preorder_traversal(node.left, node_list) + preorder_traversal(node.right, node_list) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(n) "" def postorder postorder_traversal(@root, []) end - def postorder_traversal(node, node_array) - return node_array if !node - postorder_traversal(node.left, node_array) - postorder_traversal(node.right, node_array) - visit(node, node_array) + def postorder_traversal(node, node_list) + return node_list if !node + postorder_traversal(node.left, node_list) + postorder_traversal(node.right, node_list) + visit(node, node_list) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def height longest_tree_path(@root) end @@ -118,11 +118,19 @@ def longest_tree_path(node) return [right, left].max + 1 end - # Optional Method - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(n) "" def bfs - raise NotImplementedError + bfs_queue = [] + curr = @root + node_list = [] + while curr + visit(curr, node_list) + bfs_queue << curr.left if curr.left + bfs_queue << curr.right if curr.right + curr = bfs_queue.shift + end + return node_list end # Useful for printing diff --git a/test/tree_test.rb b/test/tree_test.rb index 302aae5..8f2afb4 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -83,15 +83,15 @@ end - # describe "breadth first search" do - # it "will give an empty array for an empty tree" do - # expect(tree.bfs).must_equal [] - # end - - # it "will return an array of a level-by-level output of the tree" do - # expect(tree_with_nodes.bfs).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"}, - # {:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"}, - # {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] - # end - # end + describe "breadth first search" do + it "will give an empty array for an empty tree" do + expect(tree.bfs).must_equal [] + end + + it "will return an array of a level-by-level output of the tree" do + expect(tree_with_nodes.bfs).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"}, + {:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"}, + {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] + end + end end \ No newline at end of file From 435b19653ff8a21c33b17090528a18e082c507a9 Mon Sep 17 00:00:00 2001 From: Sav <41908878+qqdipps@users.noreply.github.com> Date: Thu, 5 Sep 2019 07:55:12 -0700 Subject: [PATCH 3/3] Update tree.rb --- lib/tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tree.rb b/lib/tree.rb index 98c72d7..b54e341 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -106,7 +106,7 @@ def postorder_traversal(node, node_list) end # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(1) + # Space Complexity: O(n) def height longest_tree_path(@root) end