-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Faezeh #19
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
base: master
Are you sure you want to change the base?
Space - Faezeh #19
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 , 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 , 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 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 |
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.
Clever use of a block-based method.
# Time Complexity: O(Log 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.
The traversals are all O(n) in time complexity.
# Time Complexity: O(Log 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 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.
⚠ 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.