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
94 changes: 76 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,100 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

Since you are doing recursion here, this is O(log n) assuming the tree is balanced. Remember the system stack used by recursion.

def add(key, value)
raise NotImplementedError
new_node = TreeNode.new(key, value)

if @root == nil
@root = new_node
else
add_node(@root, new_node)
end
end

# Time Complexity:
# Space Complexity:
def add_node(root, new_node)
if new_node.key < root.key
return root.left = new_node unless root.left
add_node(root.left, new_node)
elsif new_node.key > root.key
return root.right = new_node unless root.right
add_node(root.right, new_node)
end
end

# Time Complexity: O(n)
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
return nil if @root == nil

current = @root

until current == nil
if key == current.key
return current.value
elsif key < current.key
current = current.left
else
current = current.right
end
end

return nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
raise NotImplementedError
return [] if @root == nil

current = @root
tree = []

return inorder_helper(current, tree)

Choose a reason for hiding this comment

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

Did you forget to commit inorder helper? Ditto for the other traversals

end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
raise NotImplementedError
return [] if @root == nil

current = @root
tree = []

return preorder_helper(current, tree)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
raise NotImplementedError
return [] if @root == nil

current = @root
array = []

return postorder_helper(current, array)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

Since you are doing recursion I would say this is O(log n) where n is the number of elements ,assuming it's a balanced tree. Remember doing recursion requires a stack!

def height
raise NotImplementedError
return 0 if @root == nil

current = @root

return height_helper(current, 1, 1)
end

def height_helper(node)

Choose a reason for hiding this comment

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

👍

return 0 unless node
left_counter = height_helper(node.left)
right_counter = height_helper(node.right)
counter = left_counter > right_counter ? left_counter : right_counter
return counter += 1
end


# Optional Method
# Time Complexity:
# Space Complexity:
Expand Down