Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
You've got some bugs and some issues with time/space complexity. Take a look at my comments and suggestions and let me know if you have questions. It looks like you had trouble debugging this. You are welcome to use my office hours or the online TA Matt, if you like for additional support.
lib/tree.rb
Outdated
| Erika Maust <[email protected]> | ||
| 9:13 PM (1 minute ago) | ||
| to erika.maust |
There was a problem hiding this comment.
| Erika Maust <erika.maust@gmail.com> | |
| 9:13 PM (1 minute ago) | |
| to erika.maust |
lib/tree.rb
Outdated
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def add(key, value) |
There was a problem hiding this comment.
👍 , just what's the space/time complexity?
lib/tree.rb
Outdated
| @root = TreeNode.new(key,value) | ||
| else | ||
| current = @root | ||
| while true |
There was a problem hiding this comment.
This loop is going to run forever...
lib/tree.rb
Outdated
| # Time Complexity: Because it's recursive I want to say O log(n) | ||
| # Space Complexity: O log (n) if it's balanced, and I guess O(n) if it isn't | ||
| def find(key) |
There was a problem hiding this comment.
Because it's a binary tree the time complexity is O(log n) for a balanced tree and O(n) if it's not a balanced tree.
lib/tree.rb
Outdated
| # Time Complexity: I think time and space are both O(n)? | ||
| # Space Complexity: | ||
| def inorder |
lib/tree.rb
Outdated
| # Time Complexity: I feel like both are O(n) since each item has to be traversed, even if it's recursively? | ||
| # Space Complexity: | ||
| def preorder |
lib/tree.rb
Outdated
| # Time Complexity: Same as above, O(n)? | ||
| # Space Complexity: | ||
| def postorder |
lib/tree.rb
Outdated
| # Time Complexity: I'm not sure about time and space here. I think O(n) | ||
| # Space Complexity: | ||
| def height |
There was a problem hiding this comment.
Time complexity is O(n) since you visit each node once. Space complexity depends on the height of the tree so it's O(log n) for a balanced tree and O(n) for an unbalanced tree.
lib/tree.rb
Outdated
| current.left = TreeNode.new(key,value) | ||
| end | ||
| else | ||
| if [email protected]? |
There was a problem hiding this comment.
What is @current?
| if !@current.right.nil? | |
| if !current.right.nil? |
lib/tree.rb
Outdated
| preorder_helper(current_node.left, list) | ||
| preorder_helper(current_node.right, list) |
There was a problem hiding this comment.
| preorder_helper(current_node.left, list) | |
| preorder_helper(current_node.right, list) | |
| postorder_helper(current_node.left, list) | |
| postorder_helper(current_node.right, list) |
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work, you hit the learning goals here. Well done Erika!
| @right = nil | ||
| end | ||
|
|
||
| def add(key, value) |
There was a problem hiding this comment.
Nice adding these methods to the TreeNode class.
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(1) | ||
| def add(key, value) |
There was a problem hiding this comment.
👍 , time complexity is right if the tree is balanced.
| # Time Complexity: O (log n) | ||
| # Space Complexity: O(1) | ||
| def find(key) |
There was a problem hiding this comment.
👍 , time complexity is right if the tree is balanced.
| # # Time Complexity: O(n) | ||
| # # Space Complexity: O(n) | ||
| def inorder |
| # # Time Complexity: O(n) | ||
| # # Space Complexity: O(n) | ||
| def preorder |
| # # Time Complexity: O(n) | ||
| # # Space Complexity: O(n) | ||
| def postorder |
| # # Time Complexity: | ||
| # # Space Complexity: | ||
| def height |
No description provided.