Conversation
kyra-patton
left a comment
There was a problem hiding this comment.
Hi Alf, it looks like you may have looked at Ivette's code for this project. While I do not mind if you turn to outside resources for help, it is difficult for me to give feedback as to your understanding of I believe it may not be your own work. For that reason, I'm grading this as a red - because I cannot assess how well you understand the problem.
At this point, you do not need to resubmit. It will not affect your ability to graduate.
| self.root = added | ||
|
|
||
| parent = self.root | ||
| while True: |
There was a problem hiding this comment.
We generally try to avoid while loops whose conditions can never be Falsey. With while loops, we always want the body of the while loop to be making progress toward the condition being False.
How might you refactor you code to eliminate this? Hint: Look at your find function
| else: | ||
| return "something went wrong" |
There was a problem hiding this comment.
This isn't necessary
| else: | |
| return "something went wrong" |
| # Space Complexity: | ||
| # Time Complexity: 0(log(n)) | ||
| # Space Complexity: 0(1) | ||
| def find(self, key): |
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def inorder(self): |
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder(self): |
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder(self): |
|
|
||
| # Time Complexity: O(nlog(n)) | ||
| # Space Complexity: 0(m) -> m == # of nodes | ||
| def height(self): |
There was a problem hiding this comment.
✨ This time and space complexity is correct for your solution!
You could refactor your code to achieve an O(n) time and space solution. Instead of having height_helper return a list, have it return an integer. When you make a recursive call on the left/right subtree, you know that your height is 1 + the height of the deepest subtree. So you can say that the height is 1 + max(height of left subtree, height of right subtree).
| pass | ||
| added = TreeNode(key, value) | ||
| if not self.root: | ||
| self.root = added |
There was a problem hiding this comment.
If you do not return after this guard close, you'll enter into an infinite loop
| self.root = added | |
| self.root = added | |
| return added |
No description provided.