Introduction
In Data Structures and Algorithms (DSA), the Binary Search Tree (BST) is one of the most important and widely used data structures. A BST helps in searching, inserting, and deleting elements efficiently. It is commonly asked in coding interviews, competitive programming, and forms the foundation for advanced data structures like AVL Trees and Red-Black Trees.
What is a Binary Search Tree?
A Binary Search Tree (BST) is a special type of binary tree where:
Each node has at most two children: the left child and the right child.
The value of the left child is always less than the parent node.
The value of the right child is always greater than the parent node.
This property holds recursively for all nodes in the tree.
Example Structure
50
/ \
30 70
/ \ / \
20 40 60 80
π In this BST
Why Use a Binary Search Tree?
β
Efficient Searching β Average time complexity is O(log n).
β
Sorted Structure β Inorder traversal of a BST gives sorted elements.
β
Dynamic Data Handling β Supports insert and delete operations efficiently.
β
Foundation for Advanced Trees β Basis for balanced trees like AVL and Red-Black Trees.
Basic Operations on Binary Search Tree
1. Insert into BST
To insert an element:
Start at the root.
If the new value is smaller β move to the left child.
If the new value is larger β move to the right child.
Repeat until you find a null spot.
class Node {
int value;
Node left, right;
Node(int value) {
this.value = value;
left = right = null;
}
}
class BST {
Node root;
void insert(int value) {
root = insertRec(root, value);
}
Node insertRec(Node root, int value) {
if (root == null) {
root = new Node(value);
return root;
}
if (value < root.value)
root.left = insertRec(root.left, value);
else if (value > root.value)
root.right = insertRec(root.right, value);
return root;
}
}
2. Search in BST
To search for an element:
If the element matches the root β found.
If smaller β search in the left subtree.
If larger β search in the right subtree.
boolean search(Node root, int key) {
if (root == null) return false;
if (root.value == key) return true;
return key < root.value
? search(root.left, key)
: search(root.right, key);
}
3. Delete in BST
Deleting a node has 3 cases:
Leaf Node (no children): Just remove it.
One Child: Replace the node with its child.
Two Children: Replace with inorder successor (smallest value in right subtree).
Node delete(Node root, int key) {
if (root == null) return root;
if (key < root.value)
root.left = delete(root.left, key);
else if (key > root.value)
root.right = delete(root.right, key);
else {
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
root.value = minValue(root.right);
root.right = delete(root.right, root.value);
}
return root;
}
int minValue(Node root) {
int min = root.value;
while (root.left != null) {
min = root.left.value;
root = root.left;
}
return min;
}
4. Traversals in BST
Traversals are ways to visit nodes:
Inorder (Left β Root β Right) β Sorted order
Preorder (Root β Left β Right)
Postorder (Left β Right β Root)
void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.value + " ");
inorder(root.right);
}
}
π Example Inorder Traversal Output for the tree:
20 30 40 50 60 70 80
Time Complexity of BST Operations
Operation | Best Case | Average Case | Worst Case |
---|
Search | O(log n) | O(log n) | O(n) |
Insert | O(log n) | O(log n) | O(n) |
Delete | O(log n) | O(log n) | O(n) |
β οΈ Worst case happens when the tree becomes skewed (like a linked list). To avoid this, use balanced BSTs (e.g., AVL, Red-Black Tree).
π¨ Infographic: Binary Search Tree (BST) Quick Summary
π Structure of a BST
50
/ \
30 70
/ \ / \
20 40 60 80
Left child < Parent
Right child > Parent
Applies recursively
β‘ Operations in BST
Insert β Place based on value comparison
Search β Go left if smaller, right if larger
Delete β Handle leaf, one child, or two children
π Traversals
Inorder β Sorted
Preorder β Copy
Postorder β Delete
β±οΈ Time Complexity
Operation | Avg Case | Worst Case |
---|
Search | O(log n) | O(n) |
Insert | O(log n) | O(n) |
Delete | O(log n) | O(n) |
β οΈ Skewed BST behaves like a linked list.
Summary
The Binary Search Tree (BST) is a powerful data structure in DSA used for fast searching, insertion, and deletion. It maintains a sorted structure and supports different tree traversals. While it is efficient in most cases with O(log n) complexity, in the worst case it can degrade to O(n). Thatβs why balanced versions of BST are used in real-world applications.