-
Notifications
You must be signed in to change notification settings - Fork 69
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
Alie Ibarra C16 #48
base: master
Are you sure you want to change the base?
Alie Ibarra C16 #48
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,110 @@ class Tree: | |
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
#----------------------- ADD ---------------------- | ||
#from from class video 👇🏼 | ||
def add_helper(self, current, key, value): | ||
if current == None: | ||
return TreeNode(key, value) | ||
elif current.key >= key: | ||
current.left = self.add_helper(current.left, key, value) | ||
else: | ||
current.right = self.add_helper(current.right, key, value) | ||
return current | ||
#from from class video 👆🏼 | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def add(self, key, value = None): | ||
pass | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find(self, key): | ||
pass | ||
if self.root is None: | ||
self.root = TreeNode(key, value) | ||
return | ||
self.add_helper(self.root, key, value) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
#----------------------- FIND ---------------------- | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def find_helper(self, current, key): | ||
if current.key == key: | ||
return current.value | ||
if current.key >= key: | ||
if not current.left: | ||
return None | ||
return self.find_helper(current.left, key) | ||
if not current.right: | ||
return None | ||
return self.find_helper(current.right, key) | ||
|
||
def find(self, key): | ||
if self.root == None: | ||
return None | ||
return self.find_helper(self.root, key) | ||
|
||
#----------------------- IN ORDER ---------------------- | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log 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. Note that unlike add and find where we only traverse down one branch of the tree, that when we do the depth-first traversals, we still do hit every node in the tree making the time and space complexity both 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. This applies to |
||
def inorder_helper(self, current, result): | ||
if current != None: | ||
self.inorder_helper(current.left, result) | ||
result.append({"key":current.key, "value": current.value}) | ||
self.inorder_helper(current.right, result) | ||
return result | ||
|
||
def inorder(self): | ||
pass | ||
result = [] | ||
return self.inorder_helper(self.root, result) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
#----------------------- PRE ORDER ---------------------- | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def preorder_helper(self, current, result): | ||
if current != None: | ||
result.append({"key":current.key, "value": current.value}) | ||
self.preorder_helper(current.left, result) | ||
self.preorder_helper(current.right, result) | ||
return result | ||
|
||
def preorder(self): | ||
pass | ||
result = [] | ||
return self.preorder_helper(self.root, result) | ||
|
||
#----------------------- POST ORDER ---------------------- | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def postorder_helper(self, current, result): | ||
if current != None: | ||
self.postorder_helper(current.left, result) | ||
self.postorder_helper(current.right, result) | ||
result.append({"key":current.key, "value": current.value}) | ||
return result | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder(self): | ||
pass | ||
result = [] | ||
return self.postorder_helper(self.root, result) | ||
|
||
#----------------------- HEIGHT ---------------------- | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
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. Looks like there's an extra comma here. |
||
height = 0 | ||
|
||
if current: | ||
height = max(self.height_helper(current.left), self.height_helper(current.right)) + 1 | ||
return height | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height(self): | ||
pass | ||
|
||
return self.height_helper(self.root) | ||
|
||
#----------------------- OPTIONAL ---------------------- | ||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
def bfs(self): | ||
pass | ||
|
||
return [] | ||
|
||
|
||
|
||
# # Useful for printing | ||
def to_s(self): | ||
return f"{self.inorder()}" | ||
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.
This implementation of add_helper works, but definitely does a bit of extra work in setting nodes. Essentially, every node on the path to where the new node ends up will have either its left or right node "updated", though most of these will be updated to the exact same node. The only one that gets a meaningful update is whenever the bottom of the tree is reached and gets its left/right node updated to the new node. These updates happen in lines 23 and 25.
This probably will not be too bad of a performance hit, but there is a way that you can do this and just update only the node you need.
But this overall fine enough!