-
Notifications
You must be signed in to change notification settings - Fork 64
Rock - Juliana Tree exercise complete #49
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 |
---|---|---|
|
@@ -7,42 +7,98 @@ def __init__(self, key, val = None): | |
self.value = val | ||
self.left = None | ||
self.right = None | ||
|
||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def add(self, key, value = None): | ||
pass | ||
n_node = TreeNode(key, value) | ||
if not self.root: | ||
self.root = n_node | ||
return None | ||
|
||
current = self.root | ||
|
||
while True: | ||
if current.key > key and current.left is None: | ||
current.left = n_node | ||
return None | ||
elif current.key <= key and current.right is None: | ||
current.right = n_node | ||
return None | ||
elif current.key > key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def find(self, key): | ||
Comment on lines
+37
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. 👍 |
||
pass | ||
current = self.root | ||
while current: | ||
if current.key == key: | ||
return current.value | ||
elif current.key > key: | ||
current = current.left | ||
else: current = current.right | ||
return None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder(self): | ||
Comment on lines
+49
to
51
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 | ||
trees = [] | ||
return self.awesome_helper_inorder(self.root, trees) | ||
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. Glad you have an awesome helper |
||
|
||
def awesome_helper_inorder(self, current, trees): | ||
if not current: | ||
return trees | ||
curr_storage = {"key": current.key, "value": current.value} | ||
|
||
self.awesome_helper_inorder(current.left, trees) | ||
trees.append(curr_storage) | ||
self.awesome_helper_inorder(current.right, trees) | ||
|
||
return trees | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity:O(n) | ||
def preorder(self): | ||
pass | ||
if not self.root: | ||
return [] | ||
left = Tree() | ||
left.root = self.root.left | ||
right = Tree() | ||
right.root = self.root.right | ||
Comment on lines
+71
to
+74
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. Why do you make a new |
||
|
||
return [{"key": self.root.key, "value": self.root.value}] + left.preorder() + right.preorder() | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder(self): | ||
Comment on lines
+78
to
80
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. 👍 With similar issues with preorder |
||
pass | ||
if not self.root: | ||
return [] | ||
|
||
left = Tree() | ||
left.root = self.root.left | ||
right = Tree() | ||
right.root = self.root.right | ||
|
||
return left.postorder() + right.postorder() + [{"key": self.root.key, "value": self.root.value}] | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(n) | ||
def height(self): | ||
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. 👍 The time complexity here is O(n) because you visit each node. |
||
pass | ||
if not self.root: | ||
return 0 | ||
left = Tree() | ||
left.root = self.root.left | ||
right = Tree() | ||
right.root = self.root.right | ||
|
||
return max(left.height(),right.height()) +1 | ||
|
||
|
||
# # Optional Method | ||
|
@@ -51,9 +107,6 @@ def height(self): | |
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.
👍