-
Notifications
You must be signed in to change notification settings - Fork 68
Spruce: Kaitlyn #52
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?
Spruce: Kaitlyn #52
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,127 @@ class Tree: | |
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(n) | ||
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. 🪐 You aren't creating any new data structures here or holding recursive calls in memory, so space complexity would be O(1) |
||
def add(self, key, value = None): | ||
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 | ||
node = TreeNode(key, value) | ||
if self.root is None: | ||
self.root = node | ||
return self | ||
|
||
current = self.root | ||
while current: | ||
if current.key > key: | ||
# Go left | ||
if current.left is None: | ||
current.left = node | ||
return self | ||
else: | ||
current = current.left | ||
else: | ||
if current.right is None: | ||
current.right = node | ||
return self | ||
else: | ||
current = current.right | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(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 | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
if self.root is None: | ||
return None | ||
if self.root.key == key: | ||
return self.root.value | ||
|
||
current = self.root | ||
while current: | ||
if key < current.key: | ||
if not current.left: | ||
return None | ||
elif current.left.key == key: | ||
return current.left.value | ||
else: | ||
current = current.left | ||
else: | ||
if not current.right: | ||
return None | ||
elif current.right.key == key: | ||
return current.right.value | ||
else: | ||
current = current.right | ||
|
||
# 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. ✨ Nice iterative solution |
||
pass | ||
node_list = [] | ||
current = self.root | ||
if current == None: | ||
return node_list | ||
while current: | ||
node_list.insert(0, {"key": current.key, "value": current.value}) | ||
current = current.left | ||
# reset | ||
current = self.root.right | ||
while current: | ||
node_list.append({"key": current.key, "value": current.value}) | ||
current = current.right | ||
return node_list | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# 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 | ||
node_list = [] | ||
current = self.root | ||
if current == None: | ||
return node_list | ||
while current: | ||
node_list.append({"key": current.key, "value": current.value}) | ||
current = current.left | ||
|
||
current = self.root.right | ||
while current: | ||
node_list.append({"key": current.key, "value": current.value}) | ||
current = current.right | ||
return node_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# 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 | ||
node_list = [] | ||
current = self.root | ||
if current == None: | ||
return node_list | ||
self.postorder_helper(current, node_list) | ||
return node_list | ||
|
||
def postorder_helper(self, current, node_list): | ||
if current: | ||
self.postorder_helper(current.left, node_list) | ||
self.postorder_helper(current.right, node_list) | ||
node_list.append({"key": current.key, "value": current.value}) | ||
return node_list | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
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. 🪐 Space complexity is going to be O(n) here because your recursive calls will make a call for every node (every subtree) in the tree. |
||
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. ✨ |
||
pass | ||
current = self.root | ||
|
||
if current is None: | ||
return 0 | ||
|
||
return self.height_helper(current) | ||
|
||
def height_helper(self, current): | ||
if current is None: | ||
return 0 | ||
|
||
left_height = self.height_helper(current.left) | ||
right_height = self.height_helper(current.right) | ||
|
||
return max(left_height, right_height) + 1 | ||
|
||
|
||
# # 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.
✨