-
Notifications
You must be signed in to change notification settings - Fork 64
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
Gloria Scissors - Binary Trees #46
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.
Well done Gloria. You hit the learning goals here. Nice work.
# Time Complexity: O(n) because looping through all the nodes | ||
# Space Complexity: O(n) because you need to add a node for each key, value | ||
def add(self, key, value = None): |
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.
👍 The time complexity is O(n log n ) if the tree is balanced and O(n) otherwise.
def create_dict(self, TreeNode): | ||
return { "key": TreeNode.key, | ||
"value": TreeNode.value } |
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.
Useful helper, maybe put this in the TreeNode
class.
# Time Complexity: log(n) because we are examining only half of the nodes | ||
# Space Complexity: O(1) because we are not allocating any memory | ||
# self refers to object the class is making | ||
def find(self, key): |
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.
👍 The time complexity is O(n log n ) if the tree is balanced and O(n) otherwise.
self.inorder_traverse(node.right, node_list) | ||
|
||
|
||
|
||
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.
👍 Time/space complexity?
# Time Complexity: 0(N) | ||
# Space Complexity: Space Complexity: 0(N) depends on input size | ||
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/space complexity?
# Time Complexity: 0(N) | ||
# Space Complexity: 0(N) depends on input size | ||
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.
👍 Time/space complexity?
# Time Complexity: 0(1) constant time | ||
# Space Complexity: 0(1) | ||
def height(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 O(n) for time complexity and O(n) space complexity if the tree is unbalanced and O(log n) if the tree is balanced.
# While que not empty | ||
# Before pop head get info of current node add dict to the final array | ||
# Encue the children insert at the tail | ||
# https://www.geeksforgeeks.org/level-order-tree-traversal/ | ||
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.
👍 Nice work!
No description provided.