Binary Search Trees (BST)
A Binary Search Tree (BST) is a type of binary tree that maintains a sorted order among its elements, allowing for efficient searching, insertion, and deletion operations. It is one of the most fundamental data structures in computer science and plays a key role in organizing data for efficient retrieval.
A Binary Search Tree is a binary tree in which the nodes are arranged in a specific order:
Here is an example of a Binary Search Tree:
50
/ \
30 70
/ \ / \
20 40 60 80
In this tree:
Inserting a node into a BST involves traversing the tree starting from the root and following these steps:
Searching for a value in a BST follows the same logic as insertion:
Deleting a node from a BST is more complex and can be handled in three possible cases:
Traversal refers to visiting all nodes in a particular order:
Below is a Python implementation of a Binary Search Tree (BST) that supports insertion, searching, and in-order traversal.
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.value = key
class BinarySearchTree:
def insert(self, root, key):
# If the tree is empty, return a new node
if not root:
return Node(key)
# Otherwise, recur down the tree
if key < root.value:
root.left = self.insert(root.left, key)
else:
root.right = self.insert(root.right, key)
return root
def search(self, root, key):
# Base case: root is null or key is present at the root
if root is None or root.value == key:
return root
# Key is greater than root's key
if key > root.value:
return self.search(root.right, key)
# Key is smaller than root's key
return self.search(root.left, key)
def inorderTraversal(self, root):
if root:
self.inorderTraversal(root.left) # Visit left subtree
print(root.value, end=" ") # Visit root
self.inorderTraversal(root.right) # Visit right subtree
# Example Usage
bst = BinarySearchTree()
root = None
# Inserting nodes
nodes = [50, 30, 70, 20, 40, 60, 80]
for node in nodes:
root = bst.insert(root, node)
# In-order Traversal (This will print the values in sorted order)
print("In-order Traversal of BST:")
bst.inorderTraversal(root)
# Searching for a node
key = 40
found_node = bst.search(root, key)
if found_node:
print(f"\nNode {key} found in the tree.")
else:
print(f"\nNode {key} not found in the tree.")
In-order Traversal of BST:
20 30 40 50 60 70 80
Node 40 found in the tree.
Efficient Searching: BSTs provide efficient searching with an average time complexity of when the tree is balanced.
Database Indexing: BSTs are often used in databases for indexing purposes. By maintaining the tree in sorted order, databases can quickly retrieve records based on search queries.
Sorting Algorithms: Since an in-order traversal of a BST produces the elements in sorted order, BSTs can be used to perform sorting.
Priority Queues: In some cases, Binary Search Trees can be used to implement priority queues, allowing for efficient insertion, deletion, and retrieval of the highest or lowest priority elements.
Autocompletion and Spell Checking: BSTs are often used in text processing applications such as autocomplete and spell-checking to quickly search for valid word prefixes or suggestions.