Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Faezeh, pretty well done, except for a bug in height, overall nice work.
Take a look at my comments and let me know what questions you have.
| # Time Complexity: O(Log n) | ||
| # Space Complexity: O(1) | ||
| def add(key, value) |
There was a problem hiding this comment.
👍 , but the space complexity is O(log n) because of the call stack, assuming the tree is balanced.
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(1) | ||
| def find(key) |
There was a problem hiding this comment.
👍 , but the space complexity is O(log n) because of the call stack, assuming the tree is balanced.
| # Time Complexity: O(Log n) | ||
| # Space Complexity: O(n) | ||
| def inorder |
There was a problem hiding this comment.
👍 Since we hit each node in the list, the the time complexity is also O(n)
| def inorder_helper(node, &block) | ||
| return if node.nil? | ||
|
|
||
| inorder_helper(node.left, &block) | ||
| yield node | ||
| inorder_helper(node.right, &block) | ||
| end |
| # Time Complexity: O(Log n) | ||
| # Space Complexity: O(n) | ||
| def preorder |
There was a problem hiding this comment.
The traversals are all O(n) in time complexity.
| # Time Complexity: O(Log n) | ||
| # Space Complexity: O(n) | ||
| def postorder |
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def height |
There was a problem hiding this comment.
⚠ This doesn't quite work as it goes down the left and right sides of the tree but doesn't investigate all paths to the leaves.
A better solution:
def height_helper(current_node)
return 0 if current_node.nil?
return 1 +[height_helper(current_node.left), height_helper(current_node.right)].max
end
No description provided.