From 0bb1a1c145e231c7da242657387540cadbb177c0 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 31 Aug 2020 00:27:15 -0700 Subject: [PATCH 1/4] writes add, find, and inorder methods plus helpers --- lib/tree.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..90dc129 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -19,37 +19,91 @@ def initialize # Time Complexity: # Space Complexity: def add(key, value) - raise NotImplementedError + new_node = TreeNode.new(key, value) + return @root = new_node if !@root + add_helper(@root, new_node) + end + + def add_helper(current, new_node) + if new_node.key <= current.key + return current.left = new_node if !current.left + add_helper(current.left, new_node) + else + return current.right = new_node if !current.right + add_helper(current.right, new_node) + end end # Time Complexity: # Space Complexity: def find(key) - raise NotImplementedError + return if !@root + return @root.value if @root.key == key + + if key <= @root.key + find_helper(key, @root.left) + else + find_helper(key, @root.right) + end + end + + def find_helper(key, current) + return current.value if current.key == key + if key <= current.key + find_helper(key, current.left) + else + find_helper(key, current.right) + end end # Time Complexity: # Space Complexity: def inorder - raise NotImplementedError + return [] if !@root + nodes = [] + inorder_helper(@root, nodes) + return nodes + end + + def inorder_helper(current, nodes) + return if !current + # traverse the left subtree + inorder_helper(current.left, nodes) + # visit the current node + nodes.push( + { + key: current.key, + value: current.value + } + ) + #traverse the right subtree + inorder_helper(current.right, nodes) end # Time Complexity: # Space Complexity: def preorder raise NotImplementedError + # visit the current node + # traverse the left subtree + # traverse the right subtree end # Time Complexity: # Space Complexity: def postorder raise NotImplementedError + # traverse the left subtree + # traverse the right subtree + # visit the current node end # Time Complexity: # Space Complexity: def height raise NotImplementedError + # If the current node is nil return 0 + # Otherwise return 1 plus the maximum of the heights of the right and left subtrees end # Optional Method From 223f64f330a102f041a6c7736cc2d6f415a91420 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 31 Aug 2020 01:09:24 -0700 Subject: [PATCH 2/4] writes preorder, postorder, and height methods plus helpers --- lib/tree.rb | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 90dc129..4152c3d 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -14,6 +14,7 @@ class Tree attr_reader :root def initialize @root = nil + @nodes = [] end # Time Complexity: @@ -60,9 +61,8 @@ def find_helper(key, current) # Space Complexity: def inorder return [] if !@root - nodes = [] - inorder_helper(@root, nodes) - return nodes + inorder_helper(@root, @nodes) + return @nodes end def inorder_helper(current, nodes) @@ -83,27 +83,58 @@ def inorder_helper(current, nodes) # Time Complexity: # Space Complexity: def preorder - raise NotImplementedError + return [] if !@root + preorder_helper(@root, @nodes) + return @nodes + end + + def preorder_helper(current, nodes) + return if !current # visit the current node + nodes.push( + { + key: current.key, + value: current.value + } + ) # traverse the left subtree - # traverse the right subtree + preorder_helper(current.left, nodes) + #traverse the right subtree + preorder_helper(current.right, nodes) end # Time Complexity: # Space Complexity: def postorder - raise NotImplementedError + return [] if !@root + postorder_helper(@root, @nodes) + return @nodes + end + + def postorder_helper(current, nodes) + return if !current # traverse the left subtree - # traverse the right subtree + postorder_helper(current.left, nodes) + #traverse the right subtree + postorder_helper(current.right, nodes) # visit the current node + nodes.push( + { + key: current.key, + value: current.value + } + ) end # Time Complexity: # Space Complexity: - def height - raise NotImplementedError + def height(current = @root, height = 0) # If the current node is nil return 0 + return height if !current # Otherwise return 1 plus the maximum of the heights of the right and left subtrees + left = height(current.left, height + 1) + right = height(current.right, height + 1) + return left >= right ? left : right end # Optional Method From e047040794828f2ddd564aa09ed9ce385acf81dc Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 31 Aug 2020 01:18:03 -0700 Subject: [PATCH 3/4] dries up methods --- lib/tree.rb | 55 ++++++++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 4152c3d..c7f62f2 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -37,88 +37,75 @@ def add_helper(current, new_node) # Time Complexity: # Space Complexity: - def find(key) + def find(key, current = @root) return if !@root - return @root.value if @root.key == key - - if key <= @root.key - find_helper(key, @root.left) - else - find_helper(key, @root.right) - end - end - - def find_helper(key, current) return current.value if current.key == key - if key <= current.key - find_helper(key, current.left) - else - find_helper(key, current.right) - end + + key <= current.key ? find(key, current.left) : find(key, current.right) end # Time Complexity: # Space Complexity: def inorder - return [] if !@root - inorder_helper(@root, @nodes) + return @nodes if !@root + inorder_helper(@root) return @nodes end - def inorder_helper(current, nodes) + def inorder_helper(current) return if !current # traverse the left subtree - inorder_helper(current.left, nodes) + inorder_helper(current.left) # visit the current node - nodes.push( + @nodes.push( { key: current.key, value: current.value } ) #traverse the right subtree - inorder_helper(current.right, nodes) + inorder_helper(current.right) end # Time Complexity: # Space Complexity: def preorder - return [] if !@root - preorder_helper(@root, @nodes) + return @nodes if !@root + preorder_helper(@root) return @nodes end - def preorder_helper(current, nodes) + def preorder_helper(current) return if !current # visit the current node - nodes.push( + @nodes.push( { key: current.key, value: current.value } ) # traverse the left subtree - preorder_helper(current.left, nodes) + preorder_helper(current.left) #traverse the right subtree - preorder_helper(current.right, nodes) + preorder_helper(current.right) end # Time Complexity: # Space Complexity: def postorder - return [] if !@root - postorder_helper(@root, @nodes) + return @nodes if !@root + postorder_helper(@root) return @nodes end - def postorder_helper(current, nodes) + def postorder_helper(current) return if !current # traverse the left subtree - postorder_helper(current.left, nodes) + postorder_helper(current.left) #traverse the right subtree - postorder_helper(current.right, nodes) + postorder_helper(current.right) # visit the current node - nodes.push( + @nodes.push( { key: current.key, value: current.value From d9128842d0fe0cb7d40e2d32c8438d44ee142dc1 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 31 Aug 2020 01:39:13 -0700 Subject: [PATCH 4/4] adds comments on time and space complexity --- lib/tree.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c7f62f2..601b61b 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -17,8 +17,8 @@ def initialize @nodes = [] end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log n) for balanced trees, O(n) for unbalanced trees; where n is the number of nodes + # Space Complexity: O(n), where n is the height def add(key, value) new_node = TreeNode.new(key, value) return @root = new_node if !@root @@ -35,8 +35,8 @@ def add_helper(current, new_node) end end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log n) for balanced trees, O(n) for unbalanced trees; where n is the number of nodes + # Space Complexity: O(n), where n is the height def find(key, current = @root) return if !@root return current.value if current.key == key @@ -44,8 +44,8 @@ def find(key, current = @root) key <= current.key ? find(key, current.left) : find(key, current.right) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n), where n is the number of nodes + # Space Complexity: O(n), where n is the height def inorder return @nodes if !@root inorder_helper(@root) @@ -67,8 +67,8 @@ def inorder_helper(current) inorder_helper(current.right) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n), where n is the number of nodes + # Space Complexity: O(n), where n is the height def preorder return @nodes if !@root preorder_helper(@root) @@ -90,8 +90,8 @@ def preorder_helper(current) preorder_helper(current.right) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n), where n is the number of nodes + # Space Complexity: O(n), where n is the height def postorder return @nodes if !@root postorder_helper(@root) @@ -113,8 +113,8 @@ def postorder_helper(current) ) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n), where n is the number of nodes + # Space Complexity: O(n), where n is the height def height(current = @root, height = 0) # If the current node is nil return 0 return height if !current