Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 79 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,101 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(1)
def add(key, value)
Comment on lines +19 to 21

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
@root = add_helper(@root, key, value)
end

# Time Complexity:
# Space Complexity:
def add_helper(root, key, value)
if root.nil?
root = TreeNode.new(key, value)
return root
end

if root.key > key
root.left = add_helper(root.left, key, value)
else
root.right = add_helper(root.right, key, value)
end

return root
end

# Time Complexity: O(log n)
# Space Complexity: O(log n)
def find(key)
Comment on lines +40 to 42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 , well done here, very compact.

raise NotImplementedError
return find_helper(@root, key)
end

# Time Complexity:
# Space Complexity:
def find_helper(node, key)
return nil if node.nil?

if node.key == key
return node.value
elsif node.key < key
return find_helper(node.right, key)
else
return find_helper(node.left, key)
end
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
Comment on lines +58 to 60

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
result = []
inorder_helper(@root, result)
return result
end

# Time Complexity:
# Space Complexity:
def inorder_helper(node, result)
return if node.nil?

inorder_helper(node.left, result)
result.push({:key => node.key, :value => node.value})
inorder_helper(node.right, result)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
Comment on lines +74 to 76

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
result = []
preorder_helper(@root, result)
return result
end
def preorder_helper(node, result)
return if node.nil?

# Time Complexity:
# Space Complexity:
result.push({:key => node.key, :value => node.value})
preorder_helper(node.left, result)
preorder_helper(node.right, result)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
Comment on lines +89 to 91

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
result = []
postorder_helper(@root, result)
return result
end

# Time Complexity:
# Space Complexity:
def postorder_helper(node, result)
return if node.nil?

postorder_helper(node.left, result)
postorder_helper(node.right, result)
result.push({:key => node.key, :value => node.value})
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def height
Comment on lines +105 to 107

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The space complexity would be O(h), where h is the height of the tree, due to the call stack.

raise NotImplementedError
return height_helper(@root, 0)
end

def height_helper(node, steps)
return steps if node.nil?
return [height_helper(node.left, steps + 1), height_helper(node.right, steps + 1)].max
end

# Optional Method
Expand Down