From 5d1b63265ee0601a18407725572642a4be5b90fd Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Mon, 2 Sep 2019 11:42:22 -0700 Subject: [PATCH 1/5] add method --- lib/tree.rb | 53 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..999bff0 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -2,59 +2,76 @@ class TreeNode attr_reader :key, :value attr_accessor :left, :right - def initialize(key, val) + def initialize(key, val) @key = key @value = val @left = nil @right = nil - end + end end class Tree attr_reader :root + def initialize @root = nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) is worst case if tree is not balanced + # Space Complexity: O(1) def add(key, value) - raise NotImplementedError + new_node = TreeNode.new(key, value) + + if @root == nil + @root = new_node.key + return + end + + current = @root + + while current != nil + if current.key >= key && current.left != nil + current = current.left + elsif current.key < key && current.right != nil + current = current.right + end + end + current.key >= key ? current.left = new_node.key : current.right = new_node.key end - # Time Complexity: - # Space Complexity: + # Time Complexity: + # Space Complexity: def find(key) - raise NotImplementedError + return @root.key end - # Time Complexity: - # Space Complexity: + # Time Complexity: + # Space Complexity: def inorder raise NotImplementedError end - # Time Complexity: - # Space Complexity: + # Time Complexity: + # Space Complexity: def preorder raise NotImplementedError end - # Time Complexity: - # Space Complexity: + # Time Complexity: + # Space Complexity: def postorder raise NotImplementedError end - # Time Complexity: - # Space Complexity: + # Time Complexity: + # Space Complexity: def height raise NotImplementedError end # Optional Method - # Time Complexity: - # Space Complexity: + # Time Complexity: + # Space Complexity: def bfs raise NotImplementedError end From e1b8662e10968e56a09994696c2a899a03360488 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Mon, 2 Sep 2019 15:48:30 -0700 Subject: [PATCH 2/5] find method --- lib/tree.rb | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 999bff0..4cea626 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -17,32 +17,45 @@ def initialize @root = nil end - # Time Complexity: O(n) is worst case if tree is not balanced + # Time Complexity: O(n) is worst case if tree is not balanced where n is the height of the tree # Space Complexity: O(1) def add(key, value) new_node = TreeNode.new(key, value) if @root == nil - @root = new_node.key + @root = new_node return end current = @root while current != nil - if current.key >= key && current.left != nil + if current.key >= key + break if current.left == nil current = current.left - elsif current.key < key && current.right != nil + else + break if current.right == nil current = current.right end end - current.key >= key ? current.left = new_node.key : current.right = new_node.key + current.key >= key ? current.left = new_node : current.right = new_node end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) is worst case if tree is not balanced where n is the height of the tree + # Space Complexity: O(1) def find(key) - return @root.key + current = @root + + while current != nil + if current.key > key + current = current.left + elsif current.key < key + current = current.right + else + return current.value + end + end + return nil end # Time Complexity: From cf60c03f2d5ac7bab2e8e5247abc415ba33e4ef1 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Mon, 2 Sep 2019 16:55:02 -0700 Subject: [PATCH 3/5] time and space complexity --- lib/tree.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 4cea626..bda1afc 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -38,6 +38,7 @@ def add(key, value) current = current.right end end + current.key >= key ? current.left = new_node : current.right = new_node end @@ -58,26 +59,27 @@ def find(key) return nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) where n is the number of nodes in the tree + # Space Complexity: O(n) where n is the number of nodes in the tree def inorder - raise NotImplementedError + tree = [] + current = @root end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) where n is the number of nodes in the tree + # Space Complexity: O(n) where n is the number of nodes in the tree def preorder raise NotImplementedError end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) where n is the number of nodes in the tree + # Space Complexity: O(n) where n is the number of nodes in the tree xs4 def postorder raise NotImplementedError end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) where n is the number of nodes in the tree + # Space Complexity: O(1) def height raise NotImplementedError end From c1fa82c2211b926a023cdc0e9e857c06b9bdffa5 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Mon, 2 Sep 2019 17:56:44 -0700 Subject: [PATCH 4/5] got order methods done --- lib/tree.rb | 37 +++++++++++++++++++++++++++++++---- test/tree_test.rb | 49 ++++++++++++++++++++++------------------------- 2 files changed, 56 insertions(+), 30 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index bda1afc..b5fd1bd 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -62,20 +62,49 @@ def find(key) # Time Complexity: O(n) where n is the number of nodes in the tree # Space Complexity: O(n) where n is the number of nodes in the tree def inorder - tree = [] - current = @root + return inorder_recursion(current = @root, tree = []) + end + + def inorder_recursion(current, tree) + if current != nil + inorder_recursion(current.left, tree) + tree << { key: current.key, value: current.value } + inorder_recursion(current.right, tree) + else + return tree + end end # Time Complexity: O(n) where n is the number of nodes in the tree # Space Complexity: O(n) where n is the number of nodes in the tree def preorder - raise NotImplementedError + return preorder_recursion(current = @root, tree = []) + end + + def preorder_recursion(current, tree) + if current != nil + tree << { key: current.key, value: current.value } + preorder_recursion(current.left, tree) + preorder_recursion(current.right, tree) + else + return tree + end end # Time Complexity: O(n) where n is the number of nodes in the tree # Space Complexity: O(n) where n is the number of nodes in the tree xs4 def postorder - raise NotImplementedError + return postorder_recursion(current = @root, tree = []) + end + + def postorder_recursion(current, tree) + if current != nil + postorder_recursion(current.left, tree) + postorder_recursion(current.right, tree) + tree << { key: current.key, value: current.value } + else + return tree + end end # Time Complexity: O(n) where n is the number of nodes in the tree diff --git a/test/tree_test.rb b/test/tree_test.rb index 8811f14..4111962 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -1,10 +1,9 @@ -require_relative 'test_helper' - +require_relative "test_helper" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe Tree do - let (:tree) {Tree.new} + let (:tree) { Tree.new } let (:tree_with_nodes) { tree.add(5, "Peter") @@ -37,23 +36,21 @@ end it "will return the tree in order" do - - expect(tree_with_nodes.inorder).must_equal [{:key=>1, :value=>"Mary"}, {:key=>3, :value=>"Paul"}, - {:key=>5, :value=>"Peter"}, {:key=>10, :value=>"Karla"}, - {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] + expect(tree_with_nodes.inorder).must_equal [{ :key => 1, :value => "Mary" }, { :key => 3, :value => "Paul" }, + { :key => 5, :value => "Peter" }, { :key => 10, :value => "Karla" }, + { :key => 15, :value => "Ada" }, { :key => 25, :value => "Kari" }] end end - describe "preorder" do it "will give an empty array for an empty tree" do expect(tree.preorder).must_equal [] end it "will return the tree in preorder" do - expect(tree_with_nodes.preorder).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"}, - {:key=>1, :value=>"Mary"}, {:key=>10, :value=>"Karla"}, - {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] + expect(tree_with_nodes.preorder).must_equal [{ :key => 5, :value => "Peter" }, { :key => 3, :value => "Paul" }, + { :key => 1, :value => "Mary" }, { :key => 10, :value => "Karla" }, + { :key => 15, :value => "Ada" }, { :key => 25, :value => "Kari" }] end end @@ -63,21 +60,21 @@ end it "will return the tree in postorder" do - expect(tree_with_nodes.postorder).must_equal [{:key=>1, :value=>"Mary"}, {:key=>3, :value=>"Paul"}, - {:key=>25, :value=>"Kari"}, {:key=>15, :value=>"Ada"}, - {:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}] + expect(tree_with_nodes.postorder).must_equal [{ :key => 1, :value => "Mary" }, { :key => 3, :value => "Paul" }, + { :key => 25, :value => "Kari" }, { :key => 15, :value => "Ada" }, + { :key => 10, :value => "Karla" }, { :key => 5, :value => "Peter" }] 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 + # 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 From 199d4125f6c0fac6c31a2799b1bd38933a33e5db Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Mon, 2 Sep 2019 21:19:57 -0700 Subject: [PATCH 5/5] add but optionals done --- lib/tree.rb | 11 +++++++++-- test/tree_test.rb | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index b5fd1bd..39707d7 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -109,8 +109,15 @@ def postorder_recursion(current, tree) # Time Complexity: O(n) where n is the number of nodes in the tree # Space Complexity: O(1) - def height - raise NotImplementedError + def height(current = @root, current_height = 0, max_height = 0) + return max_height if current == nil + + max_height = current_height if current_height > max_height + max_height = height(current.left, current_height += 1, max_height) + current_height -= 1 + max_height = height(current.right, current_height += 1, max_height) + + return max_height end # Optional Method diff --git a/test/tree_test.rb b/test/tree_test.rb index 4111962..7bae9cb 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -66,6 +66,16 @@ end end + describe "height" do + it "will find the height of a tree" do + expect(tree_with_nodes.height()).must_equal 3 + end + + it "will return 0 for an empty tree" do + expect(tree.height()).must_equal 0 + end + end + # describe "breadth first search" do # it "will give an empty array for an empty tree" do # expect(tree.bfs).must_equal []