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

Rock - Priscille #51

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
109 changes: 86 additions & 23 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,116 @@ def __init__(self, key, val = None):
self.value = val
self.left = None
self.right = None

def dict(self):
return {"key": self.key, "value": self.value}
Comment on lines +11 to +12

Choose a reason for hiding this comment

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

👍 Useful!




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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)
def add(self, key, value = None):
Comment on lines +20 to 22

Choose a reason for hiding this comment

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

👍

pass
tree = TreeNode(key, value)

if self.root == None:
self.root = tree
else:
current = self.root

while current != None:
if key == current.key:
return None
if key < current.key:
if current.left == None:
current.left = tree
current = current.left
elif key > current.key:
if current.right == None:
current.right = tree
current = current.right

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)
def find(self, key):
Comment on lines +42 to 44

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
current = self.root
while current != None:
if key == current.key:
return current.value
elif key > current.key:
current = current.right
else:
current = current.left
return None

def inorder_helper(self, current, traversal_list):
if current == None:
return

self.inorder_helper(current.left, traversal_list)
traversal_list.append(current.dict())
self.inorder_helper(current.right, traversal_list)

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)
def inorder(self):
Comment on lines +65 to 67

Choose a reason for hiding this comment

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

👍, however since you are building a list of all tree node values the space complexity is O(n)

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

def preorder_helper(self, current, traversal_list):
if current == None:
return

# Time Complexity:
# Space Complexity:
traversal_list.append(current.dict())
self.preorder_helper(current.left, traversal_list)
self.preorder_helper(current.right, traversal_list)

# Time Complexity: O(n)
# Space Complexity: O(1)
def preorder(self):
Comment on lines +80 to 82

Choose a reason for hiding this comment

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

👍, however since you are building a list of all tree node values the space complexity is O(n)

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

# Time Complexity:
# Space Complexity:
def postorder_helper(self, current, traversal_list):
if current == None:
return

self.postorder_helper(current.left, traversal_list)
self.postorder_helper(current.right, traversal_list)
traversal_list.append(current.dict())

# Time Complexity: O(n)
# Space Complexity: O(1)
def postorder(self):
Comment on lines +95 to 97

Choose a reason for hiding this comment

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

👍, however since you are building a list of all tree node values the space complexity is O(n)

pass
traversal_list = []
self.postorder_helper(self.root, traversal_list)
return traversal_list

# Time Complexity:
# Space Complexity:
def height_helper(self, current):
if current == None:
return 0
left_height = self.height_helper(current.left)
right_height = self.height_helper(current.right)
return 1 +max(left_height, right_height)

# Time Complexity: O(n)
# Space Complexity: O(1)
def height(self):
Comment on lines +109 to 111

Choose a reason for hiding this comment

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

👍 However the space complexity is O(h) where h is the height of the tree.

pass
return self.height_helper(self.root)


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




# # Useful for printing
# Useful for printing
def to_s(self):
return f"{self.inorder()}"
Binary file modified tests/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
5 changes: 3 additions & 2 deletions tests/test_binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ def test_height_of_many_node_tree(tree_with_nodes):
tree_with_nodes.add(2, "pasta")
tree_with_nodes.add(2.5, "bread")
assert tree_with_nodes.height() == 5


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

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