-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,44 +14,139 @@ class Tree: | |
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def add(self, key, value = None): | ||
pass | ||
if self.root == None: | ||
self.root = TreeNode(key, value) | ||
return | ||
current = self.root | ||
while current != None: | ||
if current.key == key: | ||
current.value = value | ||
return | ||
elif current.key > key: | ||
if current.left == None: | ||
current.left = TreeNode(key, value) | ||
return | ||
current = current.left | ||
else: | ||
if current.right == None: | ||
current.right = TreeNode(key, value) | ||
return | ||
current = current.right | ||
|
||
|
||
# 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 commentThe reason will be displayed to describe this comment to others. Learn more. 🪐⬆️ Same thing as |
||
def find(self, key): | ||
pass | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
if self.root == None: | ||
return None | ||
|
||
current = self.root | ||
while current != None: | ||
if current.key == key: | ||
return current.value | ||
elif current.key > key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
return None | ||
|
||
def inorder_helper(self, current_node, items): | ||
if current_node is None: | ||
return [] | ||
|
||
self.inorder_helper(current_node.left, items) | ||
items.append({"key": current_node.key, "value": current_node.value}) | ||
self.inorder_helper(current_node.right, items) | ||
|
||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
pass | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
if self.root == None: | ||
return [] | ||
|
||
items = [] | ||
self.inorder_helper(self.root, items) | ||
return items | ||
|
||
def preorder_helper(self, current_node, items): | ||
if current_node is None: | ||
return [] | ||
|
||
items.append({"key": current_node.key, "value": current_node.value}) | ||
self.preorder_helper(current_node.left, items) | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
pass | ||
if self.root == None: | ||
return [] | ||
|
||
items = [] | ||
self.preorder_helper(self.root, items) | ||
return items | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(self, current_node, items): | ||
if current_node is None: | ||
return [] | ||
|
||
self.postorder_helper(current_node.left, items) | ||
self.postorder_helper(current_node.right, items) | ||
items.append({"key": current_node.key, "value": current_node.value}) | ||
|
||
|
||
# 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 commentThe reason will be displayed to describe this comment to others. Learn more. ⏱🪐 This is going to be O(n) space complexity like |
||
pass | ||
if self.root == None: | ||
return [] | ||
|
||
items = [] | ||
self.postorder_helper(self.root, items) | ||
return items | ||
|
||
|
||
def height_helper(self, current_node): | ||
if current_node is None: | ||
return 0 | ||
left_height = self.height_helper(current_node.left) | ||
right_height = self.height_helper(current_node.right) | ||
return max(left_height, right_height) + 1 | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
Comment on lines
+123
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
def height(self): | ||
pass | ||
if self.root == None: | ||
return 0 | ||
|
||
return self.height_helper(self.root) | ||
|
||
|
||
def bfs_helper(self, current_node, items): | ||
if current_node is None: | ||
return [] | ||
|
||
items.append({"key": current_node.key, "value": current_node.value}) | ||
self.bfs_helper(current_node.left, items) | ||
self.bfs_helper(current_node.right, items) | ||
|
||
# # Optional Method | ||
# # 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 commentThe 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 (Hint: this may be easier to think about iteratively) |
||
pass | ||
if self.root == None: | ||
return [] | ||
|
||
|
||
items = [] | ||
self.bfs_helper(self.root, items) | ||
return items | ||
|
||
|
||
# # Useful for printing | ||
|
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)