Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 89 additions & 4 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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


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:

Choose a reason for hiding this comment

The 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

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

Suggested change
current.left = new_node
current.left = new_node
return

else:
current = current.left
else:
#go right
if current.right is None:
current.right = new_node

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above ⬆️

Suggested change
current.right = new_node
current.right = new_node
return

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

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.



# 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

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:

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

#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

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.

pass

# items = []
# self.inorder_helper(self.root, items)

# return items


# Time Complexity:
# Space Complexity:
def preorder(self):
Expand Down