Binary Search Tree (BST)
Search, insert, delete, validation and interview patterns
1. Introduction
A Binary Search Tree (BST) is a special type of Binary Tree that follows a specific ordering rule.
BST Property:
Left\ Subtree < Root < Right\ Subtree
Example:
50
/ \
30 70
/ \ / \
20 40 60 80
Notice:
Left of 50 → 30,20,40 (smaller)
Right of 50 → 70,60,80 (larger)
2. Why Binary Search Tree?
Consider an Array:
10 20 30 40 50 60 70
Searching:
O(n)
for unsorted arrays.
BST reduces searching time by eliminating half of the tree at every step.
Similar idea to Binary Search.
3. BST Properties
For every node:
All values in left subtree are smaller.
All values in right subtree are greater.
Example:
15
/ \
10 20
/ \ / \
8 12 17 25
Valid BST.
Invalid BST:
15
/ \
20 10
Violates BST rule.
4. Binary Tree vs BST
| Feature | Binary Tree | BST |
|---|---|---|
| Ordering Rule | No | Yes |
| Search | O(n) | O(log n) average |
| Insert | Any position | Ordered |
| Delete | General | Ordered |
Binary Tree:
10
/ \
50 5
Valid Binary Tree
Not BST.
5. BST Structure
Example BST:
50
/ \
30 70
/ \ / \
20 40 60 80
Root:
50
Left Subtree:
30
20
40
Right Subtree:
70
60
80
6. BST Node Implementation
class Node {
int data;
Node left;
Node right;
Node(int data){
this.data = data;
}
}
Create Root:
Node root = new Node(50);
7. BST Terminology
Example:
50
/ \
30 70
/ \ / \
20 40 60 80
Root:
50
Leaf Nodes:
20 40 60 80
Parent of 20:
30
Sibling of 20:
40
Height:
2
8. Searching in BST
Search:
60
BST:
50
/ \
30 70
/
60
Process:
60 > 50
Go Right
60 < 70
Go Left
Found
Recursive Search
public boolean search(Node root,
int key){
if(root == null)
return false;
if(root.data == key)
return true;
if(key < root.data)
return search(root.left,key);
return search(root.right,key);
}
Complexity:
Balanced BST:
O(log n)
Worst Case:
O(n)
9. Insertion in BST
Insert:
65
BST:
50
/ \
30 70
/
60
Process:
65 > 50
Right
65 < 70
Left
65 > 60
Right
Result:
50
/ \
30 70
/
60
\
65
Java Implementation
public Node insert(Node root,
int value){
if(root == null)
return new Node(value);
if(value < root.data)
root.left =
insert(root.left,value);
else if(value > root.data)
root.right =
insert(root.right,value);
return root;
}
10. Deletion in BST
Most important BST topic.
Three cases.
Case 1: Delete Leaf Node
BST:
50
/
30
Delete:
30
Result:
50
Case 2: Delete Node with One Child
Before:
50
/
30
/
20
Delete:
30
Result:
50
/
20
Case 3: Delete Node with Two Children
Before:
50
/ \
30 70
/ \
60 80
Delete:
70
Replace with:
Inorder Successor
Successor:
80
Result:
50
/ \
30 80
/
60
Deletion Complexity:
O(h)
where
h = height
11. BST Traversals
Same as Binary Tree.
Preorder
Root
Left
Right
Inorder
Left
Root
Right
Postorder
Left
Right
Root
Level Order
BFS
12. Why Inorder Traversal is Important
For BST:
Inorder Traversal always produces:
Sorted Order
Example:
50
/ \
30 70
/ \ / \
20 40 60 80
Inorder:
20 30 40 50 60 70 80
Java:
public void inorder(Node root){
if(root == null)
return;
inorder(root.left);
System.out.print(root.data+" ");
inorder(root.right);
}
13. Finding Minimum Value
Minimum value is always:
Leftmost Node
Example:
50
/
30
/
20
Minimum:
20
Code:
while(root.left != null){
root = root.left;
}
14. Finding Maximum Value
Maximum value is:
Rightmost Node
Example:
50
\
70
\
90
Maximum:
90
15. Successor and Predecessor
Inorder Successor
Next larger element.
Example:
20 30 40 50 60
Successor of:
40
is:
50
Inorder Predecessor
Previous smaller element.
Predecessor of:
40
is:
30
Used in deletion.
16. Height of BST
Example:
50
/
30
/
20
Height:
2
Recursive:
public int height(Node root){
if(root == null)
return -1;
return 1 +
Math.max(
height(root.left),
height(root.right)
);
}
17. Balanced vs Unbalanced BST
Balanced:
50
/ \
30 70
Height:
O(log n)
Unbalanced:
10
\
20
\
30
\
40
Acts like Linked List.
Height:
O(n)
18. Time Complexity Analysis
Balanced BST:
| Operation | Complexity |
|---|---|
| Search | O(log n) |
| Insert | O(log n) |
| Delete | O(log n) |
Worst Case:
| Operation | Complexity |
|---|---|
| Search | O(n) |
| Insert | O(n) |
| Delete | O(n) |
19. BST Validation
Check whether tree satisfies BST property.
Approach:
Maintain:
Minimum Allowed Value
Maximum Allowed Value
Recursive Solution:
boolean isBST(Node root,
long min,
long max)
Common Interview Question.
20. Common BST Problems
Easy:
- Search Node
- Insert Node
- Find Min
- Find Max
Medium:
- Delete Node
- Validate BST
- Lowest Common Ancestor
- Kth Smallest Element
Hard:
- Recover BST
- Largest BST in Binary Tree
- Convert BST to Doubly Linked List
21. BST vs HashMap
| Feature | BST | HashMap |
|---|---|---|
| Search | O(log n) | O(1) average |
| Ordered Data | Yes | No |
| Sorted Traversal | Yes | No |
| Range Queries | Easy | Difficult |
Use BST when:
Data must remain sorted.
22. Applications of BST
Databases
Indexing
File Systems
Hierarchical Searching
Dictionaries
Word Lookup
Range Queries
Finding values between:
50 and 100
Ordered Data Storage
Maintaining sorted records.
23. Interview Patterns
Must Know:
- Recursive Search
- Recursive Insert
- Recursive Delete
- Inorder Traversal
- Validate BST
- Height Calculation
- Lowest Common Ancestor
- Kth Smallest Element
Most Asked:
- Search in BST
- Insert into BST
- Delete Node
- Validate BST
- Lowest Common Ancestor
- Kth Smallest Element
- Inorder Successor
24. Revision Summary
Binary Search Tree:
- Special Binary Tree
- Ordered Structure
BST Rule:
Left Subtree < Root < Right Subtree
Important Operations:
- Search
- Insert
- Delete
- Traversal
- Find Min
- Find Max
Important Property:
Inorder Traversal = Sorted Order
Complexity (Balanced BST):
Search → O(log n)
Insert → O(log n)
Delete → O(log n)
Must Know Interview Topics:
- BST Search
- BST Insert
- BST Delete
- BST Validation
- Successor & Predecessor
- Lowest Common Ancestor
- Kth Smallest Element
Learning Path:
BST Basics → Search → Insert → Traversals → Delete → Validate BST → LCA → Kth Smallest → AVL Tree → Red Black Tree