-
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
Maria M. - Scissors #44
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,46 +14,109 @@ class Tree: | |
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: if tree is balanced, O(log n), if unbalanced, O(n) | ||
# Space Complexity: O(long n) | ||
def add(self, key, value = None): | ||
pass | ||
if self.root == None: | ||
self.root = TreeNode(key, value) | ||
else: | ||
self.add_helper(self.root, key, value) | ||
|
||
def add_helper(self, current_node, key, value): | ||
if current_node == None: | ||
return TreeNode(key, value) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
if key <= current_node.key: | ||
current_node.left = self.add_helper( | ||
current_node.left, key, value) | ||
else: | ||
current_node.right = self.add_helper( | ||
current_node.right, key, value) | ||
return current_node | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def find(self, key): | ||
Comment on lines
+37
to
39
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. 👍 Space complexity is the same as time complexity. |
||
pass | ||
if self.root == None: | ||
return None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
current = self.root | ||
while current != None: | ||
if current.key == key: | ||
return current.value | ||
elif current.key < key: | ||
current = current.right | ||
else: | ||
current = current.left | ||
return None | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder(self): | ||
Comment on lines
+53
to
55
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 | ||
result = [] | ||
if self.root == None: | ||
return result | ||
return self.inorder_helper(self.root, result) | ||
|
||
def inorder_helper(self, root, result): | ||
if root: | ||
self.inorder_helper(root.left, result) | ||
result.append({'key': root.key, 'value': root.value}) | ||
self.inorder_helper(root.right, result) | ||
return result | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder(self): | ||
Comment on lines
+68
to
70
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 | ||
result = [] | ||
if self.root == None: | ||
return result | ||
return self.preorder_helper(self.root, result) | ||
|
||
def preorder_helper(self, root, result): | ||
if root: | ||
result.append({'key': root.key, 'value': root.value}) | ||
self.preorder_helper(root.left, result) | ||
self.preorder_helper(root.right, result) | ||
return result | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder(self): | ||
Comment on lines
+83
to
85
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 | ||
result = [] | ||
if self.root == None: | ||
return result | ||
return self.postorder_helper(self.root, result) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height(self): | ||
pass | ||
def postorder_helper(self, root, result): | ||
if root: | ||
self.postorder_helper(root.left, result) | ||
self.postorder_helper(root.right, result) | ||
result.append({'key': root.key, 'value': root.value}) | ||
return result | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def height(self): | ||
Comment on lines
+98
to
+100
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. 👍 The space complexity is O( log n) if the tree is balanced and O(n) otherwise. |
||
if self.root == None: | ||
return 0 | ||
return self.height_helper(self.root) | ||
|
||
def height_helper(self, root): | ||
if root == None: | ||
return 0 | ||
|
||
left_height = self.height_helper(root.left) | ||
right_height = self.height_helper(root.right) | ||
tree_height = max(left_height, right_height) + 1 | ||
return tree_height | ||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
def bfs(self): | ||
pass | ||
|
||
|
||
|
||
|
||
# # Useful for printing | ||
def to_s(self): | ||
return f"{self.inorder()}" |
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 is the same as time complexity.