-
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
Spruce - Angela Fan #56
base: master
Are you sure you want to change the base?
Conversation
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.
✨🌟 Nice work, Angela. I left some comments on time and space complexity as well as how you might rework your BFS implementation. Let me know what questions you have.
🟢
# Time Complexity: | ||
# Space Complexity: | ||
# 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.
🪐 Space complexity will be O(1) here because you aren't creating a call stack or any new data structures that vary with the size of the input (the size of your tree)
# Time Complexity: | ||
# Space Complexity: | ||
# 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.
🪐⬆️ Same thing as add
with the space complexity here
|
||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder(self): |
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.
✨
self.preorder_helper(current_node.right, items) | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def preorder(self): |
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.
✨
|
||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def postorder(self): |
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 is going to be O(n) space complexity like preorder
and inorder
because you are creating a list of all n nodes in the tree and to do that you are creating a recursive call for each of the n nodes.
# 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.
⏱🪐 Time and space complexity is going to be O(log n) because you recursively call height_helper
on each node in the list.
# # Time Complexity: | ||
# # Space Complexity: | ||
# # Time Complexity: O(n) | ||
# # Space Complexity: O(n) | ||
def bfs(self): |
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.
With breadth first search you want to traverse the tree starting from its root, then visit each of it's children (from left to right), before visiting the children's children and so forth. In other words, you want to traverse the tree level by level.
With your bfs_helper
, you are adding the root node of the current tree, but then your recursive call through the left subtree will finish before you ever look at the root of the right subtree.
(Hint: this may be easier to think about iteratively)
No description provided.