-
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 Tree Paper Karla T. #55
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,36 +14,87 @@ class Tree: | |
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n ) | ||
|
||
def add_helper(self, current, key, value): | ||
if current is None: | ||
return TreeNode(key, value) | ||
if current.key >= key: | ||
current.left = self.add_helper(current.left, key, value) | ||
else: | ||
current.right = self.add_helper(current.right, key, value) | ||
return current | ||
|
||
def add(self, key, value = None): | ||
pass | ||
if self.root == None: | ||
self.root = TreeNode(key, value) | ||
else: | ||
self.add_helper(self.root, key, value) | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O( log n ) | ||
# Space Complexity: O (log 1 ) | ||
def find(self, key): | ||
Comment on lines
+36
to
38
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 | ||
current = self.root | ||
while current: | ||
if current.key == key: | ||
return current.value | ||
elif current.key > key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
|
||
# Time Complexity: O( n ) | ||
# Space Complexity: O( n ) | ||
def inorder_helper(self, current_node, listofvalues): | ||
if not current_node: | ||
return listofvalues | ||
obj = {'key': current_node.key, 'value': current_node.value} | ||
listofvalues.append(obj) | ||
self.inorder_helper(current_node.right, listofvalues) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder(self): | ||
pass | ||
listofvalues = [] | ||
return self.inorder_helper(self.root, listofvalues) | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder_helper(self, current_node, listofvalues): | ||
Comment on lines
+61
to
+63
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_node: | ||
return listofvalues | ||
obj = {'key': current_node.key, 'value': current_node.value} | ||
listofvalues.append(obj) | ||
self.preorder_helper(current_node.left, listofvalues) | ||
self.preorder_helper(current_node.right, listofvalues) | ||
return listofvalues | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder(self): | ||
pass | ||
listofvalues = [] | ||
return self.preorder_helper(self.root, listofvalues) | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder_helper(self, current_node, listofvalues): | ||
Comment on lines
+76
to
+78
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_node: | ||
return listofvalues | ||
obj = {'key': current_node.key, 'value':current_node.value} | ||
self.postorder_helper(current_node.left, listofvalues) | ||
self.postorder_helper(current_node.right, listofvalues) | ||
listofvalues.append(obj) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder(self): | ||
pass | ||
listofvalues = [] | ||
return self.postorder_helper(self.root, listofvalues) | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(log n) | ||
def height_helper(self, current_node): | ||
Comment on lines
+89
to
+91
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_node: | ||
return 0 | ||
return max(self.height_helper(current_node.left), self.height_helper(current_node.right)) + 1 | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height(self): | ||
pass | ||
|
||
return self.height_helper(self.root) | ||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
|
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.
👍 Time/space complexity is correct if the tree is balanced. O(n) otherwise.