-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sabrina #32
base: master
Are you sure you want to change the base?
Sabrina #32
Conversation
There was a problem hiding this 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.
# Time Complexity: O(log n) | ||
# Space Complexity: O(n) | ||
def add(key, value) |
There was a problem hiding this comment.
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.
# Time Complexity: O(log n) | ||
# Space Complexity: O(n) | ||
def find(key) |
There was a problem hiding this comment.
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.
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(2n) or O(n) | ||
def height |
There was a problem hiding this comment.
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.
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
def bfs | ||
raise NotImplementedError | ||
end | ||
|
||
def delete(key) |
There was a problem hiding this comment.
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.
No description provided.