-
Notifications
You must be signed in to change notification settings - Fork 64
Nataliia Getlin - Rock - C15 #40
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
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 |
---|---|---|
|
@@ -142,3 +142,4 @@ dmypy.json | |
# Cython debug symbols | ||
cython_debug/ | ||
|
||
tests/__pycache__/__init__.cpython-39.pyc |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,163 @@ | ||
class TreeNode: | ||
def __init__(self, key, val = None): | ||
if val == None: | ||
def __init__(self, key, val=None): | ||
if val is None: | ||
val = key | ||
|
||
self.key = key | ||
self.value = val | ||
self.left = None | ||
self.right = None | ||
|
||
|
||
def to_json(self): | ||
return { | ||
"key": self.key, | ||
"value": self.value | ||
} | ||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(self, key, value = None): | ||
pass | ||
# ----------- add ---------- | ||
|
||
# Time Complexity: O(log n) for balanced BST, O(n) for unbalanced | ||
# Space Complexity: O(1) | ||
|
||
def add_helper(self, current, key, value): | ||
Comment on lines
+24
to
+27
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. 👍 , however the space complexity is O(log n) if the tree is balanced. |
||
if current is None: | ||
return TreeNode(key, value) | ||
if current.key >= key: | ||
current.left = self.add_helper(current.left, key, value) | ||
elif current.key < key: | ||
current.right = self.add_helper(current.right, key, value) | ||
return current | ||
|
||
def add(self, key, value=None): | ||
if self.root is None: | ||
self.root = TreeNode(key, value) | ||
else: | ||
self.add_helper(self.root, key, value) | ||
|
||
# ----------- find ---------- | ||
|
||
# Time Complexity: O(log n) for balanced BST, O(n) for unbalanced | ||
# Space Complexity: O(1) | ||
|
||
def find_helper(self, current, key): | ||
Comment on lines
+44
to
+47
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. 👍 However due to the recursive stack, the space complexity is O(log n) for a balanced tree. |
||
if current is None: | ||
return None | ||
if current.key == key: | ||
return current.value | ||
elif current.key >= key: | ||
return self.find_helper(current.left, key) | ||
elif current.key < key: | ||
return self.find_helper(current.right, key) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find(self, key): | ||
pass | ||
return self.find_helper(self.root, key) | ||
|
||
# ----------- in-order ---------- | ||
|
||
# Time Complexity: O(n), because we visit each node once | ||
# Space Complexity: O(depth of the recursion) or O(h), where h is height | ||
# of the tree -> O(n) in the worst case | ||
|
||
def inorder_helper(self, current, result): | ||
Comment on lines
+62
to
+66
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. 👍 |
||
if current: | ||
self.inorder_helper(current.left, result) | ||
result.append(current.to_json()) | ||
self.inorder_helper(current.right, result) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder(self): | ||
pass | ||
if self.root is None: | ||
return list() | ||
|
||
result = list() | ||
self.inorder_helper(self.root, result) | ||
return result | ||
|
||
# ----------- pre-order ---------- | ||
|
||
# Time Complexity: ^ | ||
# Space Complexity: ^ | ||
|
||
def preorder_helper(self, current, result): | ||
if current: | ||
result.append(current.to_json()) | ||
self.preorder_helper(current.left, result) | ||
self.preorder_helper(current.right, result) | ||
Comment on lines
+82
to
+89
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 Complexity: | ||
# Space Complexity: | ||
def preorder(self): | ||
pass | ||
if self.root is None: | ||
return list() | ||
|
||
result = list() | ||
self.preorder_helper(self.root, result) | ||
return result | ||
|
||
# ----------- post-order ---------- | ||
# Time Complexity: ^ | ||
# Space Complexity: ^ | ||
|
||
def postorder_helper(self, current, result): | ||
Comment on lines
+99
to
+103
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/space |
||
if current: | ||
self.postorder_helper(current.left, result) | ||
self.postorder_helper(current.right, result) | ||
result.append(current.to_json()) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder(self): | ||
pass | ||
if self.root is None: | ||
return list() | ||
|
||
result = list() | ||
self.postorder_helper(self.root, result) | ||
return result | ||
|
||
# ----------- height ---------- | ||
|
||
# Time Complexity: O(log n) for balanced, O(n) for unbalanced | ||
# Space Complexity: O(n) | ||
|
||
def height_helper(self, current): | ||
Comment on lines
+119
to
+122
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. 👍 |
||
if current is None: | ||
return 0 | ||
else: | ||
return 1 + max(self.height_helper(current.left), | ||
self.height_helper(current.right)) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height(self): | ||
pass | ||
if self.root is None: | ||
return 0 | ||
else: | ||
return self.height_helper(self.root) | ||
|
||
|
||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
# # Time Complexity: O(n) | ||
# # Space Complexity:O(n) | ||
|
||
def bfs(self): | ||
Comment on lines
+138
to
141
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 = list() | ||
|
||
|
||
if self.root is None: | ||
return result | ||
|
||
queue = list() | ||
queue.append(self.root) | ||
|
||
while len(queue) > 0: | ||
current = queue.pop(0) | ||
if current.left: | ||
queue.append(current.left) | ||
if current.right: | ||
queue.append(current.right) | ||
result.append(current.to_json()) | ||
|
||
return result | ||
|
||
# # 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.
Whoops