-
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
binary search trees #34
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 |
---|---|---|
@@ -1,59 +1,147 @@ | ||
class TreeNode: | ||
def __init__(self, key, val = None): | ||
def __init__(self, key, val=None): | ||
if val == None: | ||
val = key | ||
|
||
self.key = key | ||
self.value = val | ||
self.left = None | ||
self.right = None | ||
|
||
|
||
def height(self): | ||
if self.right and self.left: | ||
return 1 + max(self.left.height(), self.right.height()) | ||
elif self.left: | ||
return 1 + self.left.height() | ||
elif self.right: | ||
return 1 + self.right.height() | ||
else: | ||
return 1 | ||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(self, key, value = None): | ||
pass | ||
# Time Complexity: O(log N) | ||
# Space Complexity: O(1) | ||
|
||
def add_helper(self, current_node, key, value): | ||
Comment on lines
+26
to
+29
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 due to the recursive call stack. |
||
if current_node == None: | ||
return TreeNode(key, value) | ||
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 | ||
|
||
def add(self, key, value=None): | ||
if self.root == None: | ||
self.root = TreeNode(key, value) | ||
else: | ||
self.add_helper(self.root, key, value) | ||
# parent = None | ||
# current = self.root | ||
# while current != None: | ||
# parent = current | ||
# if current.key >= key: | ||
# current = current.left | ||
# else: | ||
# current = current.right | ||
|
||
# if parent.key > key: | ||
# parent.left = TreeNode(key, value) | ||
# else: | ||
# parent.right = TreeNode(key, value) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def find(self, key): | ||
Comment on lines
+58
to
60
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 None | ||
|
||
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_helper(self, current, traversal_list): | ||
Comment on lines
+75
to
+77
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 not current: | ||
return traversal_list | ||
|
||
obj = {'key': current.key, 'value': current.value} | ||
self.inorder_helper(current.left, traversal_list) | ||
traversal_list.append(obj) | ||
self.inorder_helper(current.right, traversal_list) | ||
return traversal_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder(self): | ||
pass | ||
traversal_list = [] | ||
return self.inorder_helper(self.root, traversal_list) | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity:O(n) | ||
def preorder_helper(self, current, traversal_list): | ||
Comment on lines
+91
to
+93
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 not current: | ||
return traversal_list | ||
if current: | ||
obj = {'key': current.key, 'value': current.value} | ||
traversal_list.append(obj) | ||
self.preorder_helper(current.left, traversal_list) | ||
self.preorder_helper(current.right, traversal_list) | ||
return traversal_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder(self): | ||
pass | ||
traversal_list = [] | ||
return self.preorder_helper(self.root, traversal_list) | ||
|
||
def postorder_helper(self, current, traversal_list): | ||
if not current: | ||
return traversal_list | ||
if current: | ||
self.postorder_helper(current.left, traversal_list) | ||
self.postorder_helper(current.right, traversal_list) | ||
obj = {'key': current.key, 'value': current.value} | ||
traversal_list.append(obj) | ||
return traversal_list | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder(self): | ||
Comment on lines
+117
to
120
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 | ||
traversal_list = [] | ||
return self.postorder_helper(self.root, traversal_list) | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height(self): | ||
pass | ||
if self.root: | ||
return self.root.height() | ||
else: | ||
return 0 | ||
|
||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
# # 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.
Interesting putting this on the
TreeNode
class.