Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
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.
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.
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 |
There was a problem hiding this comment.
👍 , 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 |
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder |
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def inorder |
| # 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.
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) |
There was a problem hiding this comment.
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.