-
Notifications
You must be signed in to change notification settings - Fork 68
Asli (Alf) - Spruce - C16 #54
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 | ||||
---|---|---|---|---|---|---|
|
@@ -14,46 +14,136 @@ class Tree: | |||||
def __init__(self): | ||||||
self.root = None | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
# Time Complexity: 0(log(n) | ||||||
# Space Complexity: 0(1) | ||||||
def add(self, key, value = None): | ||||||
pass | ||||||
added = TreeNode(key, value) | ||||||
if not self.root: | ||||||
self.root = added | ||||||
|
||||||
parent = self.root | ||||||
while True: | ||||||
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. We generally try to avoid while loops whose conditions can never be Falsey. With while loops, we always want the body of the while loop to be making progress toward the condition being False. How might you refactor you code to eliminate this? Hint: Look at your |
||||||
if added.key > parent.key: | ||||||
if not parent.right: | ||||||
parent.right = added | ||||||
return added | ||||||
else: | ||||||
parent = parent.right | ||||||
elif added.key <= parent.key: | ||||||
if not parent.left: | ||||||
parent.left = added | ||||||
return added | ||||||
else: | ||||||
parent = parent.left | ||||||
else: | ||||||
return "something went wrong" | ||||||
Comment on lines
+38
to
+39
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. This isn't necessary
Suggested change
|
||||||
|
||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
# Time Complexity: 0(log(n)) | ||||||
# Space Complexity: 0(1) | ||||||
def find(self, 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. ✨ |
||||||
pass | ||||||
current = self.root | ||||||
while current: | ||||||
if key == current.key: | ||||||
return current.value | ||||||
elif key <= current.key: | ||||||
current = current.left | ||||||
elif key > current.key: | ||||||
current = current.right | ||||||
|
||||||
return None | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def inorder_helper(self, current, nodes = None): | ||||||
if nodes is None: | ||||||
nodes = [] | ||||||
if current: | ||||||
self.inorder_helper(current.left, nodes) | ||||||
nodes.append({ | ||||||
"key": current.key, | ||||||
"value": current.value | ||||||
}) | ||||||
self.inorder_helper(current.right, nodes) | ||||||
return nodes | ||||||
|
||||||
# Time Complexity: O(n) | ||||||
# Space Complexity: O(n) | ||||||
def inorder(self): | ||||||
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.inorder_helper(self.root) | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def preorder_helper(self, current, nodes = None): | ||||||
if nodes is None: | ||||||
nodes = [] | ||||||
if current: | ||||||
nodes.append({ | ||||||
"key": current.key, | ||||||
"value": current.value | ||||||
}) | ||||||
self.preorder_helper(current.left, nodes) | ||||||
self.preorder_helper(current.right, nodes) | ||||||
return nodes | ||||||
|
||||||
# Time Complexity: O(n) | ||||||
# Space Complexity: O(n) | ||||||
def preorder(self): | ||||||
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.preorder_helper(self.root) | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def postorder_helper(self, current, nodes = None): | ||||||
if nodes is None: | ||||||
nodes = [] | ||||||
if current: | ||||||
self.postorder_helper(current.left, nodes) | ||||||
self.preorder_helper(current.right, nodes) | ||||||
nodes.append({ | ||||||
"key": current.key, | ||||||
"value": current.value | ||||||
}) | ||||||
return nodes | ||||||
|
||||||
# Time Complexity: O(n) | ||||||
# Space Complexity: O(n) | ||||||
def postorder(self): | ||||||
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.postorder_helper(self.root) | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def height(self): | ||||||
pass | ||||||
def height_helper(self, current, nodes = None): | ||||||
if nodes is None: | ||||||
nodes = [] | ||||||
if current: | ||||||
self.height_helper(current.left, nodes) | ||||||
if current.left is None and current.right is None: | ||||||
nodes.append(current.key) | ||||||
self.height_helper(current.right, nodes) | ||||||
return nodes | ||||||
|
||||||
def get_count(self, key): | ||||||
current = self.root | ||||||
count = 0 | ||||||
while current: | ||||||
count += 1 | ||||||
if key == current.key: | ||||||
return count | ||||||
elif key > current.key: | ||||||
current = current.right | ||||||
elif key < current.key: | ||||||
current = current.left | ||||||
return None | ||||||
|
||||||
# Time Complexity: O(nlog(n)) | ||||||
# Space Complexity: 0(m) -> m == # of nodes | ||||||
def height(self): | ||||||
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. ✨ This time and space complexity is correct for your solution! You could refactor your code to achieve an O(n) time and space solution. Instead of having height_helper return a list, have it return an integer. When you make a recursive call on the left/right subtree, you know that your height is 1 + the height of the deepest subtree. So you can say that the height is 1 + max(height of left subtree, height of right subtree). |
||||||
ends = self.height_helper(self.root) | ||||||
max = 0 | ||||||
for end in ends: | ||||||
height = self.get_count(end) | ||||||
if height > max: | ||||||
max = height | ||||||
return max | ||||||
|
||||||
# # Optional Method | ||||||
# # 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.
If you do not return after this guard close, you'll enter into an infinite loop