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

binary search trees #34

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
138 changes: 113 additions & 25 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,147 @@
class TreeNode:
def __init__(self, key, val = None):
def __init__(self, key, val=None):
if val == None:
val = key

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


def height(self):

Choose a reason for hiding this comment

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

Interesting putting this on the TreeNode class.

if self.right and self.left:
return 1 + max(self.left.height(), self.right.height())
elif self.left:
return 1 + self.left.height()
elif self.right:
return 1 + self.right.height()
else:
return 1


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

# Time Complexity:
# Space Complexity:
def add(self, key, value = None):
pass
# Time Complexity: O(log N)
# Space Complexity: O(1)

def add_helper(self, current_node, key, value):
Comment on lines +26 to +29

Choose a reason for hiding this comment

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

The space complexity is O(log n) if the tree is balanced due to the recursive call stack.

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

def add(self, key, value=None):
if self.root == None:
self.root = TreeNode(key, value)
else:
self.add_helper(self.root, key, value)
# parent = None
# current = self.root
# while current != None:
# parent = current
# if current.key >= key:
# current = current.left
# else:
# current = current.right

# if parent.key > key:
# parent.left = TreeNode(key, value)
# else:
# parent.right = TreeNode(key, value)

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

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.right
else:
current = current.left

return None

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder_helper(self, current, traversal_list):
Comment on lines +75 to +77

Choose a reason for hiding this comment

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

👍

if not current:
return traversal_list

obj = {'key': current.key, 'value': current.value}
self.inorder_helper(current.left, traversal_list)
traversal_list.append(obj)
self.inorder_helper(current.right, traversal_list)
return traversal_list

# Time Complexity:
# Space Complexity:
def inorder(self):
pass
traversal_list = []
return self.inorder_helper(self.root, traversal_list)

# Time Complexity: O(n)
# Space Complexity:O(n)
def preorder_helper(self, current, traversal_list):
Comment on lines +91 to +93

Choose a reason for hiding this comment

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

👍

if not current:
return traversal_list
if current:
obj = {'key': current.key, 'value': current.value}
traversal_list.append(obj)
self.preorder_helper(current.left, traversal_list)
self.preorder_helper(current.right, traversal_list)
return traversal_list

# Time Complexity:
# Space Complexity:
def preorder(self):
pass
traversal_list = []
return self.preorder_helper(self.root, traversal_list)

def postorder_helper(self, current, traversal_list):
if not current:
return traversal_list
if current:
self.postorder_helper(current.left, traversal_list)
self.postorder_helper(current.right, traversal_list)
obj = {'key': current.key, 'value': current.value}
traversal_list.append(obj)
return traversal_list

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

# Time Complexity:
# Space Complexity:
def postorder(self):
Comment on lines +117 to 120

Choose a reason for hiding this comment

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

👍

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

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

# Time Complexity:
# Space Complexity:
def height(self):
pass
if self.root:
return self.root.height()
else:
return 0


# # Optional Method
# # Time Complexity:
# # Space Complexity:
# # Time Complexity:
# # Space Complexity:


def bfs(self):
pass




# # Useful for printing


def to_s(self):
return f"{self.inorder()}"
Binary file modified tests/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
79 changes: 46 additions & 33 deletions tests/test_binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
def empty_tree() -> Tree():
return Tree()


@pytest.fixture()
def tree_with_nodes(empty_tree) -> Tree():
empty_tree.add(5, "Peter")
Expand All @@ -17,14 +18,17 @@ def tree_with_nodes(empty_tree) -> Tree():

return empty_tree


def test_add_and_find(tree_with_nodes):
assert tree_with_nodes.find(5) == "Peter"
assert tree_with_nodes.find(15) == "Ada"
assert tree_with_nodes.find(3) == "Paul"


def test_find_returns_none_for_empty_tree(empty_tree):
assert empty_tree.find(5) == None


def test_find_returns_none_for_values_not_in_tree(tree_with_nodes):
assert tree_with_nodes.find(6) == None

Expand All @@ -33,62 +37,65 @@ 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 = [
{
"key": 1,
"key": 1,
"value": "Mary"
},
},
{
"key": 3,
"key": 3,
"value": "Paul"
},
},
{
"key": 5,
"key": 5,
"value": "Peter"
},
},
{
"key": 10,
"key": 10,
"value": "Karla"
},
},
{
"key": 15,
"key": 15,
"value": "Ada"
},
},
{
"key": 25,
"key": 25,
"value": "Kari"
}
]

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


def test_preorder_on_empty_tree(empty_tree):
assert empty_tree.preorder() == []


def test_preorder_on_tree_with_nodes(tree_with_nodes):
expected_answer = [
{
"key": 5,
"key": 5,
"value": "Peter"
},
},
{
"key": 3,
"key": 3,
"value": "Paul"
},
{
"key": 1,
"value": "Mary"
},
},
{
"key": 10,
"value": "Karla"
},
},
{
"key": 15,
"value": "Ada"
},
},
{
"key": 25,
"value": "Kari"
Expand All @@ -98,27 +105,29 @@ def test_preorder_on_tree_with_nodes(tree_with_nodes):
answer = tree_with_nodes.preorder()
assert answer == expected_answer


def test_postorder_on_empty_tree(empty_tree):
assert empty_tree.postorder() == []


def test_postorder_on_tree_with_nodes(tree_with_nodes):
expected_answer = [
{
"key": 1,
"key": 1,
"value": "Mary"
},
},
{
"key": 3,
"value": "Paul"
},
{
"key": 25,
"key": 25,
"value": "Kari"
},
},
{
"key": 15,
"value": "Ada"
},
},
{
"key": 10,
"value": "Karla"
Expand All @@ -132,44 +141,49 @@ def test_postorder_on_tree_with_nodes(tree_with_nodes):
answer = tree_with_nodes.postorder()
assert answer == expected_answer


def test_height_of_empty_tree_is_zero(empty_tree):
assert empty_tree.height() == 0


def test_height_of_one_node_tree(empty_tree):
empty_tree.add(5, "pasta")
assert empty_tree.height() == 1


def test_height_of_many_node_tree(tree_with_nodes):
assert tree_with_nodes.height() == 4
tree_with_nodes.add(2, "pasta")
tree_with_nodes.add(2.5, "bread")
assert tree_with_nodes.height() == 5



def test_bfs_with_empty_tree(empty_tree):
assert empty_tree.bfs() == []


def test_bfs_with_tree_with_nodes(tree_with_nodes):
expected_answer = [
{
"key": 5,
"key": 5,
"value": "Peter"
},
},
{
"key": 3,
"key": 3,
"value": "Paul"
},
},
{
"key": 10,
"key": 10,
"value": "Karla"
},
},
{
"key": 1,
"key": 1,
"value": "Mary"
},
},
{
"key": 15,
"value": "Ada"
},
},
{
"key": 25,
"value": "Kari"
Expand All @@ -178,4 +192,3 @@ def test_bfs_with_tree_with_nodes(tree_with_nodes):

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