-
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
Reid C16 #61
base: master
Are you sure you want to change the base?
Reid C16 #61
Conversation
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.
🌸 Hi Reid, it looks like you got started but weren't able to fully finish. I appreciate you turning in what you have. If you have time to go back and attempt this again, I would encourage you to do so.
I left some comments to hopefully set you on the right track. You have a good start!
🔴
def add(self, key, value = None): | ||
pass | ||
new_node = TreeNode(key, value) #creating the node we are going to add | ||
if self.root == None: |
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
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) if the tree is balanced | ||
# Space Complexity: O(1) - we are just creating one object (the new node) |
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.
🪐 The space complexity here will actually be O(log n) as well if you implement this recursively s iy looks like you begin to in add_helper
because of the recursive call stack, but you are correct it will be O(1) for the iterative solution you implemented
#go left | ||
if current.left is None: #I am at the bottom | ||
#so add the node | ||
current.left = new_node |
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.
Otherwise current will never be reassigned to the next subtree and you'll be stuck in an infinite loop
current.left = new_node | |
current.left = new_node | |
return |
else: | ||
#go right | ||
if current.right is None: | ||
current.right = new_node |
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.
Same as above ⬆️
current.right = new_node | |
current.right = new_node | |
return |
else: | ||
if value < self.root.value: | ||
self.root.left = self.add(self.root.left, value) | ||
else: | ||
self.root.right = self.add(self.root.right, value) | ||
|
||
return self.root |
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 looks like it's left over from when you were trying to implement the function recursively. The above while loop should be enough to complete add
iteratively.
if self.root == None: | ||
return None | ||
|
||
if self.root.key == key: | ||
return self.root.value |
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.
👍🏻 Good start!
|
||
current = self.root | ||
while current: | ||
if current.key > key: |
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.
You're on the right track here. Since it's a binary search tree, if you the key of the current node we are looking at is greater than the key we are searching for, we want to search the left subtree. Otherwise, we want to search the right subtree.
Each time we iterate through our while loop again and are iterating over a new node in the tree, we want to check if the current node's key is the key we are looking for. If it is, you can return that node's value.
Otherwise, keep using the key to check if you want to go down the left or right subtree. If you reach a leaf and still haven't found a match, you can return None
because you know there is no node in the tree with that key.
It follows a similar pattern to add
above. It just has a twist
def inorder_helper(self, current_node, items): | ||
|
||
if current_node is not None: | ||
self.inorder_helper(current_node.left, items) | ||
items.append({"key": current_node.key, "value": current_node.value}) | ||
self.inorder(current_node.right, items) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
def inorder(self): |
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.
You are pretty close. You just want to initialize items
in inorder
and start the first call to inorder_helper
(which node do you want to start your traversal from?)
The only other thing you need is to add a return statement so inorder
is giving something back.
No description provided.