Skip to content

Day3 #754

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 8 commits into
base: master
Choose a base branch
from
Open

Day3 #754

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
220 changes: 203 additions & 17 deletions binary_search_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,134 @@
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`
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.
"""
import sys
sys.path.extend(['queue', 'stack', 'binary_search_tree'])

class Node:
def __init__(self, value = None, next_node = None):
self.value = value
self.next_node = next_node

def get_value(self):
return self.value

def get_next(self):
return self.next_node

def set_next(self, new_next):
self.next_node = new_next

class LinkedList:
def __init__(self):
self.head = None
self.tail = None

def add_to_tail(self, value):
new_node = Node(value, None)
if not self.head:
self.head = new_node
self.tail = new_node
else:
self.tail.set_next(new_node)
self.tail = new_node

def remove_head(self):
if not self.head:
head = self.head
self.head = None
self.tail = None
return head.get_value()
value = self.head.get_value()
self.head = self.head.get_next()
return value

def remove_tail(self):
if not self.head:
return None

if self.head is self.tail:
value = self.head.get_value()
self.head = None
self.tail = None
return value

current = self.head

while current.get_next() is not self.tail:
current = current.get_next()

value = self.tail.get_value()
self.tail = current
return value

def contains(self, value):
if not self.head:
return False

current = self.head
while current:
if current.get_value() == value:
return True
current = current.get_next()
return False

def get_max(self):
if not self.head:
return None
max_value = self.head.get_value()
current = self.head.get_next()
while current:
if current.get_value() > max_value:
max_value = current.get_value()
current = current.get_next()
return max_value

class Queue:
def __init__(self):
self.size = 0
self.storage = LinkedList()

def __len__(self):
return self.size

def enqueue(self, value):
self.storage.add_to_tail(value)
self.size += 1


def dequeue(self):
if self.size == 0:
return None
self.size -= 1
return self.storage.remove_head()

class Stack:
def __init__(self):
self.size = 0
self.storage = LinkedList()

def __len__(self):
return self.size

def push(self, value):
self.size += 1
self.storage.add_to_tail(value)

def isEmpty(self):
return self.size == 0

def pop(self):
if self.size == 0:
return None
self.size -= 1
return self.storage.remove_tail()

class BSTNode:
def __init__(self, value):
self.value = value
Expand All @@ -17,48 +139,110 @@ def __init__(self, value):

# Insert the given value into the tree
def insert(self, value):
pass
if value >= self.value:
if self.right:
self.right.insert(value)
else:
self.right = BSTNode(value)

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

# Return True if the tree contains the value
# False if it does not
def contains(self, target):
pass
if target == self.value:
return True
elif target > self.value:
if self.right:
return self.right.contains(target)
else:
return False

else:
if self.left:
return self.left.contains(target)
else:
return False

# Return the maximum value found in the tree
def get_max(self):
pass
current = self
while current.right is not None:
current = current.right
return current.value

# 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
if not self:
return
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
def bft_print(self, node):
queue = Queue()
queue.enqueue(node)
while queue.size > 0:
node = queue.dequeue()
print(node.value)
if node.left:
queue.enqueue(node.left)
if node.right:
queue.enqueue(node.right)

# Print the value of every node, starting with the given node,
# in an iterative depth first traversal
def dft_print(self):
pass
def dft_print(self, node):
stack = Stack()
stack.push(node)
while not stack.isEmpty():
node = stack.pop()
print(node.value)
if node.right:
stack.push(node.right)
if node.left:
stack.push(node.left)

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

# Print Pre-order recursive DFT
def pre_order_dft(self):
pass
if not self:
return
print(self.value)
if self.left:
self.left.pre_order_dft()
if self.right:
self.right.pre_order_dft()

# Print Post-order recursive DFT
def post_order_dft(self):
pass
if not self:
return
if self.left:
self.left.post_order_dft()
if self.right:
self.right.post_order_dft()
print(self.value)

"""
This code is necessary for testing the `print` methods
Expand All @@ -72,14 +256,16 @@ def post_order_dft(self):
bst.insert(3)
bst.insert(4)
bst.insert(2)
print("bst,bft print")
bst.bft_print(bst)

bst.bft_print()
bst.dft_print()
print("bst.dft print")
bst.dft_print(bst)

print("elegant methods")
print("pre order")
bst.pre_order_dft()
print("in order")
bst.in_order_dft()
bst.in_order_print()
print("post order")
bst.post_order_dft()
4 changes: 2 additions & 2 deletions binary_search_tree/test_binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ def test_print_traversals(self):
self.assertEqual(output, "1\n2\n3\n4\n5\n6\n7\n8\n")

sys.stdout = io.StringIO()
self.bst.bft_print()
self.bst.bft_print(self.bst)
output = sys.stdout.getvalue()
self.assertTrue(output == "1\n8\n5\n3\n7\n2\n4\n6\n" or
output == "1\n8\n5\n7\n3\n6\n4\n2\n")

sys.stdout = io.StringIO()
self.bst.dft_print()
self.bst.dft_print(self.bst)
output = sys.stdout.getvalue()
self.assertTrue(output == "1\n8\n5\n7\n6\n3\n4\n2\n" or
output == "1\n8\n5\n3\n2\n4\n7\n6\n")
Expand Down
Loading