From cc7a3f60fca7ed73fa39d1d2a9277221dc4e4580 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Sun, 25 Aug 2019 11:29:58 -0700 Subject: [PATCH 1/9] test github login --- lib/tree.rb | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..78c0c28 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -10,51 +10,53 @@ def initialize(key, val) end end +# test github login + class Tree attr_reader :root def initialize @root = nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: + # Space Complexity: def add(key, value) raise NotImplementedError end - # Time Complexity: - # Space Complexity: + # Time Complexity: + # Space Complexity: def find(key) raise NotImplementedError 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 7bdae566dfecd8190543197f841bb8cefd466939 Mon Sep 17 00:00:00 2001 From: Elle Kosling Date: Thu, 29 Aug 2019 18:33:42 -0700 Subject: [PATCH 2/9] Implemented add and find methods. Added minitest skip package. --- lib/tree.rb | 24 +++++++++++++++++++----- package-lock.json | 3 +++ test/tree_test.rb | 11 ++++++----- 3 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 package-lock.json diff --git a/lib/tree.rb b/lib/tree.rb index 78c0c28..9ef6278 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -10,8 +10,6 @@ def initialize(key, val) end end -# test github login - class Tree attr_reader :root def initialize @@ -21,13 +19,29 @@ def initialize # Time Complexity: # Space Complexity: def add(key, value) - raise NotImplementedError + if !@root + new_node = TreeNode.new(key, value) + @root = new_node + return + end + + current = @root + new_node = TreeNode.new(key, value) + new_node.key <= current.key ? current.left = new_node : current.right = new_node + end - # Time Complexity: + # Time Complexity: # Space Complexity: def find(key) - raise NotImplementedError + return nil if !@root + current = @root + + until current == nil + return current.value if key == current.key + key < current.key ? current = current.left : current = current.right + end + end # Time Complexity: diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..48e341a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +} diff --git a/test/tree_test.rb b/test/tree_test.rb index 8811f14..3b2185e 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -1,6 +1,7 @@ +require 'minitest/autorun' +require 'minitest/skip_dsl' require_relative 'test_helper' - Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe Tree do @@ -31,7 +32,7 @@ expect(tree.find(50)).must_be_nil end - describe "inorder" do + xdescribe "inorder" do it "will give an empty array for an empty tree" do expect(tree.inorder).must_equal [] end @@ -45,7 +46,7 @@ end - describe "preorder" do + xdescribe "preorder" do it "will give an empty array for an empty tree" do expect(tree.preorder).must_equal [] end @@ -57,7 +58,7 @@ end end - describe "postorder" do + xdescribe "postorder" do it "will give an empty array for an empty tree" do expect(tree.postorder).must_equal [] end @@ -69,7 +70,7 @@ end end - describe "breadth first search" do + xdescribe "breadth first search" do it "will give an empty array for an empty tree" do expect(tree.bfs).must_equal [] end From 50c6db66af06209c8b16a131a9bb5a9a973afcdf Mon Sep 17 00:00:00 2001 From: Elle Kosling Date: Sat, 31 Aug 2019 08:10:09 -0700 Subject: [PATCH 3/9] Began implementation of inorder function. Saving work. --- lib/tree.rb | 27 ++++++++++++++++++++++----- test/tree_test.rb | 3 ++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 9ef6278..803e4db 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(1) + # Space Complexity: O(1) def add(key, value) if !@root new_node = TreeNode.new(key, value) @@ -31,8 +31,8 @@ def add(key, value) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(logn) where n is the length of the array + # Space Complexity: O(1) def find(key) return nil if !@root current = @root @@ -47,7 +47,24 @@ def find(key) # Time Complexity: # Space Complexity: def inorder - raise NotImplementedError + @output = [] + current = @root + + return @output if !@root + + puts "@@@@@@" + + def analyze(current) + puts 'in analyze' + analyze(current.left) if current.left + @output << {:key => current.key, :value => current.value} + analyze(current.right) if current.right + end + # + analyze(current) + puts "output: #{@output}" + + return @output end # Time Complexity: diff --git a/test/tree_test.rb b/test/tree_test.rb index 3b2185e..720b3bf 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -11,6 +11,7 @@ tree.add(5, "Peter") tree.add(3, "Paul") tree.add(1, "Mary") + # tree.add(4, "Elle") tree.add(10, "Karla") tree.add(15, "Ada") tree.add(25, "Kari") @@ -32,7 +33,7 @@ expect(tree.find(50)).must_be_nil end - xdescribe "inorder" do + describe "inorder" do it "will give an empty array for an empty tree" do expect(tree.inorder).must_equal [] end From e44e3dfe844bf13c8b29755eee270fdf35955d76 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Sun, 1 Sep 2019 12:58:02 -0700 Subject: [PATCH 4/9] Continued to work on the analyze recursion problem. --- lib/tree.rb | 13 ++++++++----- test/tree_test.rb | 21 +++++++++++---------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 803e4db..4c1606b 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -28,10 +28,10 @@ def add(key, value) current = @root new_node = TreeNode.new(key, value) new_node.key <= current.key ? current.left = new_node : current.right = new_node - + end - # Time Complexity: O(logn) where n is the length of the array + # Time Complexity: O(logn) where n is the length of the array if the tree is balanced. O(n) if not. # Space Complexity: O(1) def find(key) return nil if !@root @@ -52,15 +52,18 @@ def inorder return @output if !@root - puts "@@@@@@" - def analyze(current) puts 'in analyze' analyze(current.left) if current.left + + if current.left == @output.last && @output.last != current.right + analyze(current) + end + @output << {:key => current.key, :value => current.value} analyze(current.right) if current.right end - # + # analyze(current) puts "output: #{@output}" diff --git a/test/tree_test.rb b/test/tree_test.rb index 720b3bf..4a7b7b9 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -11,8 +11,9 @@ tree.add(5, "Peter") tree.add(3, "Paul") tree.add(1, "Mary") - # tree.add(4, "Elle") + tree.add(4, "Elle") tree.add(10, "Karla") + tree.add(9, "Brad") tree.add(15, "Ada") tree.add(25, "Kari") tree @@ -40,8 +41,8 @@ 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"}, + 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 @@ -53,8 +54,8 @@ 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"}, + 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 @@ -65,8 +66,8 @@ 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"}, + 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 @@ -77,9 +78,9 @@ 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"}, + 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 +end From 66256e3891e6587376629e448f3569ccb5103a15 Mon Sep 17 00:00:00 2001 From: Mello-Cello Date: Sun, 1 Sep 2019 15:17:55 -0700 Subject: [PATCH 5/9] Fixed add method. All tests passing for add and inorder. --- lib/tree.rb | 47 +++++++++++++++++++++++++++++++++++++++++------ test/tree_test.rb | 4 ++-- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 4c1606b..3589ea2 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -27,7 +27,41 @@ def add(key, value) current = @root new_node = TreeNode.new(key, value) - new_node.key <= current.key ? current.left = new_node : current.right = new_node + done = false + + until done + node_left_of_curr = current.left + node_right_of_curr = current.right + if new_node.key <= current.key # GO LEFT + if !current.left + current.left = new_node + done = true + puts "current #{current.key}" + elsif new_node.key > node_left_of_curr.key + new_node.left = node_left_of_curr + current.left = new_node + done = true + puts "current #{current.key}" + else + current = current.left + puts "current #{current.key}" + end + else # new_node.value > current.value # GO RIGHT + if !current.right + current.right = new_node + done = true + puts "current #{current.key}" + elsif new_node.key < node_right_of_curr.key + new_node.right = node_right_of_curr + current.right = new_node + done = true + puts "current #{current.key}" + else + current = current.right + puts "current #{current.key}" + end + end + end end @@ -53,15 +87,16 @@ def inorder return @output if !@root def analyze(current) - puts 'in analyze' + # puts 'in analyze' analyze(current.left) if current.left - if current.left == @output.last && @output.last != current.right - analyze(current) - end - @output << {:key => current.key, :value => current.value} analyze(current.right) if current.right + + # if !current.left && !current.right + # return + # end + end # analyze(current) diff --git a/test/tree_test.rb b/test/tree_test.rb index 4a7b7b9..efda319 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -11,9 +11,9 @@ tree.add(5, "Peter") tree.add(3, "Paul") tree.add(1, "Mary") - tree.add(4, "Elle") + # tree.add(4, "Elle") tree.add(10, "Karla") - tree.add(9, "Brad") + # tree.add(9, "Brad") tree.add(15, "Ada") tree.add(25, "Kari") tree From d2ff7b9bb9f1cc28ec530d2ab9db96a598baf2fe Mon Sep 17 00:00:00 2001 From: Elle Kosling Date: Mon, 2 Sep 2019 13:14:55 -0700 Subject: [PATCH 6/9] Cleaned up methods and comments, added big O.) --- lib/tree.rb | 42 ++++++++---------------------------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 3589ea2..550884c 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -16,7 +16,7 @@ def initialize @root = nil end - # Time Complexity: O(1) + # Time Complexity: O(logn) if the tree is balanced (best case), O(n) if tree is unbalanced (worst case) # Space Complexity: O(1) def add(key, value) if !@root @@ -27,42 +27,24 @@ def add(key, value) current = @root new_node = TreeNode.new(key, value) - done = false - until done - node_left_of_curr = current.left - node_right_of_curr = current.right + until !current if new_node.key <= current.key # GO LEFT if !current.left current.left = new_node - done = true - puts "current #{current.key}" - elsif new_node.key > node_left_of_curr.key - new_node.left = node_left_of_curr - current.left = new_node - done = true - puts "current #{current.key}" + return else current = current.left - puts "current #{current.key}" end else # new_node.value > current.value # GO RIGHT if !current.right current.right = new_node - done = true - puts "current #{current.key}" - elsif new_node.key < node_right_of_curr.key - new_node.right = node_right_of_curr - current.right = new_node - done = true - puts "current #{current.key}" + return else current = current.right - puts "current #{current.key}" end end end - end # Time Complexity: O(logn) where n is the length of the array if the tree is balanced. O(n) if not. @@ -78,8 +60,8 @@ def find(key) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n-squared) + # Space Complexity: O(n) where n is the height of the recursion stack def inorder @output = [] current = @root @@ -87,21 +69,13 @@ def inorder return @output if !@root def analyze(current) - # puts 'in analyze' analyze(current.left) if current.left - @output << {:key => current.key, :value => current.value} analyze(current.right) if current.right - - # if !current.left && !current.right - # return - # end - end - # + analyze(current) - puts "output: #{@output}" - + return @output end From 1c0c6a9a3d7ae00dea21d58a4304d7f703dfbe7c Mon Sep 17 00:00:00 2001 From: Elle Kosling Date: Mon, 2 Sep 2019 14:53:51 -0700 Subject: [PATCH 7/9] Wrote preorder and postorder methods. --- lib/tree.rb | 46 ++++++++++++++++++++++++++++++++++++---------- test/tree_test.rb | 4 ++-- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 550884c..b1f8ccc 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -68,27 +68,53 @@ def inorder return @output if !@root - def analyze(current) - analyze(current.left) if current.left + def analyzeInorder(current) + analyzeInorder(current.left) if current.left @output << {:key => current.key, :value => current.value} - analyze(current.right) if current.right + analyzeInorder(current.right) if current.right end - analyze(current) + analyzeInorder(current) return @output end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n-squared) + # Space Complexity: O(n) where n is the height of the recursion stack def preorder - raise NotImplementedError + @output = [] + current = @root + + return @output if !@root + + def analyzePreorder(current) + @output << {:key => current.key, :value => current.value} + analyzePreorder(current.left) if current.left + analyzePreorder(current.right) if current.right + end + + analyzePreorder(current) + + return @output end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n-squared) + # Space Complexity: O(n) where n is the height of the recursion stack def postorder - raise NotImplementedError + @output = [] + current = @root + + return @output if !@root + + def analyzePostorder(current) + analyzePostorder(current.left) if current.left + analyzePostorder(current.right) if current.right + @output << {:key => current.key, :value => current.value} + end + + analyzePostorder(current) + + return @output end # Time Complexity: diff --git a/test/tree_test.rb b/test/tree_test.rb index efda319..a162463 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -48,7 +48,7 @@ end - xdescribe "preorder" do + describe "preorder" do it "will give an empty array for an empty tree" do expect(tree.preorder).must_equal [] end @@ -60,7 +60,7 @@ end end - xdescribe "postorder" do + describe "postorder" do it "will give an empty array for an empty tree" do expect(tree.postorder).must_equal [] end From a50f83c9c007c46bc7b1ce00b4fecfc0cc287ac7 Mon Sep 17 00:00:00 2001 From: Elle Kosling Date: Mon, 2 Sep 2019 20:04:37 -0700 Subject: [PATCH 8/9] Implemented height count. --- lib/tree.rb | 17 ++++++++++++++--- test/tree_test.rb | 12 +++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index b1f8ccc..9e493ba 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -117,10 +117,21 @@ def analyzePostorder(current) return @output end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log n) if tree is balanced, O(n) if unbalanced + # Space Complexity: O(1) def height - raise NotImplementedError + return 0 if !@root + + def findMaxHeight(node) + return 0 if !node + + height_left = findMaxHeight(node.left) + height_right = findMaxHeight(node.right) + + return height_left > height_right ? (height_left + 1) : (height_right + 1) + end + + findMaxHeight(@root) end # Optional Method diff --git a/test/tree_test.rb b/test/tree_test.rb index a162463..9886b14 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -72,7 +72,17 @@ end end - xdescribe "breadth first search" do + describe "height" do + it "returns 0 for an empty tree" do + expect(tree.height).must_equal 0 + end + + it "returns correct height of tree" do + expect(tree_with_nodes.height).must_equal 4 + end + end + + xdescribe "breadth first search" do # writing this method is optional it "will give an empty array for an empty tree" do expect(tree.bfs).must_equal [] end From e8dc9210788202f660a6dfa015edeafaa27876d0 Mon Sep 17 00:00:00 2001 From: Elle K Date: Mon, 2 Sep 2019 20:28:47 -0700 Subject: [PATCH 9/9] Update tree_test.rb --- test/tree_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tree_test.rb b/test/tree_test.rb index 9886b14..07c8c3e 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -82,7 +82,7 @@ end end - xdescribe "breadth first search" do # writing this method is optional + xdescribe "breadth first search" do # optional it "will give an empty array for an empty tree" do expect(tree.bfs).must_equal [] end