Skip to content

Ruiz #59

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Ruiz #59

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
128 changes: 104 additions & 24 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,124 @@ def __init__(self):

# Time Complexity:
# Space Complexity:
def add(self, key, value = None):
pass
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
# Time Complexity: O(log n)
# Space Complexity: O(log n)

# Time Complexity:
# Space Complexity:
def add(self, key, value=None):

Choose a reason for hiding this comment

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

✨ Nice recursive solution

if self.root == None:
self.root = TreeNode(key, value)
else:
self.add_helper(self.root, key, value)
def find_helper(self, current, key):
Comment on lines +34 to +35

Choose a reason for hiding this comment

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

🤓 Style note: We like to add a blank line between the end of one function and the beginning of another

if current.key == key:
return current.value
if current.key >= key:
if not current.left:
return None
else:
return self.find_helper(current.left, key)
else:
if not current.right:
return None
else:
return self.find_helper(current.right, key)
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def find(self, key):

Choose a reason for hiding this comment

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

pass
if self.root == None:
return None
else:
return self.find_helper(self.root, key)
def inorder_helper(self, current, result):
if current:

self.inorder_helper(current.left, result)

result.append({"key": current.key, "value": current.value})

self.inorder_helper(current.right, result)

# Time Complexity:
# Space Complexity:
return result

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder(self):

Choose a reason for hiding this comment

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

pass
tree = []
return self.inorder_helper(self.root, tree)

# Time Complexity:
# Space Complexity:
def preorder_helper(self, current, tree):
if current:

tree.append({"key": current.key, "value": current.value})

self.preorder_helper(current.left, tree)

self.preorder_helper(current.right, tree)
return tree

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(self):

Choose a reason for hiding this comment

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

pass
tree = []
return self.preorder_helper(self.root, tree)

# Time Complexity:
# Space Complexity:
def postorder_helper(self, current, tree):
if current:

self.postorder_helper(current.left, tree)

self.postorder_helper(current.right, tree)

tree.append({"key": current.key, "value": current.value})

return tree

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(self):

Choose a reason for hiding this comment

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

pass
tree = []
return self.postorder_helper(self.root, tree)
def height_counter(self, current):
if not current:
return 0

# Time Complexity:
# Space Complexity:
return(max(self.height_counter(current.left), self.height_counter(current.right))) + 1

# Time Complexity: O(log n)
# Space Complexity: O(log n)
def height(self):

Choose a reason for hiding this comment

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

pass
return self.height_counter(self.root)


# # Optional Method
# # Time Complexity:
# # Space Complexity:
Comment on lines -49 to -50

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?


def bfs(self):

Choose a reason for hiding this comment

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

✨😎 Really nice work!

pass
values = []
queue = []


if self.root:
queue.append(self.root)

while len(queue) > 0:
current = queue.pop(0)
if current.left:
queue.append(current.left)
if current.right:
queue.append(current.right)

values.append({
"key": current.key,
"value": current.value,
})

return values

# # Useful for printing
def to_s(self):
return f"{self.inorder()}"
return f"{self.inorder()}"