Skip to content

C16 - Spruce - Vange #51

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 4 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
93 changes: 83 additions & 10 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,110 @@ class Tree:
def __init__(self):
self.root = None

def add_helper(self, current_node, key, value ):
if current_node == None:
return TreeNode(key, value)
if key <= current_node.key:
current_node.left = self.add_helper(current_node.left, key, value)
else:
current_node.right = self.add_helper(current_node.right, key, value)
return current_node

# Time Complexity:
# Space Complexity:
Comment on lines 26 to 27

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input?

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.

👍

pass
if self.root == None:
self.root = TreeNode(key, value)
else:
self.add_helper(self.root, key, value)

# Time Complexity:
# Space Complexity:
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.

What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input?

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

# Time Complexity:
# Space Complexity:
# In-Order: Left, Current, Right
def inorder_helper(self, root, inorder_list):
if root:
# Traverse left
self.inorder_helper(root.left, inorder_list)
# Traverse root
# print(str(root.val) + "->", end='')
inorder_list.append({"key" : root.key, "value" : root.value})
# Traverse right
self.inorder_helper(root.right, inorder_list)
return inorder_list

# # Time Complexity:
# # Space Complexity:
Comment on lines +61 to +62

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input?

def inorder(self):

Choose a reason for hiding this comment

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

👍

pass
inorder_list = []
if self.root == None:
return inorder_list
else:
return self.inorder_helper(self.root, inorder_list)

def preorder_helper(self, root, preorder_list):
if root:
preorder_list.append({"key" : root.key, "value" : root.value})
self.preorder_helper(root.left, preorder_list)
self.preorder_helper(root.right, preorder_list)
return preorder_list

# Time Complexity:
# Space Complexity:
Comment on lines 77 to 78

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input?

def preorder(self):
pass
preorder_list = []
if self.root == None:
return preorder_list
else:
return self.preorder_helper(self.root, preorder_list)

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

# Time Complexity:
# Space Complexity:
Comment on lines 93 to 94

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input?

def postorder(self):
pass
postorder_list = []
if self.root == None:
return postorder_list
else:
return self.postorder_helper(self.root, postorder_list)


def height_helper(self, root):
if root == None:
return 0
lefth= self.height_helper(root.left)
righth = self.height_helper(root.right)

if lefth > righth:
return lefth + 1
else:
return righth + 1

# Time Complexity:
# Space Complexity:
Comment on lines 114 to 115

Choose a reason for hiding this comment

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

What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input?

def height(self):

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 0
else:
return self.height_helper(self.root)


# # Optional Method
Expand All @@ -51,8 +126,6 @@ def height(self):
def bfs(self):
pass




# # Useful for printing
def to_s(self):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
from binary_search_tree.tree import Tree


@pytest.fixture()
def empty_tree() -> Tree():
return Tree()
Expand All @@ -25,6 +24,7 @@ def test_find_returns_none_for_empty_tree(empty_tree):

def test_can_find_single_root_node(empty_tree):
empty_tree.add(25, "Kari")

assert empty_tree.find(25) == "Kari"


Expand All @@ -49,7 +49,6 @@ def test_inorder_with_empty_tree(empty_tree):
answer = empty_tree.inorder()
assert empty_tree.inorder() == []


def test_inorder_with_nodes(tree_with_nodes):
expected_answer = [
{
Expand Down Expand Up @@ -236,4 +235,4 @@ def test_bfs_with_tree_with_nodes(tree_with_nodes):
]

answer = tree_with_nodes.bfs()
assert answer == expected_answer
assert answer == expected_answer