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_recursive(node, 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.

👍 , However the space complexity is O(log n) if the tree is balanced because of the call stack.

return TreeNode.new(key, value) if node.nil?
if key < node.key
node.left = add_recursive(node.left, key, value)
else
node.right = add_recursive(node.right, key, value)
end
return node
end

def add(key, value)
raise NotImplementedError
@root = add_recursive(@root, key, value)
end

# Time Complexity: O(log n)
# Space Complexity: O(1)
def find_recursive(node, key)
Comment on lines +35 to +37

Choose a reason for hiding this comment

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

👍 , However the space complexity is O(log n) if the tree is balanced because of the call stack.

return node.value if node.key == key

if key <= node.key
node.left = find_recursive(node.left, key)
else
node.right = find_recursive(node.right, key)
end
end

# Time Complexity:
# Space Complexity:
def find(key)
raise NotImplementedError
current = @root
return nil if !current

return find_recursive(root, key)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder_recursive(node, list)
Comment on lines +54 to +56

Choose a reason for hiding this comment

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

👍

return list if node.nil?

inorder_recursive(node.left, list)
list << {key: node.key, value: node.value}
inorder_recursive(node.right, list)
end

# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
return inorder_recursive(@root, [])
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder_recursive(node, list)
Comment on lines +68 to +70

Choose a reason for hiding this comment

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

👍

return list if node.nil?

list << {key: node.key, value: node.value}
preorder_recursive(node.left, list)
preorder_recursive(node.right, list)
end

# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError
return preorder_recursive(@root, [])
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder_recursive(node, list)
Comment on lines +82 to +84

Choose a reason for hiding this comment

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

👍

return list if node.nil?

postorder_recursive(node.left, list)
postorder_recursive(node.right, list)
list << {key: node.key, value: node.value}
end
def postorder
raise NotImplementedError
return postorder_recursive(@root, [])
end

# Time Complexity: O(n)
# Space Complexity: O(n
def height_recursive(node)
Comment on lines +95 to +97

Choose a reason for hiding this comment

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

👍 , the space complexity is O(log n) if the tree is balanced and O(n) if it's unbalanced.

return 0 if node.nil?

left = height_recursive(node.left)
right = height_recursive(node.right)

if left > right
height = left
else
height = right
end
height += 1
return height
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
return height_recursive(@root)
end

# Optional Method
Expand Down