From ef62ea9e461cc944876c7165788f08767d6d4b74 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 2 Sep 2019 22:18:53 -0700 Subject: [PATCH 1/3] wrote methods to pass tests --- lib/tree.rb | 103 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 6 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..8081123 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -19,37 +19,76 @@ def initialize # Time Complexity: # Space Complexity: def add(key, value) - raise NotImplementedError + new_node = TreeNode.new(key, value) + + if @root == nil + @root = new_node + else + add_helper(@root, new_node) + end end # Time Complexity: # Space Complexity: def find(key) - raise NotImplementedError + return nil if @root == nil + + current = @root + + until current == nil + if key == current.key + return current.value + elsif key < current.key + current = current.left + else + current = current.right + end + end + + return nil end # Time Complexity: # Space Complexity: def inorder - raise NotImplementedError + return [] if @root == nil + + current = @root + tree = [] + + return inorder_helper(current, tree) end # Time Complexity: # Space Complexity: def preorder - raise NotImplementedError + return [] if @root == nil + + current = @root + tree = [] + + return preorder_helper(current, tree) end # Time Complexity: # Space Complexity: def postorder - raise NotImplementedError + return [] if @root == nil + + current = @root + tree = [] + + return postorder_helper(current, tree) end # Time Complexity: # Space Complexity: def height - raise NotImplementedError + return 0 if @root == nil + + current = @root + + return height_helper(current, 1, 1) end # Optional Method @@ -63,4 +102,56 @@ def bfs def to_s return "#{self.inorder}" end + + private + + def add_helper(current, new_node) + return new_node if current == nil + + if current.key >= new_node.key + current.left = add_helper(current.left, new_node) + else + current.right = add_helper(current.right, new_node) + end + + return current + end + + def inorder_helper(current, tree) + return tree if current == nil + + inorder_helper(current.left, tree) + tree.push({ :key => current.key, :value => current.value }) + inorder_helper(current.right, tree) + + end + + def preorder_helper(current, tree) + return tree if current == nil + + tree.push({ :key => current.key, :value => current.value }) + + preorder_helper(current.left, tree) + preorder_helper(current.right, tree) + end + + def postorder_helper(current, tree) + return tree if current == nil + + postorder_helper(current.left, tree) + postorder_helper(current.right, tree) + + tree.push({ :key => current.key, :value => current.value }) + end + + def height_helper(current, current_height, max_height) + return max_height if current == nil + + max_height = current_height if current_height > max_height + + max_height = height_helper(current.left, current_height + 1, max_height) + max_height = height_helper(current.right, current_height +1, max_height) + + return max_height + end end From 2e7358494fc5e0f8a238da76499c532ee45be13d Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 2 Sep 2019 22:21:31 -0700 Subject: [PATCH 2/3] correction to readme defintion of find based on tests --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eec9657..1509c69 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ 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 -- `find(value)` - This method returns true if the given value is in the tree and false otherwise. +- `find(value)` - This method returns the corresponding value if the given key is in the tree and nil otherwise. - `inorder` - This method returns an array of all the elements in the tree, in order. - `postorder` - This method returns an array of all the elements in a postorder fashion (left, right , root). - `preorder` - This method returns an array of all the elements in a preorder fashion (root, left, right). From a19b2495f95ed2cbaad5ede79931d42943b3e961 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Tue, 3 Sep 2019 09:01:03 -0700 Subject: [PATCH 3/3] added time and space complexities --- lib/tree.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 8081123..050bd6a 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(n) (O(logn) if tree is balanced), where n is the height of the tree + # Space Complexity: O(1) def add(key, value) new_node = TreeNode.new(key, value) @@ -28,8 +28,8 @@ def add(key, value) end end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) (O(logn) if tree is balanced), where n is the height of the tree + # Space Complexity: O(1) def find(key) return nil if @root == nil @@ -48,8 +48,8 @@ def find(key) return nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n), where n is the number of nodes + # Space Complexity: O(n), where n is the number of nodes def inorder return [] if @root == nil @@ -59,8 +59,8 @@ def inorder return inorder_helper(current, tree) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n), where n is the number of nodes + # Space Complexity: O(n), where n is the number of nodes def preorder return [] if @root == nil @@ -70,8 +70,8 @@ def preorder return preorder_helper(current, tree) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n), where n is the number of nodes + # Space Complexity: O(n), where n is the number of nodes def postorder return [] if @root == nil @@ -81,8 +81,8 @@ def postorder return postorder_helper(current, tree) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n), where n is the number of nodes + # Space Complexity: O(1) def height return 0 if @root == nil