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
104 changes: 89 additions & 15 deletions binary_search_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
2. Implement the `in_order_print`, `bft_print`, and `dft_print` methods
on the BSTNode class.
"""

import sys
sys.path.append('../queue')
from dll_queue import dll_queue
sys.path.append('../stack')
from dll_stack import dll_Stack

class BSTNode:
def __init__(self, value):
self.value = value
Expand All @@ -17,37 +24,104 @@ def __init__(self, value):

# Insert the given value into the tree
def insert(self, value):
pass
# if root is not empty and value is greater than root, place value to right of root
if (value >= self.value) and (self.right != None):
self.right.insert(value)
# if root is empty and value is greater than root, create a node and place value to right of root
if (value >= self.value) and (self.right == None):
new_node = BSTNode(value)
self.right = new_node
# if root is not empty and value is lesser than root, place value to right of root
if (value < self.value) and (self.left != None):
self.left.insert(value)
# if root is empty and value is lesser than root, create a node and place value to left of root
if (value < self.value) and (self.left == None):
new_node = BSTNode(value)
self.left = new_node

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

# Return the maximum value found in the tree
def get_max(self):
pass
curr_node = self
# print(curr_node.value)
while curr_node.right:
curr_node=curr_node.right
return curr_node.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
# goto the left and bottomost node and print that value to its left
# now print value of node iteself
# then goto right and print that value
# recurse until all values are printed.
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
# create a queue for nodes
# add first node to the queue
# while queue is not empty
# remove the first node from the queue
# print the remove node
# add children into the queue
queue = dll_queue()
queue.enqueue(self)
while len(queue)>0:
curr_node = queue.dequeue()
print (f'Value of current node is {curr_node.value}')
if curr_node.left:
queue.enqueue(curr_node.left)
if curr_node.right:
queue.enqueue(curr_node.right)

# Print the value of every node, starting with the given node,
# in an iterative depth first traversal
def dft_print(self):
pass
# Create a stack for the nodes
# add the first node to stack
# add the the children to the stack. keep in mind to add these in same order.
stack = dll_Stack()
stack.push(self)

while len(stack)>0:
curr_node = stack.pop()
print(f'Value of current node is {curr_node.value}')
if curr_node.left:
stack.push(curr_node.left)
if curr_node.right:
stack.push(curr_node.right)

# Stretch Goals -------------------------
# Note: Research may be required
Expand All @@ -73,13 +147,13 @@ def post_order_dft(self):
bst.insert(4)
bst.insert(2)

bst.bft_print()
#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()
# print("elegant methods")
# print("pre order")
# bst.pre_order_dft()
# print("in order")
# bst.in_order_dft()
# print("post order")
# bst.post_order_dft()
1 change: 1 addition & 0 deletions binary_search_tree/test_binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def test_print_traversals(self):
sys.stdout = io.StringIO()
self.bst.bft_print()
output = sys.stdout.getvalue()
# print (output)
self.assertTrue(output == "1\n8\n5\n3\n7\n2\n4\n6\n" or
output == "1\n8\n5\n7\n3\n6\n4\n2\n")

Expand Down
Loading