-
Notifications
You must be signed in to change notification settings - Fork 68
binary_search_tree #45
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 |
---|---|---|
|
@@ -9,51 +9,119 @@ def __init__(self, key, val = None): | |
self.right = None | ||
|
||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add_helper_function(self, current, next): | ||
if next.key < current.key: | ||
if not current.left: | ||
current.left = next | ||
return | ||
self.add_helper_function(current.left, next) | ||
else: | ||
if not current.right: | ||
current.right = next | ||
return | ||
self.add_helper_function(current.right, next) | ||
|
||
def add(self, key, value = None): | ||
pass | ||
if not self.root: | ||
self.root = TreeNode(key, value) | ||
else: | ||
new_node = TreeNode(key, value) | ||
self.add_helper_function(self.root, new_node) | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def find(self, key): | ||
pass | ||
current = self.root | ||
|
||
while current: | ||
if current.key == key: | ||
return current.value | ||
elif key < current.key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
|
||
return None | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
|
||
|
||
def inorder_helper_function(self, current, node_values): | ||
if not current: | ||
return node_values | ||
|
||
self.inorder_helper_function(current.left, node_values) | ||
node_values.append({ | ||
"key": current.key, | ||
"value": current.value | ||
}) | ||
self.inorder_helper_function(current.right, node_values) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
return node_values | ||
|
||
def inorder(self): | ||
pass | ||
values = [] | ||
return self.inorder_helper_function(self.root, values) | ||
|
||
# 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. Note that unlike This is the case for |
||
|
||
|
||
def preorder_helper_function(self, current, node_values): | ||
if not current: | ||
return node_values | ||
|
||
node_values.append({ | ||
"key": current.key, | ||
"value": current.value | ||
}) | ||
self.preorder_helper_function(current.left, node_values) | ||
self.preorder_helper_function(current.right, node_values) | ||
|
||
return node_values | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder(self): | ||
pass | ||
values = [] | ||
return self.preorder_helper_function(self.root, values) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder(self): | ||
pass | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height(self): | ||
pass | ||
|
||
def postorder_helper_function(self, current, node_values): | ||
if not current: | ||
return node_values | ||
|
||
self.postorder_helper_function(current.left, node_values) | ||
self.postorder_helper_function(current.right, node_values) | ||
node_values.append({ | ||
"key": current.key, | ||
"value": current.value | ||
}) | ||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
def bfs(self): | ||
pass | ||
return node_values | ||
|
||
def postorder(self): | ||
values = [] | ||
return self.postorder_helper_function(self.root, values) | ||
|
||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
|
||
|
||
def height_helper(self, current): | ||
if not current: | ||
return 0 | ||
|
||
# # Useful for printing | ||
def to_s(self): | ||
return f"{self.inorder()}" | ||
return max(self.height_helper(current.left), self.height_helper(current.right)) + 1 | ||
|
||
def height(self): | ||
return self.height_helper(self.root) | ||
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. It doesn't seem you added a time or space complexity calculation for your implementation of |
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.
Good news! Since you did an iterative implementation of
find
, you don't take the hit of the extra stack frames you get from recursion, so your space complexity here is actually O(1), since no new memory is allocated.