-
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
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,21 +14,106 @@ class Tree: | |||||||
def __init__(self): | ||||||||
self.root = None | ||||||||
|
||||||||
# 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) | ||||||||
|
||||||||
def add_helper(self, current_root, new_node): | ||||||||
#we can treat each sub-tree like it's a new tree | ||||||||
if current_root is None: | ||||||||
return new_node | ||||||||
|
||||||||
if new_node.key < current_root.key: | ||||||||
current_root.left = self.add_helper(current_root.left, new_node) | ||||||||
else: | ||||||||
current_root.right = self.add_helper(current_root.right, new_node) | ||||||||
|
||||||||
return current_root | ||||||||
|
||||||||
|
||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. ✨ Nice |
||||||||
self.root = new_node | ||||||||
return self.root | ||||||||
|
||||||||
current = self.root | ||||||||
while current: | ||||||||
if current.key > key: | ||||||||
#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 commentThe 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
Suggested change
|
||||||||
else: | ||||||||
current = current.left | ||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Same as above ⬆️
Suggested change
|
||||||||
else: | ||||||||
current = current.right | ||||||||
#Since there is a root, | ||||||||
#Checking whether the new value is bigger than the root | ||||||||
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 | ||||||||
Comment on lines
+56
to
+62
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 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 |
||||||||
|
||||||||
|
||||||||
# Time Complexity: | ||||||||
# Space Complexity: | ||||||||
def find(self, key): | ||||||||
pass | ||||||||
#check if there's anything in the tree | ||||||||
if self.root == None: | ||||||||
return None | ||||||||
|
||||||||
if self.root.key == key: | ||||||||
return self.root.value | ||||||||
Comment on lines
+69
to
+73
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. 👍🏻 Good start! |
||||||||
|
||||||||
current = self.root | ||||||||
while current: | ||||||||
if current.key > 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. 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 It follows a similar pattern to |
||||||||
#go left | ||||||||
if current.left is None: | ||||||||
#we are at the bottom.. nothing else here | ||||||||
print("Not sure what to do here") | ||||||||
else: | ||||||||
if current.right is None: | ||||||||
break | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
# Time Complexity: | ||||||||
# Space Complexity: | ||||||||
|
||||||||
#lets you take the tree and get back an array w all the elements sorted in order | ||||||||
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): | ||||||||
Comment on lines
+95
to
108
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 are pretty close. You just want to initialize The only other thing you need is to add a return statement so |
||||||||
pass | ||||||||
|
||||||||
# items = [] | ||||||||
# self.inorder_helper(self.root, items) | ||||||||
|
||||||||
# return items | ||||||||
|
||||||||
|
||||||||
# Time Complexity: | ||||||||
# Space Complexity: | ||||||||
def preorder(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.
🪐 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