2019-12-26 11:50:12 +00:00
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
A binary search Tree
|
2019-12-26 11:50:12 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
class Node:
|
2019-12-21 00:44:31 +00:00
|
|
|
def __init__(self, value, parent):
|
|
|
|
self.value = value
|
|
|
|
self.parent = parent # Added in order to delete a node easier
|
2018-10-19 12:48:28 +00:00
|
|
|
self.left = None
|
|
|
|
self.right = None
|
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
def __repr__(self):
|
|
|
|
from pprint import pformat
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
if self.left is None and self.right is None:
|
|
|
|
return str(self.value)
|
2020-01-18 12:24:33 +00:00
|
|
|
return pformat({"%s" % (self.value): (self.left, self.right)}, indent=1)
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
class BinarySearchTree:
|
2019-12-26 11:50:12 +00:00
|
|
|
def __init__(self, root=None):
|
2019-12-21 00:44:31 +00:00
|
|
|
self.root = root
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
Return a string of all the Nodes using in order traversal
|
|
|
|
"""
|
|
|
|
return str(self.root)
|
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
def __reassign_nodes(self, node, new_children):
|
|
|
|
if new_children is not None: # reset its kids
|
|
|
|
new_children.parent = node.parent
|
2019-12-26 11:50:12 +00:00
|
|
|
if node.parent is not None: # reset its parent
|
|
|
|
if self.is_right(node): # If it is the right children
|
2020-03-04 12:40:28 +00:00
|
|
|
node.parent.right = new_children
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2020-03-04 12:40:28 +00:00
|
|
|
node.parent.left = new_children
|
2019-12-21 00:44:31 +00:00
|
|
|
else:
|
2020-03-04 12:40:28 +00:00
|
|
|
self.root = new_children
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
def is_right(self, node):
|
|
|
|
return node == node.parent.right
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
def empty(self):
|
|
|
|
return self.root is None
|
|
|
|
|
|
|
|
def __insert(self, value):
|
|
|
|
"""
|
|
|
|
Insert a new node in Binary Search Tree with value label
|
|
|
|
"""
|
|
|
|
new_node = Node(value, None) # create a new Node
|
|
|
|
if self.empty(): # if Tree is empty
|
|
|
|
self.root = new_node # set its root
|
2019-12-26 11:50:12 +00:00
|
|
|
else: # Tree is not empty
|
|
|
|
parent_node = self.root # from root
|
2019-12-21 00:44:31 +00:00
|
|
|
while True: # While we don't get to a leaf
|
2019-12-26 11:50:12 +00:00
|
|
|
if value < parent_node.value: # We go left
|
2020-02-12 20:49:41 +00:00
|
|
|
if parent_node.left is None:
|
2019-12-21 00:44:31 +00:00
|
|
|
parent_node.left = new_node # We insert the new node in a leaf
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
parent_node = parent_node.left
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2020-02-12 20:49:41 +00:00
|
|
|
if parent_node.right is None:
|
2019-12-21 00:44:31 +00:00
|
|
|
parent_node.right = new_node
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
parent_node = parent_node.right
|
|
|
|
new_node.parent = parent_node
|
|
|
|
|
|
|
|
def insert(self, *values):
|
|
|
|
for value in values:
|
|
|
|
self.__insert(value)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def search(self, value):
|
|
|
|
if self.empty():
|
2020-02-11 08:29:09 +00:00
|
|
|
raise IndexError("Warning: Tree is empty! please use another.")
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2019-12-21 00:44:31 +00:00
|
|
|
node = self.root
|
|
|
|
# use lazy evaluation here to avoid NoneType Attribute error
|
|
|
|
while node is not None and node.value is not value:
|
|
|
|
node = node.left if value < node.value else node.right
|
|
|
|
return node
|
|
|
|
|
2019-12-26 11:50:12 +00:00
|
|
|
def get_max(self, node=None):
|
2019-12-21 00:44:31 +00:00
|
|
|
"""
|
|
|
|
We go deep on the right branch
|
|
|
|
"""
|
|
|
|
if node is None:
|
|
|
|
node = self.root
|
2019-10-05 05:14:13 +00:00
|
|
|
if not self.empty():
|
2019-12-26 11:50:12 +00:00
|
|
|
while node.right is not None:
|
2019-12-21 00:44:31 +00:00
|
|
|
node = node.right
|
|
|
|
return node
|
|
|
|
|
2019-12-26 11:50:12 +00:00
|
|
|
def get_min(self, node=None):
|
2019-12-21 00:44:31 +00:00
|
|
|
"""
|
|
|
|
We go deep on the left branch
|
|
|
|
"""
|
2019-12-26 11:50:12 +00:00
|
|
|
if node is None:
|
2019-12-21 00:44:31 +00:00
|
|
|
node = self.root
|
2019-12-26 11:50:12 +00:00
|
|
|
if not self.empty():
|
2019-12-21 00:44:31 +00:00
|
|
|
node = self.root
|
2019-12-26 11:50:12 +00:00
|
|
|
while node.left is not None:
|
2019-12-21 00:44:31 +00:00
|
|
|
node = node.left
|
|
|
|
return node
|
|
|
|
|
|
|
|
def remove(self, value):
|
2019-12-26 11:50:12 +00:00
|
|
|
node = self.search(value) # Look for the node with that label
|
|
|
|
if node is not None:
|
|
|
|
if node.left is None and node.right is None: # If it has no children
|
2019-12-21 00:44:31 +00:00
|
|
|
self.__reassign_nodes(node, None)
|
2019-12-26 11:50:12 +00:00
|
|
|
elif node.left is None: # Has only right children
|
2019-12-21 00:44:31 +00:00
|
|
|
self.__reassign_nodes(node, node.right)
|
2019-12-26 11:50:12 +00:00
|
|
|
elif node.right is None: # Has only left children
|
2019-12-21 00:44:31 +00:00
|
|
|
self.__reassign_nodes(node, node.left)
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2020-03-04 12:40:28 +00:00
|
|
|
tmp_node = self.get_max(
|
2019-12-26 11:50:12 +00:00
|
|
|
node.left
|
2020-03-04 12:40:28 +00:00
|
|
|
) # Gets the max value of the left branch
|
|
|
|
self.remove(tmp_node.value)
|
2019-12-26 11:50:12 +00:00
|
|
|
node.value = (
|
2020-03-04 12:40:28 +00:00
|
|
|
tmp_node.value
|
|
|
|
) # Assigns the value to the node to delete and keep tree structure
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
def preorder_traverse(self, node):
|
|
|
|
if node is not None:
|
2020-03-04 12:40:28 +00:00
|
|
|
yield node # Preorder Traversal
|
2019-12-21 00:44:31 +00:00
|
|
|
yield from self.preorder_traverse(node.left)
|
|
|
|
yield from self.preorder_traverse(node.right)
|
|
|
|
|
2020-03-04 12:40:28 +00:00
|
|
|
def traversal_tree(self, traversal_function=None):
|
2019-12-21 00:44:31 +00:00
|
|
|
"""
|
|
|
|
This function traversal the tree.
|
|
|
|
You can pass a function to traversal the tree as needed by client code
|
|
|
|
"""
|
2020-03-04 12:40:28 +00:00
|
|
|
if traversal_function is None:
|
2019-12-21 00:44:31 +00:00
|
|
|
return self.preorder_traverse(self.root)
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2020-03-04 12:40:28 +00:00
|
|
|
return traversal_function(self.root)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-08-21 06:55:50 +00:00
|
|
|
def inorder(self, arr: list, node: Node):
|
|
|
|
"""Perform an inorder traversal and append values of the nodes to
|
|
|
|
a list named arr"""
|
|
|
|
if node:
|
|
|
|
self.inorder(arr, node.left)
|
|
|
|
arr.append(node.value)
|
|
|
|
self.inorder(arr, node.right)
|
|
|
|
|
|
|
|
def find_kth_smallest(self, k: int, node: Node) -> int:
|
2021-04-26 05:46:50 +00:00
|
|
|
"""Return the kth smallest element in a binary search tree"""
|
2021-10-22 14:07:05 +00:00
|
|
|
arr: list = []
|
2020-08-21 06:55:50 +00:00
|
|
|
self.inorder(arr, node) # append all values to list using inorder traversal
|
|
|
|
return arr[k - 1]
|
|
|
|
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
def postorder(curr_node):
|
|
|
|
"""
|
|
|
|
postOrder (left, right, self)
|
|
|
|
"""
|
2020-03-04 12:40:28 +00:00
|
|
|
node_list = list()
|
2018-10-19 12:48:28 +00:00
|
|
|
if curr_node is not None:
|
2020-03-04 12:40:28 +00:00
|
|
|
node_list = postorder(curr_node.left) + postorder(curr_node.right) + [curr_node]
|
|
|
|
return node_list
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
def binary_search_tree():
|
2020-04-30 20:47:11 +00:00
|
|
|
r"""
|
2018-10-19 12:48:28 +00:00
|
|
|
Example
|
|
|
|
8
|
|
|
|
/ \
|
|
|
|
3 10
|
|
|
|
/ \ \
|
|
|
|
1 6 14
|
|
|
|
/ \ /
|
2019-08-19 13:37:49 +00:00
|
|
|
4 7 13
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-02-11 08:29:09 +00:00
|
|
|
>>> t = BinarySearchTree().insert(8, 3, 6, 1, 10, 14, 13, 4, 7)
|
|
|
|
>>> print(" ".join(repr(i.value) for i in t.traversal_tree()))
|
|
|
|
8 3 1 6 4 7 10 14 13
|
|
|
|
>>> print(" ".join(repr(i.value) for i in t.traversal_tree(postorder)))
|
|
|
|
1 4 7 6 3 13 14 10 8
|
|
|
|
>>> BinarySearchTree().search(6)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
IndexError: Warning: Tree is empty! please use another.
|
2019-12-26 11:50:12 +00:00
|
|
|
"""
|
2019-12-21 00:44:31 +00:00
|
|
|
testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
|
2018-10-19 12:48:28 +00:00
|
|
|
t = BinarySearchTree()
|
2019-12-21 00:44:31 +00:00
|
|
|
for i in testlist:
|
|
|
|
t.insert(i)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
# Prints all the elements of the list in order traversal
|
2019-12-21 00:44:31 +00:00
|
|
|
print(t)
|
2019-12-26 11:50:12 +00:00
|
|
|
|
|
|
|
if t.search(6) is not None:
|
2019-12-21 00:44:31 +00:00
|
|
|
print("The value 6 exists")
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2019-12-21 00:44:31 +00:00
|
|
|
print("The value 6 doesn't exist")
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-12-26 11:50:12 +00:00
|
|
|
if t.search(-1) is not None:
|
2019-12-21 00:44:31 +00:00
|
|
|
print("The value -1 exists")
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2019-12-21 00:44:31 +00:00
|
|
|
print("The value -1 doesn't exist")
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-12-26 11:50:12 +00:00
|
|
|
if not t.empty():
|
2019-12-21 00:44:31 +00:00
|
|
|
print("Max Value: ", t.get_max().value)
|
|
|
|
print("Min Value: ", t.get_min().value)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
for i in testlist:
|
|
|
|
t.remove(i)
|
|
|
|
print(t)
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
if __name__ == "__main__":
|
2019-12-21 00:44:31 +00:00
|
|
|
import doctest
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
doctest.testmod()
|
2020-02-11 08:29:09 +00:00
|
|
|
# binary_search_tree()
|