Skip to content

Conversation

@Galaxylaughing
Copy link

No description provided.

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

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

Nice work, some issues with time/space complexity but the methods all pretty much work. Delete is also 90% there, which is awesome.

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

Choose a reason for hiding this comment

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

Since the number of recursive calls never go above the depth of the tree, it's O(log n) for space complexity due to the recursive call stack.

This of course assumes the tree is balanced.

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

Choose a reason for hiding this comment

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

Since the number of recursive calls never go above the depth of the tree, it's O(log n) for space complexity due to the recursive call stack.

This of course assumes the tree is balanced.

# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(2n) or O(n)
def height

Choose a reason for hiding this comment

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

👍 , but again the space complexity is O(log n) for a balanced tree and O(n) for an unbalanced one.

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

Choose a reason for hiding this comment

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

👍

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

Choose a reason for hiding this comment

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

👍

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

Choose a reason for hiding this comment

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

👍

# else if the found node has 2 children
# - find the right child's leftmost leaf
right_child = current.right
leftmost_left = right_child.left

Choose a reason for hiding this comment

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

I think a better variable name here is leftmost_right = right_child.

Note I used right_child instead of right_child.left because you don't know if the right child has a left child as well.

raise NotImplementedError
end

def delete(key)

Choose a reason for hiding this comment

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

I think this method is 90% there. When you have a node with 2 children to delete, you can swap the value of that node with it's smallest child on the right subtree, and then call delete again on the right subtree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants