-
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
Rock- Briyana Haywood #38
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,10 +1,10 @@ | ||||||||||||
class TreeNode: | ||||||||||||
def __init__(self, key, val = None): | ||||||||||||
if val == None: | ||||||||||||
val = key | ||||||||||||
def __init__(self, key, value = None): | ||||||||||||
if value == None: | ||||||||||||
value = key | ||||||||||||
|
||||||||||||
self.key = key | ||||||||||||
self.value = val | ||||||||||||
self.value = value | ||||||||||||
self.left = None | ||||||||||||
self.right = None | ||||||||||||
|
||||||||||||
|
@@ -14,36 +14,113 @@ class Tree: | |||||||||||
def __init__(self): | ||||||||||||
self.root = None | ||||||||||||
|
||||||||||||
# Time Complexity: | ||||||||||||
# Space Complexity: | ||||||||||||
|
||||||||||||
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: O(log n) - balanced | ||||||||||||
# Space Complexity: O(log n) | ||||||||||||
def add(self, key, value = None): | ||||||||||||
Comment on lines
+28
to
30
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. 👍 Good to note the balanced requirement |
||||||||||||
pass | ||||||||||||
if self.root == None: | ||||||||||||
self.root = TreeNode(key, value) | ||||||||||||
else: | ||||||||||||
self.add_helper(self.root, key, value) | ||||||||||||
Comment on lines
+31
to
+34
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. Could be rewritten as:
Suggested change
|
||||||||||||
|
||||||||||||
|
||||||||||||
def find_helper(self, current, key): | ||||||||||||
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. Another good recursive helper |
||||||||||||
if current == None: | ||||||||||||
return None | ||||||||||||
elif current.key == key: | ||||||||||||
return current.value | ||||||||||||
elif key < current.key: | ||||||||||||
return self.find_helper(current.left, key) | ||||||||||||
else: | ||||||||||||
return self.find_helper(current.right, key) | ||||||||||||
|
||||||||||||
|
||||||||||||
# Time Complexity: | ||||||||||||
# Space Complexity: | ||||||||||||
# Time Complexity: O(n) | ||||||||||||
# Space Complexity: O(n) | ||||||||||||
def find(self, key): | ||||||||||||
Comment on lines
+48
to
50
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 | ||||||||||||
return self.find_helper(self.root, key) | ||||||||||||
|
||||||||||||
|
||||||||||||
def inorder_helper(self, current, traversal_list): | ||||||||||||
if current == None: | ||||||||||||
return None | ||||||||||||
self.inorder_helper(current.left, traversal_list) | ||||||||||||
traversal_list.append({ | ||||||||||||
"key": current.key, | ||||||||||||
"value": current.value | ||||||||||||
}) | ||||||||||||
self.inorder_helper(current.right, traversal_list) | ||||||||||||
|
||||||||||||
|
||||||||||||
# Time Complexity: | ||||||||||||
# Space Complexity: | ||||||||||||
# Time Complexity: O(n) | ||||||||||||
# Space Complexity: O(n) | ||||||||||||
def inorder(self): | ||||||||||||
Comment on lines
+65
to
67
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 = [] | ||||||||||||
self.inorder_helper(self.root, traversal_list) | ||||||||||||
return traversal_list | ||||||||||||
|
||||||||||||
|
||||||||||||
def preorder_helper(self, current, traversal_list): | ||||||||||||
if current != None: | ||||||||||||
traversal_list.append({ | ||||||||||||
"key": current.key, | ||||||||||||
"value": current.value | ||||||||||||
}) | ||||||||||||
self.preorder_helper(current.left, traversal_list) | ||||||||||||
self.preorder_helper(current.right, traversal_list) | ||||||||||||
|
||||||||||||
# Time Complexity: | ||||||||||||
# Space Complexity: | ||||||||||||
|
||||||||||||
# Time Complexity: O(n) | ||||||||||||
# Space Complexity: O(n) | ||||||||||||
def preorder(self): | ||||||||||||
Comment on lines
+83
to
85
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 = [] | ||||||||||||
self.preorder_helper(self.root, traversal_list) | ||||||||||||
return traversal_list | ||||||||||||
|
||||||||||||
|
||||||||||||
def postorder_helper(self, current, traversal_list): | ||||||||||||
if current == None: | ||||||||||||
return None | ||||||||||||
self.postorder_helper(current.left, traversal_list) | ||||||||||||
self.postorder_helper(current.right, traversal_list) | ||||||||||||
traversal_list.append({ | ||||||||||||
"key": current.key, | ||||||||||||
"value": current.value | ||||||||||||
}) | ||||||||||||
|
||||||||||||
# Time Complexity: | ||||||||||||
# Space Complexity: | ||||||||||||
|
||||||||||||
# Time Complexity: O(n) | ||||||||||||
# Space Complexity: O(n) | ||||||||||||
def postorder(self): | ||||||||||||
Comment on lines
+102
to
104
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 = [] | ||||||||||||
self.postorder_helper(self.root, traversal_list) | ||||||||||||
return traversal_list | ||||||||||||
|
||||||||||||
|
||||||||||||
def height_helper(self, current): | ||||||||||||
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. nice! |
||||||||||||
if current == None: | ||||||||||||
return 0 | ||||||||||||
else: | ||||||||||||
left = self.height_helper(current.left) | ||||||||||||
right = self.height_helper(current.right) | ||||||||||||
return max(left,right) + 1 | ||||||||||||
|
||||||||||||
# Time Complexity: | ||||||||||||
# Space Complexity: | ||||||||||||
def height(self): | ||||||||||||
pass | ||||||||||||
|
||||||||||||
# Time Complexity: O(n) | ||||||||||||
# Space Complexity: O(n) | ||||||||||||
def height(self): | ||||||||||||
Comment on lines
+119
to
+121
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 the height of the tree (which worst case is |
||||||||||||
current = self.root | ||||||||||||
return self.height_helper(current) | ||||||||||||
|
||||||||||||
# # Optional Method | ||||||||||||
# # Time Complexity: | ||||||||||||
|
@@ -52,8 +129,6 @@ 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.
Nice recursive helper!