From 3fbb52fb33a67738c9869a1520aa06da8aa4dad4 Mon Sep 17 00:00:00 2001 From: tithvorlakmok Date: Mon, 31 Aug 2020 10:00:39 -0700 Subject: [PATCH 1/2] Passed all the tests --- lib/tree.rb | 80 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 13 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..e1b13aa 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 + # raise NotImplementedError + # if the root is nil set the root to be a new node with the given value and return the node. + return @root = TreeNode.new(key, value) if @root.nil? + current = @root + while current != nil + if key <= current.key + if current.left.nil? + current.left = TreeNode.new(key, value) + return + end + current = current.left + else + if current.right.nil? + current.right = TreeNode.new(key, value) + return + end + current = current.right + end + end end # Time Complexity: # Space Complexity: def find(key) - raise NotImplementedError + return nil if @root.nil? + current = @root + while current != nil + if key == current.key + return current.value + elsif key > current.key + current = current.right + else + current = current.left + end + end end # Time Complexity: - # Space Complexity: - def inorder - raise NotImplementedError + # Space Complexity: O(n) + + def inorder(current = @root, array = []) + # traverse the left subtree + # visit the current node + # traverse the right subtree + return array if current.nil? + inorder(current.left, array) + array << { key: current.key, value: current.value } + inorder(current.right, array) + end - + # Time Complexity: # Space Complexity: - def preorder - raise NotImplementedError + def preorder(current = @root, array = []) + # visit the current node + # traverse the left subtree + # traverse the right subtree + return array if current.nil? + array << { key: current.key, value: current.value } + preorder(current.left, array) + preorder(current.right, array) end # Time Complexity: # Space Complexity: - def postorder - raise NotImplementedError + + def postorder(current = @root, array = []) + # traverse the left subtree + # traverse the right subtree + # visit the current node + return array if current.nil? + postorder(current.left, array) + postorder(current.right, array) + array << { key: current.key, value: current.value } end - + # Time Complexity: # Space Complexity: - def height - raise NotImplementedError + def height(current = @root) + # If the current node is nil return 0 + # Otherwise return 1 plus the maximum of the heights of the right and left subtrees + return 0 if current.nil? + left = height(current.left) + right = height(current.right) + left >= right ? 1 + left : 1 + right end # Optional Method From 0ee10443540532e8419e1a66fcdcfd661b5f866d Mon Sep 17 00:00:00 2001 From: tithvorlakmok Date: Mon, 31 Aug 2020 10:04:10 -0700 Subject: [PATCH 2/2] Added the time complexity and space complexity --- lib/tree.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index e1b13aa..d60e72b 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(1) def add(key, value) # raise NotImplementedError # if the root is nil set the root to be a new node with the given value and return the node. @@ -40,8 +40,8 @@ def add(key, value) end end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log n) + # Space Complexity: O(1) def find(key) return nil if @root.nil? current = @root @@ -56,7 +56,7 @@ def find(key) end end - # Time Complexity: + # Time Complexity: O(n) # Space Complexity: O(n) def inorder(current = @root, array = []) @@ -70,8 +70,8 @@ def inorder(current = @root, array = []) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) def preorder(current = @root, array = []) # visit the current node # traverse the left subtree @@ -82,8 +82,8 @@ def preorder(current = @root, array = []) preorder(current.right, array) end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) def postorder(current = @root, array = []) # traverse the left subtree @@ -95,8 +95,8 @@ def postorder(current = @root, array = []) array << { key: current.key, value: current.value } end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) def height(current = @root) # If the current node is nil return 0 # Otherwise return 1 plus the maximum of the heights of the right and left subtrees