-
Notifications
You must be signed in to change notification settings - Fork 69
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
Alie Ibarra C16 #48
base: master
Are you sure you want to change the base?
Alie Ibarra C16 #48
Conversation
Grabbing this to grade! |
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.
Good work!
I added some comments about a small performance issue with your node addition solution, your depth-first traversal solution complexity calculations, and a minor typo in the height solution. However, this is a working implementation and demonstrates your understanding, so this is good enough for a Green!
current.left = self.add_helper(current.left, key, value) | ||
else: | ||
current.right = self.add_helper(current.right, key, value) | ||
return current |
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.
This implementation of add_helper works, but definitely does a bit of extra work in setting nodes. Essentially, every node on the path to where the new node ends up will have either its left or right node "updated", though most of these will be updated to the exact same node. The only one that gets a meaningful update is whenever the bottom of the tree is reached and gets its left/right node updated to the new node. These updates happen in lines 23 and 25.
This probably will not be too bad of a performance hit, but there is a way that you can do this and just update only the node you need.
But this overall fine enough!
|
||
#----------------------- IN ORDER ---------------------- | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) |
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.
Note that unlike add and find where we only traverse down one branch of the tree, that when we do the depth-first traversals, we still do hit every node in the tree making the time and space complexity both O(n).
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.
This applies to inorder
, preorder
, and postorder
.
#----------------------- HEIGHT ---------------------- | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def height_helper(self, current,): |
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.
Looks like there's an extra comma here.
No description provided.