-
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
Leaves- Yitgop #46
base: master
Are you sure you want to change the base?
Leaves- Yitgop #46
Conversation
One for empty tree, one for tree_with_nodes
Add a test for the height method
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.
You clearly have the general idea on how to work with binary trees. That said there are a number of bugs here and some issues with time/space complexity. Take a look at my comments/suggestions and let me know any questions you have.
lib/tree.rb
Outdated
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def add_helper(curr, 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.
👍
lib/tree.rb
Outdated
# Time Complexity: O(log n) | ||
# Space Complexity: O(log 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.
👍
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder_helper(curr, list) |
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.
👍
lib/tree.rb
Outdated
# Time Complexity: O(n) | ||
# Space Complexity: O(log n) | ||
def preorder_helper(curr, list) |
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.
👍
lib/tree.rb
Outdated
# Time Complexity: O(n) | ||
# Space Complexity: O(log n) | ||
def postorder_helper(curr, list) |
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(log n) | |
def postorder_helper(curr, list) | |
# Time Complexity: O(n) | |
# Space Complexity: O(n) - since you're building a list | |
def postorder_helper(curr, list) |
lib/tree.rb
Outdated
left = height_helper(curr.left, height + 1) | ||
right = height_helper(curr.right, height + 1) |
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.
left = height_helper(curr.left, height + 1) | |
right = height_helper(curr.right, height + 1) | |
left = height_helper(curr.left, height + 1) | |
right = height_helper(curr.right, height + 1) | |
return [left, right].max |
lib/tree.rb
Outdated
def height_helper(curr, height) | ||
return height if curr.nil? | ||
|
||
height = [left, right].max |
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.
What are left and right?
height = [left, right].max |
|
||
inorder_helper(curr.left, list) | ||
list << {key: curr.key, value: curr.value} | ||
inorder_helper(curr.right, list) |
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.
Just for clarity on what's being returned, this is a minor suggestion.
inorder_helper(curr.right, list) | |
inorder_helper(curr.right, list) | |
return list |
lib/tree.rb
Outdated
@root = TreeNode.new(key, value) if @root.nil? | ||
|
||
return add_helper(@root, 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.
@root = TreeNode.new(key, value) if @root.nil? | |
return add_helper(@root, key, value) | |
@root = add_helper(@root, key, value) |
No description provided.