2023-08-15 23:04:53 +00:00
|
|
|
r"""
|
2018-10-19 12:48:28 +00:00
|
|
|
A binary search Tree
|
2023-08-15 23:04:53 +00:00
|
|
|
|
|
|
|
Example
|
|
|
|
8
|
|
|
|
/ \
|
|
|
|
3 10
|
|
|
|
/ \ \
|
|
|
|
1 6 14
|
|
|
|
/ \ /
|
|
|
|
4 7 13
|
|
|
|
|
2023-11-05 08:38:39 +00:00
|
|
|
>>> t = BinarySearchTree().insert(8, 3, 6, 1, 10, 14, 13, 4, 7)
|
2023-08-15 23:04:53 +00:00
|
|
|
>>> print(" ".join(repr(i.value) for i in t.traversal_tree()))
|
|
|
|
8 3 1 6 4 7 10 14 13
|
2023-10-25 20:28:23 +00:00
|
|
|
|
|
|
|
>>> tuple(i.value for i in t.traversal_tree(inorder))
|
|
|
|
(1, 3, 4, 6, 7, 8, 10, 13, 14)
|
|
|
|
>>> tuple(t)
|
|
|
|
(1, 3, 4, 6, 7, 8, 10, 13, 14)
|
|
|
|
>>> t.find_kth_smallest(3, t.root)
|
|
|
|
4
|
|
|
|
>>> tuple(t)[3-1]
|
|
|
|
4
|
|
|
|
|
2023-08-15 23:04:53 +00:00
|
|
|
>>> print(" ".join(repr(i.value) for i in t.traversal_tree(postorder)))
|
|
|
|
1 4 7 6 3 13 14 10 8
|
|
|
|
>>> t.remove(20)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Value 20 not found
|
|
|
|
>>> BinarySearchTree().search(6)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
IndexError: Warning: Tree is empty! please use another.
|
|
|
|
|
|
|
|
Other example:
|
|
|
|
|
|
|
|
>>> testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
|
|
|
|
>>> t = BinarySearchTree()
|
|
|
|
>>> for i in testlist:
|
2023-11-05 08:38:39 +00:00
|
|
|
... t.insert(i) # doctest: +ELLIPSIS
|
|
|
|
BinarySearchTree(root=8)
|
|
|
|
BinarySearchTree(root={'8': (3, None)})
|
|
|
|
BinarySearchTree(root={'8': ({'3': (None, 6)}, None)})
|
|
|
|
BinarySearchTree(root={'8': ({'3': (1, 6)}, None)})
|
|
|
|
BinarySearchTree(root={'8': ({'3': (1, 6)}, 10)})
|
|
|
|
BinarySearchTree(root={'8': ({'3': (1, 6)}, {'10': (None, 14)})})
|
|
|
|
BinarySearchTree(root={'8': ({'3': (1, 6)}, {'10': (None, {'14': (13, None)})})})
|
|
|
|
BinarySearchTree(root={'8': ({'3': (1, {'6': (4, None)})}, {'10': (None, {'14': ...
|
|
|
|
BinarySearchTree(root={'8': ({'3': (1, {'6': (4, 7)})}, {'10': (None, {'14': (13, ...
|
2023-08-15 23:04:53 +00:00
|
|
|
|
|
|
|
Prints all the elements of the list in order traversal
|
|
|
|
>>> print(t)
|
|
|
|
{'8': ({'3': (1, {'6': (4, 7)})}, {'10': (None, {'14': (13, None)})})}
|
|
|
|
|
|
|
|
Test existence
|
|
|
|
>>> t.search(6) is not None
|
|
|
|
True
|
2023-10-25 20:28:23 +00:00
|
|
|
>>> 6 in t
|
|
|
|
True
|
2023-08-15 23:04:53 +00:00
|
|
|
>>> t.search(-1) is not None
|
|
|
|
False
|
2023-10-25 20:28:23 +00:00
|
|
|
>>> -1 in t
|
|
|
|
False
|
2023-08-15 23:04:53 +00:00
|
|
|
|
|
|
|
>>> t.search(6).is_right
|
|
|
|
True
|
|
|
|
>>> t.search(1).is_right
|
|
|
|
False
|
|
|
|
|
|
|
|
>>> t.get_max().value
|
|
|
|
14
|
2023-10-25 20:28:23 +00:00
|
|
|
>>> max(t)
|
|
|
|
14
|
2023-08-15 23:04:53 +00:00
|
|
|
>>> t.get_min().value
|
|
|
|
1
|
2023-10-25 20:28:23 +00:00
|
|
|
>>> min(t)
|
|
|
|
1
|
2023-08-15 23:04:53 +00:00
|
|
|
>>> t.empty()
|
|
|
|
False
|
2023-10-25 20:28:23 +00:00
|
|
|
>>> not t
|
|
|
|
False
|
2023-08-15 23:04:53 +00:00
|
|
|
>>> for i in testlist:
|
|
|
|
... t.remove(i)
|
|
|
|
>>> t.empty()
|
|
|
|
True
|
2023-10-25 20:28:23 +00:00
|
|
|
>>> not t
|
|
|
|
True
|
2019-12-26 11:50:12 +00:00
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
|
2023-10-25 20:28:23 +00:00
|
|
|
from __future__ import annotations
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2023-10-25 20:28:23 +00:00
|
|
|
from collections.abc import Iterable, Iterator
|
|
|
|
from dataclasses import dataclass
|
2023-11-05 08:38:39 +00:00
|
|
|
from typing import Any, Self
|
2022-10-15 22:51:23 +00:00
|
|
|
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2023-10-25 20:28:23 +00:00
|
|
|
@dataclass
|
2019-10-05 05:14:13 +00:00
|
|
|
class Node:
|
2023-10-25 20:28:23 +00:00
|
|
|
value: int
|
|
|
|
left: Node | None = None
|
|
|
|
right: Node | None = None
|
|
|
|
parent: Node | None = None # Added in order to delete a node easier
|
|
|
|
|
|
|
|
def __iter__(self) -> Iterator[int]:
|
|
|
|
"""
|
|
|
|
>>> list(Node(0))
|
|
|
|
[0]
|
|
|
|
>>> list(Node(0, Node(-1), Node(1), None))
|
|
|
|
[-1, 0, 1]
|
|
|
|
"""
|
|
|
|
yield from self.left or []
|
|
|
|
yield self.value
|
|
|
|
yield from self.right or []
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def __repr__(self) -> str:
|
2019-12-21 00:44:31 +00:00
|
|
|
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)
|
2022-07-07 14:34:07 +00:00
|
|
|
return pformat({f"{self.value}": (self.left, self.right)}, indent=1)
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2023-08-15 23:04:53 +00:00
|
|
|
@property
|
|
|
|
def is_right(self) -> bool:
|
2023-10-25 20:28:23 +00:00
|
|
|
return bool(self.parent and self is self.parent.right)
|
2023-08-15 23:04:53 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2023-10-25 20:28:23 +00:00
|
|
|
@dataclass
|
2019-10-05 05:14:13 +00:00
|
|
|
class BinarySearchTree:
|
2023-10-25 20:28:23 +00:00
|
|
|
root: Node | None = None
|
|
|
|
|
|
|
|
def __bool__(self) -> bool:
|
|
|
|
return bool(self.root)
|
|
|
|
|
|
|
|
def __iter__(self) -> Iterator[int]:
|
|
|
|
yield from self.root or []
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def __str__(self) -> str:
|
2019-12-21 00:44:31 +00:00
|
|
|
"""
|
|
|
|
Return a string of all the Nodes using in order traversal
|
|
|
|
"""
|
|
|
|
return str(self.root)
|
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def __reassign_nodes(self, node: Node, new_children: Node | None) -> None:
|
2020-03-04 12:40:28 +00:00
|
|
|
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
|
2023-08-15 23:04:53 +00:00
|
|
|
if node.is_right: # If it is the right child
|
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:
|
2023-06-18 16:30:06 +00:00
|
|
|
self.root = new_children
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def empty(self) -> bool:
|
2023-11-05 08:38:39 +00:00
|
|
|
"""
|
|
|
|
Returns True if the tree does not have any element(s).
|
|
|
|
False if the tree has element(s).
|
|
|
|
|
|
|
|
>>> BinarySearchTree().empty()
|
|
|
|
True
|
|
|
|
>>> BinarySearchTree().insert(1).empty()
|
|
|
|
False
|
|
|
|
>>> BinarySearchTree().insert(8, 3, 6, 1, 10, 14, 13, 4, 7).empty()
|
|
|
|
False
|
|
|
|
"""
|
|
|
|
return not self.root
|
2019-12-21 00:44:31 +00:00
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def __insert(self, value) -> None:
|
2019-12-21 00:44:31 +00:00
|
|
|
"""
|
|
|
|
Insert a new node in Binary Search Tree with value label
|
|
|
|
"""
|
2022-10-15 22:51:23 +00:00
|
|
|
new_node = Node(value) # create a new Node
|
2019-12-21 00:44:31 +00:00
|
|
|
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
|
2022-10-15 22:51:23 +00:00
|
|
|
if parent_node is None:
|
2023-03-01 16:23:33 +00:00
|
|
|
return
|
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
|
2024-03-28 17:25:41 +00:00
|
|
|
elif parent_node.right is None:
|
|
|
|
parent_node.right = new_node
|
|
|
|
break
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2024-03-28 17:25:41 +00:00
|
|
|
parent_node = parent_node.right
|
2019-12-21 00:44:31 +00:00
|
|
|
new_node.parent = parent_node
|
|
|
|
|
2023-11-05 08:38:39 +00:00
|
|
|
def insert(self, *values) -> Self:
|
2019-12-21 00:44:31 +00:00
|
|
|
for value in values:
|
|
|
|
self.__insert(value)
|
2023-11-05 08:38:39 +00:00
|
|
|
return self
|
2019-12-21 00:44:31 +00:00
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def search(self, value) -> Node | None:
|
2023-11-25 14:29:44 +00:00
|
|
|
"""
|
|
|
|
>>> tree = BinarySearchTree().insert(10, 20, 30, 40, 50)
|
|
|
|
>>> tree.search(10)
|
|
|
|
{'10': (None, {'20': (None, {'30': (None, {'40': (None, 50)})})})}
|
|
|
|
>>> tree.search(20)
|
|
|
|
{'20': (None, {'30': (None, {'40': (None, 50)})})}
|
|
|
|
>>> tree.search(30)
|
|
|
|
{'30': (None, {'40': (None, 50)})}
|
|
|
|
>>> tree.search(40)
|
|
|
|
{'40': (None, 50)}
|
|
|
|
>>> tree.search(50)
|
|
|
|
50
|
|
|
|
>>> tree.search(5) is None # element not present
|
|
|
|
True
|
|
|
|
>>> tree.search(0) is None # element not present
|
|
|
|
True
|
|
|
|
>>> tree.search(-5) is None # element not present
|
|
|
|
True
|
|
|
|
>>> BinarySearchTree().search(10)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
IndexError: Warning: Tree is empty! please use another.
|
|
|
|
"""
|
|
|
|
|
2019-12-21 00:44:31 +00:00
|
|
|
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
|
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def get_max(self, node: Node | None = None) -> Node | None:
|
2019-12-21 00:44:31 +00:00
|
|
|
"""
|
|
|
|
We go deep on the right branch
|
2023-11-25 14:29:44 +00:00
|
|
|
|
|
|
|
>>> BinarySearchTree().insert(10, 20, 30, 40, 50).get_max()
|
|
|
|
50
|
|
|
|
>>> BinarySearchTree().insert(-5, -1, 0.1, -0.3, -4.5).get_max()
|
|
|
|
{'0.1': (-0.3, None)}
|
|
|
|
>>> BinarySearchTree().insert(1, 78.3, 30, 74.0, 1).get_max()
|
|
|
|
{'78.3': ({'30': (1, 74.0)}, None)}
|
|
|
|
>>> BinarySearchTree().insert(1, 783, 30, 740, 1).get_max()
|
|
|
|
{'783': ({'30': (1, 740)}, None)}
|
2019-12-21 00:44:31 +00:00
|
|
|
"""
|
|
|
|
if node is None:
|
2022-10-15 22:51:23 +00:00
|
|
|
if self.root is None:
|
|
|
|
return None
|
2019-12-21 00:44:31 +00:00
|
|
|
node = self.root
|
2022-10-15 22:51:23 +00:00
|
|
|
|
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
|
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def get_min(self, node: Node | None = None) -> Node | None:
|
2019-12-21 00:44:31 +00:00
|
|
|
"""
|
|
|
|
We go deep on the left branch
|
2023-11-25 14:29:44 +00:00
|
|
|
|
|
|
|
>>> BinarySearchTree().insert(10, 20, 30, 40, 50).get_min()
|
|
|
|
{'10': (None, {'20': (None, {'30': (None, {'40': (None, 50)})})})}
|
|
|
|
>>> BinarySearchTree().insert(-5, -1, 0, -0.3, -4.5).get_min()
|
|
|
|
{'-5': (None, {'-1': (-4.5, {'0': (-0.3, None)})})}
|
|
|
|
>>> BinarySearchTree().insert(1, 78.3, 30, 74.0, 1).get_min()
|
|
|
|
{'1': (None, {'78.3': ({'30': (1, 74.0)}, None)})}
|
|
|
|
>>> BinarySearchTree().insert(1, 783, 30, 740, 1).get_min()
|
|
|
|
{'1': (None, {'783': ({'30': (1, 740)}, None)})}
|
2019-12-21 00:44:31 +00:00
|
|
|
"""
|
2019-12-26 11:50:12 +00:00
|
|
|
if node is None:
|
2019-12-21 00:44:31 +00:00
|
|
|
node = self.root
|
2022-10-15 22:51:23 +00:00
|
|
|
if self.root is None:
|
|
|
|
return None
|
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
|
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def remove(self, value: int) -> None:
|
2023-08-15 23:04:53 +00:00
|
|
|
# Look for the node with that label
|
|
|
|
node = self.search(value)
|
|
|
|
if node is None:
|
|
|
|
msg = f"Value {value} not found"
|
|
|
|
raise ValueError(msg)
|
|
|
|
|
|
|
|
if node.left is None and node.right is None: # If it has no children
|
|
|
|
self.__reassign_nodes(node, None)
|
|
|
|
elif node.left is None: # Has only right children
|
|
|
|
self.__reassign_nodes(node, node.right)
|
|
|
|
elif node.right is None: # Has only left children
|
|
|
|
self.__reassign_nodes(node, node.left)
|
|
|
|
else:
|
|
|
|
predecessor = self.get_max(
|
|
|
|
node.left
|
|
|
|
) # Gets the max value of the left branch
|
|
|
|
self.remove(predecessor.value) # type: ignore
|
|
|
|
node.value = (
|
|
|
|
predecessor.value # type: ignore
|
|
|
|
) # Assigns the value to the node to delete and keep tree structure
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def preorder_traverse(self, node: Node | None) -> Iterable:
|
2019-12-21 00:44:31 +00:00
|
|
|
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)
|
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def traversal_tree(self, traversal_function=None) -> Any:
|
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
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def inorder(self, arr: list, node: Node | None) -> None:
|
2020-08-21 06:55:50 +00:00
|
|
|
"""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"""
|
2022-10-15 22:51:23 +00:00
|
|
|
arr: list[int] = []
|
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
|
|
|
|
2023-10-25 20:28:23 +00:00
|
|
|
def inorder(curr_node: Node | None) -> list[Node]:
|
|
|
|
"""
|
|
|
|
inorder (left, self, right)
|
|
|
|
"""
|
|
|
|
node_list = []
|
|
|
|
if curr_node is not None:
|
2024-04-02 16:59:41 +00:00
|
|
|
node_list = [*inorder(curr_node.left), curr_node, *inorder(curr_node.right)]
|
2023-10-25 20:28:23 +00:00
|
|
|
return node_list
|
|
|
|
|
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
def postorder(curr_node: Node | None) -> list[Node]:
|
2019-12-21 00:44:31 +00:00
|
|
|
"""
|
|
|
|
postOrder (left, right, self)
|
|
|
|
"""
|
2022-10-15 01:07:03 +00:00
|
|
|
node_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
|
|
|
|
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
|
|
|
|
2022-10-15 22:51:23 +00:00
|
|
|
doctest.testmod(verbose=True)
|