Binary Search Trees
Tree data structures, search algorithms, and BST traversal. Aligned with BST visualizer simulator.
What you'll learn
- The defining properties of a Binary Search Tree (BST)
- How to insert, search, and delete nodes efficiently
- Why in-order traversal produces sorted output
- The difference between balanced and unbalanced BSTs
- Time complexity: average O(log n) vs worst-case O(n)
- The basics of AVL trees and self-balancing
Key concepts
BST Property
A BST is a binary tree where every node satisfies:
Left subtree values < Node value < Right subtree values
This holds recursively for every node in the tree.
50
/ \
30 70
/ \ / \
20 40 60 80
Core Operations
| Operation | Average Case | Worst Case (skewed tree) |
|---|---|---|
| Search | O(log n) | O(n) |
| Insert | O(log n) | O(n) |
| Delete | O(log n) | O(n) |
| In-order | O(n) | O(n) |
Insert Algorithm
- Start at root.
- If new value < current node → go left; else → go right.
- Repeat until an empty spot is found; insert there.
Search Algorithm
- Start at root.
- If value == current node → found.
- If value < current → search left subtree.
- If value > current → search right subtree.
- If null is reached → not found.
Delete Algorithm (three cases)
| Case | Situation | Action |
|---|---|---|
| 1 | Node has no children | Simply remove the node |
| 2 | Node has one child | Replace node with its child |
| 3 | Node has two children | Replace with in-order successor (smallest value in right subtree), then delete successor |
In-Order Traversal → Sorted Output
Traversal order: Left → Root → Right
For the tree above: 20 → 30 → 40 → 50 → 60 → 70 → 80
This property makes BSTs useful for sorting and range queries.
Balanced vs Unbalanced BST
| Feature | Balanced BST | Unbalanced (Skewed) BST |
|---|---|---|
| Height | O(log n) | O(n) |
| Search | O(log n) | O(n) |
| Example | AVL, Red-Black | Inserting sorted data |
Skewed tree example — inserting 10, 20, 30, 40 in order produces a linked list, not a tree.
AVL Trees (Self-Balancing BST)
An AVL tree maintains the balance factor at every node:
Balance Factor = Height(Left subtree) − Height(Right subtree)
Allowed values: −1, 0, or +1. If violated after insert/delete, the tree is rotated to restore balance.
| Rotation Type | When Used |
|---|---|
| Right Rotation | Left-Left imbalance |
| Left Rotation | Right-Right imbalance |
| Left-Right | Left-Right imbalance |
| Right-Left | Right-Left imbalance |
AVL trees guarantee O(log n) for all operations even in the worst case.
Quick check
-
A BST contains values 15, 10, 20, 8, 12, 17, 25. Write the result of an in-order traversal.
-
What is the worst-case time complexity for searching in a BST, and when does this worst case occur?
-
You need to delete node 20 from the BST below. Node 20 has left child 17 and right child 25, and 25 has left child 22. What value replaces 20, and why?
-
Explain why inserting data that is already sorted into a standard BST is inefficient.
-
What condition does an AVL tree enforce that a standard BST does not, and how does it maintain that condition after an insertion?
Key Takeaways (TL;DR)
- What you'll learn
- Key concepts
- Quick check
Master this topic with Drishti OS
Get unlimited mock tests, AI-powered mentorship, and complete video courses when you join.
Start Free Practice