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

Sarah Jane Olivas - Spruce - C16 #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
150 changes: 122 additions & 28 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,153 @@
class TreeNode:
def __init__(self, key, val = None):
def __init__(self, key, val=None):

"""
key is used to compare values
value can be any kind of object
"""

if val == None:
val = key

self.key = key
self.value = val
self.left = None
self.right = None



class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
def add(self, key, value = None):
pass
# Time Complexity: O(log n) if ordered, O(n) if unordered
# Space Complexity: O(1)
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 work comparing the time complexity of a balanced vs unbalanced tree

node = TreeNode(key, value)
if self.root is None:
self.root = node
return

# Time Complexity:
# Space Complexity:
current = self.root
while current:
if current.key > key:
# go left
if current.left is None:
current.left = node
return
else:
current = current.left
else:
# go right
if current.right is None:
current.right = node
return
else:
current = current.right

# Time Complexity: O(log n) if ordered, O(n) if unordered
# Space Complexity: O(1)
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 is None:
return None

if self.root.key == key:
return self.root.value

# Time Complexity:
# Space Complexity:
current = self.root
while current:
if key < current.key:
if not current.left:
return None
elif current.left.key == key:
return current.left.value
current = current.left
else:
if not current.right:
return None
elif current.right.key == key:
return current.right.value
current = current.right

# 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.

✨Nice iterative solution

pass
node_list = []
current = self.root
if current == None:
return node_list

while current:
node_list.insert(0, {"key": current.key, "value": current.value})
current = current.left

# Time Complexity:
# Space Complexity:
current = self.root.right
while current:
node_list.append({"key": current.key, "value": current.value})
current = current.right

return node_list

# 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
node_list = []
current = self.root
if current == None:
return node_list
while current:
node_list.append({"key": current.key, "value": current.value})
current = current.left

# Time Complexity:
# Space Complexity:
current = self.root.right
while current:
node_list.append({"key": current.key, "value": current.value})
current = current.right
return node_list

# 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
node_list = []
current = self.root
if current == None:
return node_list
self.postorder_helper(current, node_list)
return node_list

def postorder_helper(self, current, node_list):
if current:
self.postorder_helper(current.left, node_list)
self.postorder_helper(current.right, node_list)
node_list.append({"key": current.key, "value": current.value})
return node_list

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

🪐 Space complexity is going to be O(n) because you make a recursive call on each subtree (each node in the tree can be considered a subtree)

def height(self):
pass
current = self.root

if current is None:
return 0

# # Optional Method
# # Time Complexity:
# # Space Complexity:
def bfs(self):
pass
return self.height_helper(current)


def height_helper(self, current):
if current is None:
return 0

left_height = self.height_helper(current.left)
right_height = self.height_helper(current.right)

return max(left_height, right_height) + 1

# ======================================================================================

# # Optional Method
# # Time Complexity:
# # Space Complexity:
def bfs(self):
pass

# # Useful for printing
# # Useful for printing
def to_s(self):
return f"{self.inorder()}"
4 changes: 2 additions & 2 deletions tests/test_binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ def test_will_report_height_of_unbalanced_tree():

assert unbalanced_tree.height() == 5


@pytest.mark.skip(reason="Going Further methods")
def test_bfs_with_empty_tree(empty_tree):
assert empty_tree.bfs() == []


@pytest.mark.skip(reason="Going Further methods")
def test_bfs_with_tree_with_nodes(tree_with_nodes):
expected_answer = [
{
Expand Down