From 0be94f8fc84215add10df7c0ba813ce936e41c12 Mon Sep 17 00:00:00 2001 From: Chris M Date: Tue, 20 Aug 2019 22:31:14 -0700 Subject: [PATCH 1/5] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 73287d1..eec9657 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Tree Exercise +This project is due **Monday September 2nd, 2019** + In this exercise you will implement, in Ruby, several Tree methods. - `add(value)` - This method adds a value to the Binary Search Tree From 5179a2804138b80c0b05f63abf60f656f47b3034 Mon Sep 17 00:00:00 2001 From: Elle K Date: Mon, 2 Sep 2019 20:37:09 -0700 Subject: [PATCH 2/5] Add a test for the height method One for empty tree, one for tree_with_nodes --- test/tree_test.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/tree_test.rb b/test/tree_test.rb index 8811f14..222b693 100644 --- a/test/tree_test.rb +++ b/test/tree_test.rb @@ -68,6 +68,16 @@ {:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}] end end + + 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 describe "breadth first search" do it "will give an empty array for an empty tree" do @@ -80,4 +90,4 @@ {:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}] end end -end \ No newline at end of file +end From 46ead49b122d336a2bfe65069788176f6ea075c4 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Thu, 20 Feb 2020 16:30:37 -0800 Subject: [PATCH 3/5] 1. Add & Find --- lib/tree.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..05b2990 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -18,14 +18,60 @@ def initialize # Time Complexity: # Space Complexity: + def add_helper(curr, key, value) + return TreeNode.new(key, value) if curr.nil? + + if key < curr.key + curr.left = add_helper(curr.left, key, value) + else + curr.right = add_helper(curr.right, key, value) + end + + return curr + end + def add(key, value) - raise NotImplementedError + @root = TreeNode.new(key, value) if @root.nil? + + add_helper(@root, key, value) + # ITERATION SOLUTION: + # curr = @root + # while true + # if key < curr.key + # if !curr.left.nil? + # curr = curr.left + # else + # curr.left = TreeNode.new(key, value) + # end + # elsif key > curr.key + # if !curr.right.nil? + # curr = curr.left + # else + # curr.left = TreeNode.new(key, value) + # end + # else + # curr.value = value + # end + # end end # Time Complexity: - # Space Complexity: + # Space Complexity: def find(key) - raise NotImplementedError + return nil if @root.nil? + + curr = @root + until curr.nil? + if key > curr.key + curr = curr.right + elsif key < curr.key + curr = curr.left + else + return curr.value + end + end + + return nil end # Time Complexity: From 89241808a66b8704adbfb48bc3b6bb3150b6ab55 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Wed, 26 Feb 2020 21:55:52 -0800 Subject: [PATCH 4/5] initial commit --- lib/tree.rb | 89 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 05b2990..1de33ee 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) + # Space Complexity: O(log n) def add_helper(curr, key, value) return TreeNode.new(key, value) if curr.nil? @@ -33,34 +33,14 @@ def add_helper(curr, key, value) def add(key, value) @root = TreeNode.new(key, value) if @root.nil? - add_helper(@root, key, value) - # ITERATION SOLUTION: - # curr = @root - # while true - # if key < curr.key - # if !curr.left.nil? - # curr = curr.left - # else - # curr.left = TreeNode.new(key, value) - # end - # elsif key > curr.key - # if !curr.right.nil? - # curr = curr.left - # else - # curr.left = TreeNode.new(key, value) - # end - # else - # curr.value = value - # end - # end + return add_helper(@root, key, value) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log n) + # Space Complexity: O(log n) def find(key) - return nil if @root.nil? - curr = @root + until curr.nil? if key > curr.key curr = curr.right @@ -74,28 +54,61 @@ def find(key) return nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) + def inorder_helper(curr, list) + return list if curr.nil? + + inorder_helper(curr.left, list) + list << {key: curr.key, value: curr.value} + inorder_helper(curr.right, list) + end + def inorder - raise NotImplementedError + return inorder_helper(@root, []) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(log n) + def preorder_helper(curr, list) + return list if curr.nil? + + list << { key: curr.key, value: curr.value } + preorder_helper(curr.left, list) + preorder_helper(curr.right, list) + end + def preorder - raise NotImplementedError + return preorder_helper(@root, []) + end + + # Time Complexity: O(n) + # Space Complexity: O(log n) + def postorder_helper(curr, list) + return list if curr.nil? + + postorder_helper(curr.left, list) + postorder_helper(curr.right, list) + list << { key: curr.key, value: curr.value } end - # Time Complexity: - # Space Complexity: def postorder - raise NotImplementedError + return postorder_helper(@root, []) + end + + # Time Complexity: O(n) + # Space Complexity: O(log n) + def height_helper(curr, height) + return height if curr.nil? + + height = [left, right].max + + left = height_helper(curr.left, height + 1) + right = height_helper(curr.right, height + 1) end - # Time Complexity: - # Space Complexity: def height - raise NotImplementedError + return height_helper(@root, 0) end # Optional Method From 6c368b37347eff4cc9b41138fb21c27b914b8518 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Sat, 16 May 2020 13:58:50 -0700 Subject: [PATCH 5/5] refactor based on feedback --- lib/tree.rb | 63 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 1de33ee..c8aa902 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -2,12 +2,12 @@ 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 @@ -16,8 +16,8 @@ def initialize @root = nil end - # Time Complexity: O(log n) - # Space Complexity: O(log n) + # Time Complexity: O(log n)/ O(n) + # Space Complexity: O(log n) for a balanced BST; O(n) for unbalanced BST def add_helper(curr, key, value) return TreeNode.new(key, value) if curr.nil? @@ -31,13 +31,11 @@ def add_helper(curr, key, value) end def add(key, value) - @root = TreeNode.new(key, value) if @root.nil? - - return add_helper(@root, key, value) + @root = add_helper(@root, key, value) end - # Time Complexity: O(log n) - # Space Complexity: O(log n) + # Time Complexity: O(log n)/O(n) + # Space Complexity: O(1) def find(key) curr = @root @@ -58,10 +56,13 @@ def find(key) # Space Complexity: O(n) def inorder_helper(curr, list) return list if curr.nil? - + + # inorder: left - root - right inorder_helper(curr.left, list) list << {key: curr.key, value: curr.value} inorder_helper(curr.right, list) + + return list end def inorder @@ -69,13 +70,16 @@ def inorder end # Time Complexity: O(n) - # Space Complexity: O(log n) + # Space Complexity: O(n) def preorder_helper(curr, list) return list if curr.nil? + # preorder: root - left - right list << { key: curr.key, value: curr.value } preorder_helper(curr.left, list) preorder_helper(curr.right, list) + + return list end def preorder @@ -83,13 +87,16 @@ def preorder end # Time Complexity: O(n) - # Space Complexity: O(log n) + # Space Complexity: O(n) def postorder_helper(curr, list) return list if curr.nil? + # postorder: left - right - root postorder_helper(curr.left, list) postorder_helper(curr.right, list) list << { key: curr.key, value: curr.value } + + return list end def postorder @@ -97,14 +104,15 @@ def postorder end # Time Complexity: O(n) - # Space Complexity: O(log n) + # Space Complexity: O(log n)/ O(n) def height_helper(curr, height) return height if curr.nil? - - height = [left, right].max - left = height_helper(curr.left, height + 1) - right = height_helper(curr.right, height + 1) + height += 1 + left_height = height_helper(curr.left, height) + right_height = height_helper(curr.right, height) + + return [left_height, right_height].max end def height @@ -112,10 +120,25 @@ def height end # Optional Method - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) def bfs - raise NotImplementedError + visted = [] + return visted if @root.nil? + + to_visit = [@root] + until to_visit.empty? + curr = to_visit.shift + visted << { + key: curr.key, + value: curr.value + } + + to_visit << curr.left if curr.left + to_visit << curr.right if curr.right + end + + return visted end # Useful for printing