Conversation
One for empty tree, one for tree_with_nodes
Add a test for the height method
CheezItMan
left a comment
There was a problem hiding this comment.
Nicely done, all the methods work, but you do have issues with time/space complexity. Take a look at my comments and let me know what questions you have.
lib/tree.rb
Outdated
| # Time Complexity: O(logn) in worst case scenario | ||
| # Space Complexity: O(1) always just adding 1 new node | ||
| def add(key, value) |
There was a problem hiding this comment.
Actually O(log n) is the best and average case runtime.
Since you're doing recursion, you do incur some space complexity.
There was a problem hiding this comment.
@CheezItMan for the time complexity - would it be O(n) for worst case, and is that possible only if the tree is unbalanced?
| # Time Complexity: O(logn) since binary | ||
| # Space Complexity: O(1) since adding nothing new | ||
| def find(key) |
There was a problem hiding this comment.
Similar issues with time/space to add
| # Time Complexity: O(n) since going to each node once, backtracking but still backtracking a smaller number of times than there are nodes, so it depends on the number of nodes no matter what. | ||
| # Space Complexity: O(1) - nothing being made | ||
| def inorder |
There was a problem hiding this comment.
Space complexity is O(n) since you're building an array!
| # Time Complexity: same as inorder | ||
| # Space Complexity: same as inorder | ||
| def preorder |
| # Time Complexity: same as inorder | ||
| # Space Complexity: same as inorder | ||
| def postorder |
| # Time Complexity: O(n) since each node is visited once | ||
| # Space Complexity: O(1) since it doesn't make anything new | ||
| def height |
There was a problem hiding this comment.
The method works, but you have recursion and so some space complexity. Predictions?
No description provided.