Open
Conversation
One for empty tree, one for tree_with_nodes
Add a test for the height method
CheezItMan
reviewed
Feb 27, 2020
CheezItMan
left a comment
There was a problem hiding this comment.
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
Comment on lines
19
to
21
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(log n) | ||
| def add_helper(curr, key, value) |
lib/tree.rb
Outdated
Comment on lines
39
to
41
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(log n) | ||
| def find(key) |
Comment on lines
+57
to
+59
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def inorder_helper(curr, list) |
Comment on lines
71
to
73
| # Time Complexity: O(n) | ||
| # Space Complexity: O(log n) | ||
| def preorder_helper(curr, list) |
Comment on lines
85
to
87
| # Time Complexity: O(n) | ||
| # Space Complexity: O(log n) | ||
| def postorder_helper(curr, list) |
There was a problem hiding this comment.
Suggested change
| # 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
Comment on lines
106
to
107
| left = height_helper(curr.left, height + 1) | ||
| right = height_helper(curr.right, height + 1) |
There was a problem hiding this comment.
Suggested change
| 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.
What are left and right?
Suggested change
| 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.
Just for clarity on what's being returned, this is a minor suggestion.
Suggested change
| inorder_helper(curr.right, list) | |
| inorder_helper(curr.right, list) | |
| return list |
lib/tree.rb
Outdated
Comment on lines
34
to
36
| @root = TreeNode.new(key, value) if @root.nil? | ||
|
|
||
| return add_helper(@root, key, value) |
There was a problem hiding this comment.
Suggested change
| @root = TreeNode.new(key, value) if @root.nil? | |
| return add_helper(@root, key, value) | |
| @root = add_helper(@root, key, value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.