Skip to content

Spruce - Michelle Morales #47

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



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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(logn)
# Space Complexity: O(1)
def add(self, key, value = None):
pass
if self.root == None:
self.root = TreeNode(key, value)
return

current = self.root

# Time Complexity:
# Space Complexity:
while current != None:
if key < current.key:
if current.left == None:
current.left = TreeNode(key, value)
return
else:
current = current.left
else:
if current.right == None:
current.right = TreeNode(key, value)
return
else:
current = current.right

# Time Complexity: O(LogN)
# Space Complexity: O(1)
def find(self, key):
pass
current = self.root

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

return None

# This method returns an array of all the elements in the tree, in order.
# Time Complexity: O(N)
# Space Complexity: O(N)
def inorder(self):
pass
"""
if this is null
return
inOrder(left)
visit this
inOrder(right)
"""
current = self.root
result = []

# Time Complexity:
# Space Complexity:
return self.inorder_helper(current, result)

def inorder_helper(self, current, result):
if current is None:
return result

current.left = self.inorder_helper(current.left, result)
result.append({"key": current.key, "value": current.value})
current.right = self.inorder_helper(current.right, result)
return result

# This method returns an array of all the elements in a preorder fashion (root, left, right)
# Time Complexity: O(N)
# Space Complexity: O(N)
def preorder(self):
pass
current = self.root
result = []

return self.preorder_helper(current, result)

def preorder_helper(self, current, result):
if current is None:
return result

result.append({"key": current.key, "value": current.value})
current.left = self.preorder_helper(current.left, result)
current.right = self.preorder_helper(current.right, result)

# Time Complexity:
# Space Complexity:
return result

# This method returns an array of all the elements in a postorder fashion (left, right , root).
# Time Complexity: O(N)
# Space Complexity: O(N)
def postorder(self):
pass
current = self.root
result = []

return self.postorder_helper(current, result)

def postorder_helper(self, current, result):
if current is None:
return result

# Time Complexity:
# Space Complexity:
current.left = self.postorder_helper(current.left, result)
current.right = self.postorder_helper(current.right, result)
result.append({"key": current.key, "value": current.value})

return result

# This method returns the height of the binary search tree
# Time Complexity: O(N)
# Space Complexity: O(N)
def height(self):
pass
height = 0
current = self.root

if current is None:
return height

return self.height_helper(current, height)

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

leftAns = self.height_helper(current.left, height)
rightAns = self.height_helper(current.right, height)

return max(leftAns, rightAns) + 1
# # Recursively call height of each node
# leftAns = height(self.root.left)
# rightAns = height(self.root.right)

# # Return max(leftHeight, rightHeight) at each iteration
# return max(leftAns, rightAns) + 1


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




# # Useful for printing
def to_s(self):
return f"{self.inorder()}"
5 changes: 3 additions & 2 deletions tests/test_binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from unittest import skip
import pytest
from binary_search_tree.tree import Tree

Expand Down Expand Up @@ -202,11 +203,11 @@ def test_will_report_height_of_unbalanced_tree():

assert unbalanced_tree.height() == 5


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


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