Skip to content
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.DS_Store
node_modules
*.pyc
.vscode/
.vscode/

env/*
151 changes: 121 additions & 30 deletions binary_search_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""
Binary search trees are a data structure that enforce an ordering over
the data they store. That ordering in turn makes it a lot more efficient
at searching for a particular piece of data in the tree.
Binary search trees are a data structure that enforce an ordering over
the data they store. That ordering in turn makes it a lot more efficient
at searching for a particular piece of data in the tree.

This part of the project comprises two days:
1. Implement the methods `insert`, `contains`, `get_max`, and `for_each`
on the BSTNode class.
2. Implement the `in_order_print`, `bft_print`, and `dft_print` methods
on the BSTNode class.
"""
from queue import Queue


class BSTNode:
def __init__(self, value):
self.value = value
Expand All @@ -17,69 +20,157 @@ def __init__(self, value):

# Insert the given value into the tree
def insert(self, value):
pass

if value < self.value:
if self.left is None:
self.left = BSTNode(value)
else:
self.left.insert(value)

if value >= self.value:
if self.right is None:
self.right = BSTNode(value)
else:
self.right.insert(value)

# Return True if the tree contains the value
# False if it does not

def contains(self, target):
pass

if self.value == target:
print(f"Made it with {target}")
return True
elif target < self.value and self.left:
self.left.contains(target)
elif target > self.value and self.right:
self.right.contains(target)

return False

# def contains(self, target):
# # check if the node is == target
# if self.value == target:
# # if true return true
# return True
# # otherwise check if target is < node value
# elif target < self.value:
# # if left is None, target doesn't exist in tree, return false
# if self.left == None:
# return False
# # if left value is = target return true
# elif self.left.value == target:
# return True
# # otherwise move down left, call contains on left node
# else:
# self.left.contains(target)
# # otherwise check if target is >= node value
# elif target > self.value:
# # if right is None, target doesn't exist in tree, return false
# if self.right == None:
# return False
# # if right value is = target return true
# elif self.right.value == target:
# return True
# # otherwise move down right, call contains on right node
# else:
# self.right.contains(target)

# Return the maximum value found in the tree

def get_max(self):
pass
current_max = self.value
current_node = self

while current_node is not None:
current_max = current_node.value
current_node = current_node.right

return current_max

# Call the function `fn` on the value of each node

def for_each(self, fn):
pass
fn(self.value)

if self.left:
self.left.for_each(fn)
if self.right:
self.right.for_each(fn)

# Part 2 -----------------------

# Print all the values in order from low to high
# Hint: Use a recursive, depth first traversal
def in_order_print(self):
pass
# Recursive: place print statement in between recursive calls that explore left and right subtrees

if self.left:
self.left.in_order_print()
print(self.value)
if self.right:
self.right.in_order_print()

# Print the value of every node, starting with the given node,
# in an iterative breadth first traversal

def bft_print(self):
pass
q = Queue()
if self is None:
return
q.put(self)
while q.empty() is not True:
current = q.get()
print(current.value)
if current.left:

q.put(current.left)
if current.right:

q.put(current.right)

# Print the value of every node, starting with the given node,
# in an iterative depth first traversal

def dft_print(self):
pass
stack = []
stack.append(self)

while len(stack) is not 0:
current = stack.pop()
print(current.value)

if current.right:
stack.append(current.right)

if current.left:
stack.append(current.left)

# Stretch Goals -------------------------
# Note: Research may be required

# Print Pre-order recursive DFT

def pre_order_dft(self):
pass

# Print Post-order recursive DFT
def post_order_dft(self):
pass


"""
This code is necessary for testing the `print` methods
"""
bst = BSTNode(1)

bst.insert(8)
bst.insert(5)
bst.insert(7)
bst.insert(6)
bst.insert(3)
bst.insert(4)
bst.insert(2)

bst.bft_print()
bst.dft_print()

print("elegant methods")
print("pre order")
bst.pre_order_dft()
print("in order")
bst.in_order_dft()
print("post order")
bst.post_order_dft()
tree = BSTNode(1)
tree.insert(8)
tree.insert(5)
tree.insert(7)
tree.insert(6)
tree.insert(3)
tree.insert(4)
tree.insert(2)

# tree.in_order_print()
# tree.bft_print()
tree.dft_print()
6 changes: 4 additions & 2 deletions binary_search_tree/test_binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io
from binary_search_tree import BSTNode


class BinarySearchTreeTests(unittest.TestCase):
def setUp(self):
self.bst = BSTNode(5)
Expand All @@ -15,7 +16,7 @@ def test_insert(self):
self.bst.insert(6)
self.assertEqual(self.bst.left.right.value, 3)
self.assertEqual(self.bst.right.left.value, 6)

def test_handle_dupe_insert(self):
self.bst2 = BSTNode(1)
self.bst2.insert(1)
Expand All @@ -38,7 +39,7 @@ def test_get_max(self):

def test_for_each(self):
arr = []
cb = lambda x: arr.append(x)
def cb(x): return arr.append(x)

v1 = random.randint(1, 101)
v2 = random.randint(1, 101)
Expand Down Expand Up @@ -106,5 +107,6 @@ def test_print_traversals(self):

sys.stdout = stdout_ # Restore stdout


if __name__ == '__main__':
unittest.main()
Loading