-
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
C15 Paper Saejin Tanguay #48
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 |
---|---|---|
|
@@ -8,48 +8,151 @@ def __init__(self, key, val = None): | |
self.left = None | ||
self.right = None | ||
|
||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: On | ||
# Space Complexity: O1 | ||
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) | ||
|
||
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: | ||
# Space Complexity: | ||
# Time Complexity: On | ||
# Space Complexity: O1 | ||
def find(self, key): | ||
Comment on lines
+33
to
35
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 space complexity is O(log n) if the tree is balanced and O(n) otherwise due to the recursive call stack. |
||
pass | ||
if self.root == None: | ||
return None | ||
else: | ||
return self.find_helper(self.root, key) | ||
|
||
def find_helper(self, curr, key): | ||
if not curr: | ||
return None | ||
elif curr.key == key: | ||
return curr.value | ||
elif key < curr.key: | ||
return self.find_helper(curr.left, key) | ||
elif key > curr.key: | ||
return self.find_helper(curr.right, key) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: On | ||
# Space Complexity: On | ||
def inorder(self): | ||
Comment on lines
+51
to
53
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 | ||
all_ele = [] | ||
return self.inorder_helper(self.root, all_ele) | ||
|
||
def inorder_helper(self, curr, all_ele): | ||
if not curr: | ||
return all_ele | ||
self.inorder_helper(curr.left, all_ele) | ||
all_ele.append({ | ||
"key": curr.key, | ||
"value": curr.value}) | ||
self.inorder_helper(curr.right, all_ele) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
return all_ele | ||
|
||
# Time Complexity: On | ||
# Space Complexity: On | ||
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 | ||
all_ele = [] | ||
return self.preorder_helper(self.root, all_ele) | ||
|
||
def preorder_helper(self, curr, all_ele): | ||
if not curr: | ||
return all_ele | ||
|
||
all_ele.append({ | ||
"key": curr.key, | ||
"value": curr.value | ||
}) | ||
self.preorder_helper(curr.left, all_ele) | ||
self.preorder_helper(curr.right, all_ele) | ||
|
||
return all_ele | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: On | ||
# Space Complexity: On | ||
def postorder(self): | ||
Comment on lines
+87
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. 👍 |
||
pass | ||
all_ele = [] | ||
return self.postorder_helper(self.root, all_ele) | ||
|
||
def postorder_helper(self, curr, all_ele): | ||
if not curr: | ||
return all_ele | ||
|
||
self.postorder_helper(curr.left, all_ele) | ||
self.postorder_helper(curr.right, all_ele) | ||
|
||
all_ele.append({ | ||
"key": curr.key, | ||
"value": curr.value | ||
}) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
return all_ele | ||
|
||
# # Time Complexity: On | ||
# # Space Complexity: O1 | ||
def height(self): | ||
Comment on lines
+107
to
109
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 ) for a balanced tree and O(n) for an unbalanced tree. This is due to the recursive call stack. |
||
pass | ||
return self.height_helper(self.root) | ||
|
||
def height_helper(self, curr): | ||
if not curr: | ||
return 0 | ||
|
||
left_height = self.height_helper(curr.left) | ||
right_height = self.height_helper(curr.right) | ||
|
||
return max(left_height, right_height) +1 | ||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
# # Time Complexity: On | ||
# # Space Complexity: On | ||
def bfs(self): | ||
Comment on lines
+122
to
124
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 | ||
|
||
arr = [] | ||
|
||
if not self.root: | ||
return arr | ||
|
||
root = { | ||
"key": self.root.key, | ||
"value": self.root.value} | ||
|
||
temp = [self.root] | ||
arr.append(root) | ||
|
||
while len(temp) > 0: | ||
curr = temp[0] | ||
|
||
if curr.left: | ||
temp.append(curr.left) | ||
arr.append({ | ||
"key": curr.left.key, | ||
"value": curr.left.value}) | ||
|
||
if curr.right: | ||
temp.append(curr.right) | ||
arr.append({ | ||
"key": curr.right.key, | ||
"value": curr.right.value}) | ||
|
||
temp.remove(curr) | ||
|
||
return arr | ||
|
||
|
||
|
||
|
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.
👍 However space complexity is O(log n) if the tree is balanced and O(n) otherwise due to the recursive call stack.