class Node:
def __init__(self, val):
self.l_child = None
self.r_child = None
self.data = val
def binary_insert(root, node):
if root is None:
root = node
else:
if root.data > node.data:
if root.l_child is None:
root.l_child = node
else:
binary_insert(root.l_child, node)
else:
if root.r_child is None:
root.r_child = node
else:
binary_insert(root.r_child, node)
def in_order_print(root):
if not root:
return
in_order_print(root.l_child)
print root.data
in_order_print(root.r_child)
def pre_order_print(root):
if not root:
return
print root.data
pre_order_print(root.l_child)
pre_order_print(root.r_child) r = Node(3)
binary_insert(r, Node(7))
binary_insert(r, Node(1))
binary_insert(r, Node(5)) 3
/ \
1 7
/
5print "in order:"
in_order_print(r)
print "pre order"
pre_order_print(r)
in order:
1
3
5
7
pre order
3
1
7
5
# Given a Binary Tree, write a function that returns the size of the largest subtree which
# is also a Binary Search Tree (BST).
# If the complete Binary Tree is BST, then return the size of whole tree.
class BinarySearchTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def largest_BST(self):
# Set the initial values for calling
# largestBSTUtil()
Min = [999999999999] # For minimum value in right subtree
Max = [-999999999999] # For maximum value in left subtree
max_size = [0] # For size of the largest BST
is_bst = [0]
largestBSTUtil(node, Min, Max, max_size, is_bst)
return max_size[0]
# largestBSTUtil() updates max_size_ref[0]
# for the size of the largest BST subtree.
# Also, if the tree rooted with node is
# non-empty and a BST, then returns size of
# the tree. Otherwise returns 0.
def largestBSTUtil(node, min_ref, max_ref, max_size_ref, is_bst_ref):
# Base Case
if node == None:
is_bst_ref[0] = 1 # An empty tree is BST
return 0 # Size of the BST is 0
Min = 999999999999
# A flag variable for left subtree property
# i.e., max(root.left) < root.data
left_flag = False
# A flag variable for right subtree property
# i.e., min(root.right) > root.data
right_flag = False
ls, rs = 0, 0 # To store sizes of left and
# right subtrees
# Following tasks are done by recursive
# call for left subtree
# a) Get the maximum value in left subtree
# (Stored in max_ref[0])
# b) Check whether Left Subtree is BST or
# not (Stored in is_bst_ref[0])
# c) Get the size of maximum size BST in
# left subtree (updates max_size[0])
max_ref[0] = -999999999999
ls = largestBSTUtil(node.left, min_ref, max_ref, max_size_ref, is_bst_ref)
if is_bst_ref[0] == 1 and node.data > max_ref[0]:
left_flag = True
# Before updating min_ref[0], store the min
# value in left subtree. So that we have the
# correct minimum value for this subtree
Min = min_ref[0]
# The following recursive call does similar
# (similar to left subtree) task for right subtree
min_ref[0] = 999999999999
rs = largestBSTUtil(node.right, min_ref, max_ref, max_size_ref, is_bst_ref)
if is_bst_ref[0] == 1 and node.data < min_ref[0]:
right_flag = True
# Update min and max values for the
# parent recursive calls
if Min < min_ref[0]:
min_ref[0] = Min
if node.data < min_ref[0]: # For leaf nodes
min_ref[0] = node.data
if node.data > max_ref[0]:
max_ref[0] = node.data
# If both left and right subtrees are BST.
# And left and right subtree properties hold
# for this node, then this tree is BST.
# So return the size of this tree
if left_flag and right_flag:
if ls + rs + 1 > max_size_ref[0]:
max_size_ref[0] = ls + rs + 1
return ls + rs + 1
else:
# Since this subtree is not BST, set is_bst
# flag for parent calls is_bst_ref[0] = 0;
return 0
# Driver Code
if __name__ == "__main__":
# Let us construct the following Tree
# 50
# / \
# 10 60
# / \ / \
# 5 20 55 70
# / / \
# 45 65 80
root = newNode(50)
root.left = newNode(10)
root.right = newNode(60)
root.left.left = newNode(5)
root.left.right = newNode(20)
root.right.left = newNode(55)
root.right.left.left = newNode(45)
root.right.right = newNode(70)
root.right.right.left = newNode(65)
root.right.right.right = newNode(80)
# The complete tree is not BST as 45 is in
# right subtree of 50. The following subtree
# is the largest BST
# 60
# / \
# 55 70
# / / \
# 45 65 80
print("Size of the largest BST is", largestBST(root))
# This code is contributed by PranchalK
class Node(object):
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None def insert(self, data):
if self.data == data:
return False # As BST cannot contain duplicate data
elif data < self.data:
''' Data less than the root data is placed to the left of the root '''
if self.leftChild:
return self.leftChild.insert(data)
else:
self.leftChild = Node(data)
return True
else:
''' Data greater than the root data is placed to the right of the root '''
if self.rightChild:
return self.rightChild.insert(data)
else:
self.rightChild = Node(data)
return True def find(self, data):
if(data == self.data):
return True
elif(data < self.data):
if self.leftChild:
return self.leftChild.find(data)
else:
return False
else:
if self.rightChild:
return self.rightChild.find(data)
else:
return False def preorder(self):
'''For preorder traversal of the BST '''
if self:
print(str(self.data), end = ' ')
if self.leftChild:
self.leftChild.preorder()
if self.rightChild:
self.rightChild.preorder() def inorder(self):
''' For Inorder traversal of the BST '''
if self:
if self.leftChild:
self.leftChild.inorder()
print(str(self.data), end = ' ')
if self.rightChild:
self.rightChild.inorder() def postorder(self):
''' For postorder traversal of the BST '''
if self:
if self.leftChild:
self.leftChild.postorder()
if self.rightChild:
self.rightChild.postorder()
print(str(self.data), end = ' ')class Tree(object):
def __init__(self, initial_data = []):
self.root = None
# If provided, add initial data
for data in initial_data:
self.insert(data)
def insert(self, data):
if self.root:
return self.root.insert(data)
else:
self.root = Node(data)
return True
def find(self, data):
if self.root:
return self.root.find(data)
else:
return False
def preorder(self):
if self.root is not None:
print()
print('Preorder: ')
self.root.preorder()
def inorder(self):
print()
if self.root is not None:
print('Inorder: ')
self.root.inorder()
def postorder(self):
print()
if self.root is not None:
print('Postorder: ')
self.root.postorder() def pprint(self, head_node=0, _pre="", _last=True, term=False):
head_node = self.root if head_node == 0 else head_node
data = "*" if head_node is None else head_node.data
print(_pre, "`- " if _last else "|- ", data, sep="")
_pre += " " if _last else "| "
if term: return
for i, child in enumerate([head_node.leftChild, head_node.rightChild]):
self.pprint(child, _pre, bool(i) ,term=not(bool(child)))if __name__ == '__main__':
tree = Tree()
tree.insert(10)
tree.insert(12)
tree.insert(5)
tree.insert(4)
tree.insert(20)
tree.insert(8)
tree.insert(7)
tree.insert(15)
tree.insert(13)
tree.pprint()
print(tree.find(1))
print(tree.find(12))
tree.preorder()
tree.inorder()
tree.postorder()class Node(object):
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
def insert(self, data):
if self.data == data:
return False # As BST cannot contain duplicate data
elif data < self.data:
''' Data less than the root data is placed to the left of the root '''
if self.leftChild:
return self.leftChild.insert(data)
else:
self.leftChild = Node(data)
return True
else:
''' Data greater than the root data is placed to the right of the root '''
if self.rightChild:
return self.rightChild.insert(data)
else:
self.rightChild = Node(data)
return True
def find(self, data):
if(data == self.data):
return True
elif(data < self.data):
if self.leftChild:
return self.leftChild.find(data)
else:
return False
else:
if self.rightChild:
return self.rightChild.find(data)
else:
return False
def preorder(self):
'''For preorder traversal of the BST '''
if self:
print(str(self.data), end = ' ')
if self.leftChild:
self.leftChild.preorder()
if self.rightChild:
self.rightChild.preorder()
def inorder(self):
''' For Inorder traversal of the BST '''
if self:
if self.leftChild:
self.leftChild.inorder()
print(str(self.data), end = ' ')
if self.rightChild:
self.rightChild.inorder()
def postorder(self):
''' For postorder traversal of the BST '''
if self:
if self.leftChild:
self.leftChild.postorder()
if self.rightChild:
self.rightChild.postorder()
print(str(self.data), end = ' ')
class Tree(object):
def __init__(self, initial_data = []):
self.root = None
# If provided, add initial data
for data in initial_data:
self.insert(data)
def insert(self, data):
if self.root:
return self.root.insert(data)
else:
self.root = Node(data)
return True
def find(self, data):
if self.root:
return self.root.find(data)
else:
return False
def preorder(self):
if self.root is not None:
print()
print('Preorder: ')
self.root.preorder()
def inorder(self):
print()
if self.root is not None:
print('Inorder: ')
self.root.inorder()
def postorder(self):
print()
if self.root is not None:
print('Postorder: ')
self.root.postorder()
def pprint(self, head_node=0, _pre="", _last=True, term=False):
head_node = self.root if head_node == 0 else head_node
data = "*" if head_node is None else head_node.data
print(_pre, "`- " if _last else "|- ", data, sep="")
_pre += " " if _last else "| "
if term: return
for i, child in enumerate([head_node.leftChild, head_node.rightChild]):
self.pprint(child, _pre, bool(i) ,term=not(bool(child)))
if __name__ == '__main__':
tree = Tree()
tree.insert(10)
tree.insert(12)
tree.insert(5)
tree.insert(4)
tree.insert(20)
tree.insert(8)
tree.insert(7)
tree.insert(15)
tree.insert(13)
tree.pprint()
print(tree.find(1))
print(tree.find(12))
tree.preorder()
tree.inorder()
tree.postorder()`- 10
|- 5
| |- 4
| | |- *
| | `- *
| `- 8
| |- 7
| | |- *
| | `- *
| `- *
`- 12
|- *
`- 20
|- 15
| |- 13
| | |- *
| | `- *
| `- *
`- *
False
True
Preorder:
10 5 4 8 7 12 20 15 13
Inorder:
4 5 7 8 10 12 13 15 20
Postorder:
4 7 8 5 13 15 20 12 10 class BSTNode:
def __init__(self,
class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def insert(self, value):
# create a node for the new value
new_node = BSTNode(value)
# compare the node value to the self value
if (
new_node.value <= self.value
): # less then or equal to will all go to the left if any duplicats too
# we add to the left
if self.left is None:
self.left = new_node
else:
self.left.insert(value)
else:
# we add to the right
# if space exists
if self.right is None:
self.right = new_node
else:
self.right.insert(value)
def search(self, target):
if target == self.value:
return True
# check if value is less than self.value
if target < self.value:
# look to the left
if self.left is None:
return False
return self.left.search(target)
else:
# look to the right
if self.right is None:
return False
return self.right.search(target)
def find_minimum_value(self):
# if self.left is None: #recursive here
# return self.value
# min_value = self.left.find_minimum_value()
# return min_value
curr_node = self
while curr_node.left is not None:
curr_node = curr_node.left
return curr_node.value
root = BSTNode(10)
# root.left = BSTNode(6)
# root.right = BSTNode(12)
root.insert(6)
root.insert(7)
root.insert(12)
root.insert(5)
root.insert(14)
root.insert(8)
print(f"minimum value in tree is: {root.find_minimum_value()}")
print(f"does 8 exist? {root.search(8)}")
print(f"does 8 exist? {root.search(7)}")
print(f"does 8 exist? {root.search(15)}")class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def insert(self, value):
# create a node for the new value
new_node = BSTNode(value)
# compare the node value to the self value
if (
new_node.value <= self.value
): # less then or equal to will all go to the left if any duplicats too
# we add to the left
if self.left is None:
self.left = new_node
else:
self.left.insert(value)
else:
# we add to the right
# if space exists
if self.right is None:
self.right = new_node
else:
self.right.insert(value)
def search(self, target):
if target == self.value:
return True
# check if value is less than self.value
if target < self.value:
# look to the left
if self.left is None:
return False
return self.left.search(target)
else:
# look to the right
if self.right is None:
return False
return self.right.search(target)
def find_minimum_value(self):
# if self.left is None: #recursive here
# return self.value
# min_value = self.left.find_minimum_value()
# return min_value
curr_node = self
while curr_node.left is not None:
curr_node = curr_node.left
return curr_node.value
root = BSTNode(10)
# root.left = BSTNode(6)
# root.right = BSTNode(12)
root.insert(6)
root.insert(7)
root.insert(12)
root.insert(5)
root.insert(14)
root.insert(8)
print(f"minimum value in tree is: {root.find_minimum_value()}")
print(f"does 8 exist? {root.search(8)}")
print(f"does 8 exist? {root.search(7)}")
print(f"does 8 exist? {root.search(15)}")class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def insert(self, value):
# create a node for the new value
new_node = BSTNode(value)
# compare the node value to the self value
if (
new_node.value <= self.value
): # less then or equal to will all go to the left if any duplicats too
# we add to the left
if self.left is None:
self.left = new_node
else:
self.left.insert(value)
else:
# we add to the right
# if space exists
if self.right is None:
self.right = new_node
else:
self.right.insert(value)
def search(self, target):
if target == self.value:
return True
# check if value is less than self.value
if target < self.value:
# look to the left
if self.left is None:
return False
return self.left.search(target)
else:
# look to the right
if self.right is None:
return False
return self.right.search(target)
def find_minimum_value(self):
# if self.left is None: #recursive here
# return self.value
# min_value = self.left.find_minimum_value()
# return min_value
curr_node = self
while curr_node.left is not None:
curr_node = curr_node.left
return curr_node.value
root = BSTNode(10)
# root.left = BSTNode(6)
# root.right = BSTNode(12)
root.insert(6)
root.insert(7)
root.insert(12)
root.insert(5)
root.insert(14)
root.insert(8)
print(f"minimum value in tree is: {root.find_minimum_value()}")
print(f"does 8 exist? {root.search(8)}")
print(f"does 8 exist? {root.search(7)}")
print(f"does 8 exist? {root.search(15)}")# Implement a Binary Search Tree (BST) that can insert values and check if
# values are present
class Node(object):
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BST(object):
def __init__(self, root):
self.root = Node(root)
def insert(self, new_val):
if(self.root.left==None):
if(self.root.value>new_val):
self.root.left = Node(new_val)
elif(self.root.right==None):
if(self.root.value<new_val):
self.root.right = Node(new_val)
else:
current = self.root
while(current.left!=None or current.right!=None):
if(current.value>new_val):
current = current.left
else:
current = current.right
if(current.left==None):
current.left = Node(new_val)
else:
current.right = Node(new_val)
def search(self, find_val):
if(self.root.left==None and self.root.right==None and self.root.value!=find_val):
return False
else:
current = self.root
val_possible = True
while(val_possible):
if(current.value==find_val):
return True
if(current.value<find_val):
current = current.right
else:
current = current.left
if(current==None):
return False
if(current.value<find_val and (current.right==None or current.right>find_val)):
return False
if(current.value>find_val and (current.left==None or current.left<find_val)):
return False
# Set up tree
tree = BST(4)
# Insert elements
tree.insert(2)
tree.insert(1)
tree.insert(3)
tree.insert(5)
# Check search
# Should be True
print tree.search(4)
# Should be False
print tree.search(6)
class Node(object):
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BinaryTree(object):
def __init__(self, root):
self.root = Node(root)
def preorder_search(self, start, find_val):
"""Helper method - use this to create a
recursive search solution."""
if(start.value == find_val):
return True
if(start.left!=None):
left_result = self.preorder_search(start.left, find_val)
else:
left_result = False
if(start.right!=None and left_result!=True):
right_result = self.preorder_search(start.right, find_val)
else:
right_result = False
if(left_result==True or right_result==True):
return True
else:
return False
def preorder_print(self, start, traversal):
"""Helper method - use this to create a
recursive print solution."""
traversal += '-' + str(start.value)
left_nums = ''
right_nums = ''
if(start.left!=None):
traversal = self.preorder_print(start.left, traversal)
if(start.right!=None):
traversal = self.preorder_print(start.right, traversal)
return traversal
def search(self, find_val):
"""Return True if the value
is in the tree, return
False otherwise."""
return self.preorder_search(self.root, find_val)
#print(self.preorder_search(self.root, find_val))
def print_tree(self):
"""Print out all tree nodes
as they are visited in
a pre-order traversal."""
all_nodes = self.preorder_print(self.root, '')
all_nodes = all_nodes[1:]
return all_nodes
# Set up tree
tree = BinaryTree(1)
tree.root.left = Node(2)
tree.root.right = Node(3)
tree.root.left.left = Node(4)
tree.root.left.right = Node(5)
# Test search
# Should be True
print(tree.search(4))
# Should be False
print(tree.search(6))
# Test print_tree
# Should be 1-2-4-5-3
print(tree.print_tree())
class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def insert(self, value):
# create a node for the new value
new_node = BSTNode(value)
# compare the node value to the self value
if (
new_node.value <= self.value
): # less then or equal to will all go to the left if any duplicats too
# we add to the left
if self.left is None:
self.left = new_node
else:
self.left.insert(value)
else:
# we add to the right
# if space exists
if self.right is None:
self.right = new_node
else:
self.right.insert(value)
def search(self, target):
if target == self.value:
return True
# check if value is less than self.value
if target < self.value:
# look to the left
if self.left is None:
return False
return self.left.search(target)
else:
# look to the right
if self.right is None:
return False
return self.right.search(target)
def find_minimum_value(self):
# if self.left is None: #recursive here
# return self.value
# min_value = self.left.find_minimum_value()
# return min_value
curr_node = self
while curr_node.left is not None:
curr_node = curr_node.left
return curr_node.value
root = BSTNode(10)
# root.left = BSTNode(6)
# root.right = BSTNode(12)
root.insert(6)
root.insert(7)
root.insert(12)
root.insert(5)
root.insert(14)
root.insert(8)
print(f"minimum value in tree is: {root.find_minimum_value()}")
print(f"does 8 exist? {root.search(8)}")
print(f"does 8 exist? {root.search(7)}")
print(f"does 8 exist? {root.search(15)}")
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BST:
def __init__(self):
self.root = None
def Insert(self, value):
self.root = self.__InsertWrap(self.root, value)
def __InsertWrap(self, x, value):
if x == None:
x = Node(value)
else:
if value < x.value:
# print("left")
x.left = self.__InsertWrap(x.left, value)
else:
# print("right")
x.right = self.__InsertWrap(x.right, value)
return x
def InOrder(self):
return self.__InOrder(self.root)
def __InOrder(self, x):
if x:
self.__InOrder(x.left)
print(x.value)
self.__InOrder(x.right)
def PreOrder(self):
return self.__PreOrder(self.root)
def __PreOrder(self, x):
if x:
print(x.value)
self.__PreOrder(x.left)
self.__PreOrder(x.right)
def PostOrder(self):
return self.__PostOrder(self.root)
def __PostOrder(self, x):
if x:
self.__PostOrder(x.left)
self.__PostOrder(x.right)
print(x.value)
def FindMin(self, x):
while x.left != None:
x = x.left
return x.value
def FindMax(self, x):
while x.right != None:
x = x.right
return x.value
def successor(self, x):
if x.right != None:
xx = x.right
while xx.left != None:
xx = xx.left
return xx.value
def predecessor(self, x):
if x.left != None:
xx = x.left
while xx.right != None:
xx = x.right
return xx.value
def Height(self, x):
y = self.__Height(x)
return y
def __Height(self, x):
if x == None:
return 0
else:
return 1 + max(self.__Height(x.left), self.__Height(x.right))
def delete(self, node):
x = self.root # rootNode, {Parent Node of desired Node}
if node > x.value:
y = x.right # desiredNode {Node to be delted} [iff on right]
else:
y = x.left # desiredNode [iff left]
while y.value != node: # Searching the Node to delete
if node > y.value:
x = x.right
y = y.right
else:
x = x.left
y = y.left
# print("xr", x.right.value)
# print("y", y.value)
left = x.left # left of ParentNode
right = x.right # right of ParentNode
# Case 01
if left.value == y.value and left.left is None and left.right is None:
# print("left", x.left.value)
x.left = None
elif right.value == y.value and right.left is None and right.right is None:
# print("right", x.right.value)
x.right = None
# Case 02
elif (
left.value == y.value
and (left.left is not None and left.right is None)
or (left.left is None and left.right is not None)
):
if left.left is not None:
child = left.left
elif left.right is not None:
child = left.right
x.left = None
x.left = child
# print("x",x.left.value)
elif (
right.value == y.value
and (right.left is not None and right.right is None)
or (right.left is None and right.right is not None)
):
if right.left is not None:
child = right.left
elif right.right is not None:
child = right.right
x.right = None
x.right = child
# Case 03
elif left.value == y.value and left.left is not None and left.right is not None:
lChild = left.left
rChild = left.right
min = self.successor(left)
self.delete(min)
minNode = Node(min)
minNode.left = lChild
minNode.right = rChild
x.left = None
x.left = minNode
elif (
right.value == y.value
and right.left is not None
and right.right is not None
):
lChild = right.left
rChild = right.right
min = self.successor(right)
self.delete(min)
minNode = Node(min)
minNode.left = lChild
minNode.right = rChild
x.right = None
x.right = minNode
# Driver Code
a = BST()
a.Insert(20)
a.Insert(40)
a.Insert(12)
a.Insert(1)
root = a.root
print("Getting Inorder:")
a.InOrder()
print("\nGetting Preorder:")
a.PreOrder()
print("\nGetting PostOrder:")
a.PostOrder()
print("\nGetting Height:")
print(a.Height(root))
print("\nGetting Minimum Node:")
print(a.FindMin(root))
print("\nGetting Maximum Node:")
print(a.FindMax(root))
print("\nGetting Successor:")
print(a.successor(root))
print("\nGetting Predecessor:")
print(a.predecessor(root))
print("\nDeleting a specific Node:")
a.delete(12)
print("\nTo cross-check deletion, printing preorder:")
a.PreOrder()
# In[ ]:
# A utility function to search a given key in BST
def search(root,key):
# Base Cases: root is null or key is present at root
if root is None or root.val == key:
return root
# Key is greater than root's key
if root.val < key:
return search(root.right,key)
# Key is smaller than root's key
return search(root.left,key) 100 100
/ \ Insert 40 / \
20 500 ---------> 20 500
/ \ / \
10 30 10 30
\
40# Python program to demonstrate
# insert operation in binary search tree
# A utility class that represents
# an individual node in a BST
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
# A utility function to insert
# a new node with the given key
def insert(root, key):
if root is None:
return Node(key)
else:
if root.val == key:
return root
elif root.val < key:
root.right = insert(root.right, key)
else:
root.left = insert(root.left, key)
return root
# A utility function to do inorder tree traversal
def inorder(root):
if root:
inorder(root.left)
print(root.val)
inorder(root.right)
# Driver program to test the above functions
# Let us create the following BST
# 50
# / \
# 30 70
# / \ / \
# 20 40 60 80
r = Node(50)
r = insert(r, 30)
r = insert(r, 20)
r = insert(r, 40)
r = insert(r, 70)
r = insert(r, 60)
r = insert(r, 80)
# Print inoder traversal of the BST
inorder(r)20
30
40
50
60
70
8010 15 20 30 40 50 60 class BSTNode:
def __init__(self, val=None):
self.left = None
self.right = None
self.val = valCode language: Python (python)def insert(self, val):
if not self.val:
self.val = val
return
if self.val == val:
return
if val < self.val:
if self.left:
self.left.insert(val)
return
self.left = BSTNode(val)
return
if self.right:
self.right.insert(val)
return
self.right = BSTNode(val)Code language: Python (python)def get_min(self):
current = self
while current.left is not None:
current = current.left
return current.val
def get_max(self):
current = self
while current.right is not None:
current = current.right
return current.valCode language: Python (python)def delete(self, val):
if self == None:
return self
if val < self.val:
self.left = self.left.delete(val)
return self
if val > self.val:
self.right = self.right.delete(val)
return self
if self.right == None:
return self.left
if self.left == None:
return self.right
min_larger_node = self.right
while min_larger_node.left:
min_larger_node = min_larger_node.left
self.val = min_larger_node.val
self.right = self.right.delete(min_larger_node.val)
return selfdef delete(self, val):
if self == None:
return self
if val < self.val:
if self.left:
self.left = self.left.delete(val)
return self
if val > self.val:
if self.right:
self.right = self.right.delete(val)
return self
if self.right == None:
return self.left
if self.left == None:
return self.right
min_larger_node = self.right
while min_larger_node.left:
min_larger_node = min_larger_node.left
self.val = min_larger_node.val
self.right = self.right.delete(min_larger_node.val)
return selfdef delete(self, val):
if self == None:
return self
if val < self.val:
self.left = self.left.delete(val)
return self
if val > self.val:
self.right = self.right.delete(val)
return self
if self.right == None:
return self.left
if self.left == None:
return self.right
min_larger_node = self.right
while min_larger_node.left:
min_larger_node = min_larger_node.left
self.val = min_larger_node.val
self.right = self.right.delete(min_larger_node.val)
return selfCode language: Python (python)def exists(self, val):
if val == self.val:
return True
if val < self.val:
if self.left == None:
return False
return self.left.exists(val)
if self.right == None:
return False
return self.right.exists(val)Code language: Python (python)def inorder(self, vals):
if self.left is not None:
self.left.inorder(vals)
if self.val is not None:
vals.append(self.val)
if self.right is not None:
self.right.inorder(vals)
return valsCode language: Python (python)def preorder(self, vals):
if self.val is not None:
vals.append(self.val)
if self.left is not None:
self.left.preorder(vals)
if self.right is not None:
self.right.preorder(vals)
return valsCode language: Python (python)def postorder(self, vals):
if self.left is not None:
self.left.postorder(vals)
if self.right is not None:
self.right.postorder(vals)
if self.val is not None:
vals.append(self.val)
return valsCode language: Python (python)def main():
nums = [12, 6, 18, 19, 21, 11, 3, 5, 4, 24, 18]
bst = BSTNode()
for num in nums:
bst.insert(num)
print("preorder:")
print(bst.preorder([]))
print("#")
print("postorder:")
print(bst.postorder([]))
print("#")
print("inorder:")
print(bst.inorder([]))
print("#")
nums = [2, 6, 20]
print("deleting " + str(nums))
for num in nums:
bst.delete(num)
print("#")
print("4 exists:")
print(bst.exists(4))
print("2 exists:")
print(bst.exists(2))
print("12 exists:")
print(bst.exists(12))
print("18 exists:")
print(bst.exists(18))Code language: Python (python)class BSTNode:
def __init__(self, val=None):
self.left = None
self.right = None
self.val = val
def insert(self, val):
if not self.val:
self.val = val
return
if self.val == val:
return
if val < self.val:
if self.left:
self.left.insert(val)
return
self.left = BSTNode(val)
return
if self.right:
self.right.insert(val)
return
self.right = BSTNode(val)
def get_min(self):
current = self
while current.left is not None:
current = current.left
return current.val
def get_max(self):
current = self
while current.right is not None:
current = current.right
return current.val
def delete(self, val):
if self == None:
return self
if val < self.val:
if self.left:
self.left = self.left.delete(val)
return self
if val > self.val:
if self.right:
self.right = self.right.delete(val)
return self
if self.right == None:
return self.left
if self.left == None:
return self.right
min_larger_node = self.right
while min_larger_node.left:
min_larger_node = min_larger_node.left
self.val = min_larger_node.val
self.right = self.right.delete(min_larger_node.val)
return self
def exists(self, val):
if val == self.val:
return True
if val < self.val:
if self.left == None:
return False
return self.left.exists(val)
if self.right == None:
return False
return self.right.exists(val)
def preorder(self, vals):
if self.val is not None:
vals.append(self.val)
if self.left is not None:
self.left.preorder(vals)
if self.right is not None:
self.right.preorder(vals)
return vals
def inorder(self, vals):
if self.left is not None:
self.left.inorder(vals)
if self.val is not None:
vals.append(self.val)
if self.right is not None:
self.right.inorder(vals)
return vals
def postorder(self, vals):
if self.left is not None:
self.left.postorder(vals)
if self.right is not None:
self.right.postorder(vals)
if self.val is not None:
vals.append(self.val)
return valsCode language: Python (python)class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
let a = new TreeNode("a");
let b = new TreeNode("b");
let c = new TreeNode("c");
let d = new TreeNode("d");
let e = new TreeNode("e");
let f = new TreeNode("f");
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.right = f;class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
class BST {
constructor() {
this.root = null;
}
//Insert a new node
recursiveInsert(val, currentNode = this.root) {
if (!this.root) {
this.root = new TreeNode(val);
return this;
}
if (val < currentNode.val) {
if (!currentNode.left) {
currentNode.left = new TreeNode(val);
} else {
this.insert(val, currentNode.left);
}
} else {
if (!currentNode.right) {
currentNode.right = new TreeNode(val);
} else {
this.insert(val, currentNode.right);
}
}
}
iterativeInsert(val, currentNode = this.root) {
if (!this.root) {
this.root = new TreeNode(val);
return this;
}
if (val < currentNode.val) {
if (!currentNode.left) {
currentNode.left = new TreeNode();
} else {
while (true) {
if (val < currentNode.val) {
if (!currenNodet.left) {
currentNode.left = new TreeNode();
return this;
} else {
currentNode = currentNode.left;
}
} else {
if (!currentNode.right) {
currentNode.right = new TreeNode();
return this;
} else {
currentNode = currentNode.right;
}
}
}
}
}
}
//Search the tree
searchRecur(val, currentNode = this.root) {
if (!currentNode) return false;
if (val < currentNode.val) {
return this.searchRecur(val, currentNode.left);
} else if (val > currentNode.val) {
return this.searchRecur(val, currentNode.right);
} else {
return true;
}
}
searchIter(val) {
let currentNode = this.root;
while (currentNode) {
if (val < currentNode.val) {
currentNode = currentNode.left;
} else if (val > currentNode.val) {
currentNode = currentNode.right;
} else {
return true;
}
}
return false;
}
// Maybe works, who knows, pulled it off the internet....
deleteNodeHelper(root, key) {
if (root === null) {
return false;
}
if (key < root.val) {
root.left = deleteNodeHelper(root.left, key);
return root;
} else if (key > root.val) {
root.right = deleteNodeHelper(root.right, key);
return root;
} else {
if (root.left === null && root.right === null) {
root = null;
return root;
}
if (root.left === null) return root.right;
if (root.right === null) return root.left;
let currNode = root.right;
while (currNode.left !== null) {
currNode = currNode.left;
}
root.val = currNode.val;
root.right = deleteNodeHelper(root.right, currNode.val);
return root;
}
}
//Recursive Depth First Search
preOrderTraversal(root) {
if (!root) return [];
let left = this.preOrderTraversal(root.left);
let right = this.preOrderTraversal(root.right);
return [root.val, ...left, ...right];
}
preOrderTraversalV2(root) {
if (!root) return [];
let newArray = new Array();
newArray.push(root.val);
newArray.push(...this.preOrderTraversalV2(root.left));
newArray.push(...this.preOrderTraversalV2(root.right));
return newArray;
}
inOrderTraversal(root) {
if (!root) return [];
let left = this.inOrderTraversal(root.left);
let right = this.inOrderTraversal(root.right);
return [...left, root.val, ...right];
}
inOrderTraversalV2(root) {
if (!root) return [];
let newArray = new Array();
newArray.push(...this.inOrderTraversalV2(root.left));
newArray.push(root.val);
newArray.push(...this.inOrderTraversalV2(root.right));
return newArray;
}
postOrderTraversal(root) {
if (!root) return [];
let left = this.postOrderTraversal(root.left);
let right = this.postOrderTraversal(root.right);
return [...left, ...right, root.val];
}
postOrderTraversalV2(root) {
if (!root) return [];
let newArray = new Array();
newArray.push(...this.postOrderTraversalV2(root.left));
newArray.push(...this.postOrderTraversalV2(root.right));
newArray.push(root.val);
return newArray;
}
// Iterative Depth First Search
iterativePreOrder(root) {
let stack = [root];
let results = [];
while (stack.length) {
let current = stack.pop();
results.push(current);
if (current.left) stack.push(current.left);
if (current.right) stack.push(current.right);
}
return results;
}
iterativeInOrder(root) {
let stack = [];
let current = root;
let results = [];
while (true) {
while (current) {
stack.push(current);
current = current.left;
}
if (!stack.length) break;
let removed = stack.pop();
results.push(removed);
current = current.right;
}
return results;
}
//To-Do iterativePostOrder
//Breadth First Search
breadthFirstSearch(root) {
let queue = [root];
let result = [];
while (queue.length) {
let current = queue.shift();
if (current.left) queue.push(current.left);
if (current.right) queue.push(current.left);
current.push(result);
}
return result;
}
// Converting a Sorted Array to a Binary Search Tree
sortedArrayToBST(nums) {
if (nums.length === 0) return null;
let mid = Math.floor(nums.length / 2);
let root = new TreeNode(nums[mid]);
let left = nums.slice(0, mid);
root.left = sortedArrayToBST(left);
let right = nums.slice(mid + 1);
root.right = sortedArrayToBST(right);
return root;
}
}



