diff --git a/lib/tree.rb b/lib/tree.rb index c0d4b51..3701173 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -16,40 +16,122 @@ def initialize @root = nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(log n) + # Space Complexity: O(log n) 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 add_helper(current, new_node) + if new_node.key <= current.key + if current.left.nil? + return current.left = new_node + else + add_helper(current.left, new_node) + end + else + if current.right.nil? + return current.right = new_node + else + add_helper(current.right, new_node) + end + end + end + + # Time Complexity: O(log n) + # Space Complexity: O(log n) 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.left + else + current = current.right + end + end + + # if not found + return nil end - # Time Complexity: - # Space Complexity: + # Time Complexity: O(n) + # Space Complexity: O(n) def inorder - raise NotImplementedError + # This method returns an array of all the elements in the tree, in order. (left, root, right) + inorder_array = [] + inorder_helper(@root, inorder_array) end - # Time Complexity: - # Space Complexity: + def inorder_helper(current, array) + return array if current.nil? + + inorder_helper(current.left, array) + array << { key: current.key, value: current.value } + inorder_helper(current.right, array) + end + + # Time Complexity: O(n) + # Space Complexity: O(n) def preorder - raise NotImplementedError + # This method returns an array of all the elements in a preorder fashion (root, left, right). + preorder_array = [] + + preorder_helper(@root, preorder_array) end - # Time Complexity: - # Space Complexity: + def preorder_helper(current, array) + return array if current.nil? + + array << { key: current.key, value: current.value } + preorder_helper(current.left, array) + preorder_helper(current.right, array) + end + + # Time Complexity: O(n) + # Space Complexity: O(n) def postorder - raise NotImplementedError + # This method returns an array of all the elements in a postorder fashion (left, right, root). + postorder_array = [] + + postorder_helper(@root, postorder_array) end - # Time Complexity: - # Space Complexity: + def postorder_helper(current, array) + return array if current.nil? + + postorder_helper(current.left, array) + postorder_helper(current.right, array) + array << { key: current.key, value: current.value } + end + + # Time Complexity: O(n) - worst case for unbalanced, best case O(log n) + # Space Complexity: O(1) def height - raise NotImplementedError + # return 0 if @root == nil + + height_helper(@root) + end + + def height_helper(current, count = 0) + return count if current.nil? + + + left_height = height_helper(current.left, count + 1) + right_height = height_helper(current.right, count + 1) + return [left_height, right_height].max + + # return left_height > right_height ? left_height : right_height end # Optional Method