Loading lessons...
AVL Trees - Worked Insertion & Deletion Traces
AVL Trees - Worked Insertion & Deletion Traces
AVL questions in the end-term exam are almost always "construct the AVL tree by inserting these keys". They carry 7.5-10 marks and are lost only through careless steps. This lesson works three full insertion sequences and one deletion sequence, all computed step by step, and gives a checklist to follow.
1. Exam Checklist for Each Insertion
- Insert the key as in a BST (as a new leaf).
- Walk back up from the new leaf, recomputing BF = h(left) − h(right) of each ancestor.
- Stop at the first ancestor with BF = +2 or −2. Call it Z (the pivot).
- Read the first two moves from Z towards the new key: L-L, R-R, L-R or R-L.
- Apply the rotation: LL → right; RR → left; LR → left at Z's child, then right at Z; RL → right at Z's child, then left at Z.
- Redraw the tree and write the BF next to every node. Check that the inorder sequence is still sorted.
Notation in the tables: X(L, R) means node X with left subtree L and right subtree R; − is an empty subtree.
- Trace 1 - Numbers: 50, 20, 60, 10, 8, 15, 32, 46, 11, 48
| Insert | Imbalance | Rotation | Tree after the step |
|---|---|---|---|
| 50 | - | - | 50 |
| 20 | - | - | 50(20, −) |
| 60 | - | - | 50(20, 60) |
| 10 | - | - | 50(20(10, −), 60) |
| 8 | BF(20) = +2, path L-L | LL: right at 20 | 50(10(8, 20), 60) |
| 15 | BF(50) = +2, path L-R | LR: left at 10, right at 50 | 20(10(8, 15), 50(−, 60)) |
| 32 | - | - | 20(10(8, 15), 50(32, 60)) |
| 46 | - | - | 20(10(8, 15), 50(32(−, 46), 60)) |
| 11 | - | - | 20(10(8, 15(11, −)), 50(32(−, 46), 60)) |
| 48 | BF(32) = −2, path R-R | RR: left at 32 | 20(10(8, 15(11, −)), 50(46(32, 48), 60)) |
Step "insert 15" in detail. The tree is 50(10(8, 20), 60). 15 goes 50 → left to 10 → right to 20 → left of 20. Walking up: BF(20) = +1, BF(10) = 1 − 2 = −1, BF(50) = 3 − 1 = +2. From 50 the path goes L (to 10) then R (to 20): case LR. Left-rotate at 10, then right-rotate at 50; the grandchild 20 becomes the root of the subtree.
Before fixing (insert 15) After LR rotation
50 (+2) 20
/ \ / \
10 (-1) 60 10 50
/ \ / \ \
8 20 8 15 60
/
15Final tree (levels: 20 / 10 50 / 8 15 46 60 / 11 32 48):
20
/ \
10 50
/ \ / \
8 15 46 60
/ / \
11 32 48Every node has BF ∈ {−1, 0, +1}: 20 (0), 10 (−1), 50 (+1), 15 (+1), 46 (0).
3. Trace 2 - Months in Alphabetical Order
Insert Mar, May, Nov, Aug, Apr, Jan, Dec, July, Feb, June, Oct, Sep (compared as strings: Apr < Aug < Dec < Feb < Jan < July < June < Mar < May < Nov < Oct < Sep).
| Insert | Imbalance | Rotation | Tree after the step |
|---|---|---|---|
| Mar | - | - | Mar |
| May | - | - | Mar(−, May) |
| Nov | BF(Mar) = −2, R-R | RR at Mar | May(Mar, Nov) |
| Aug | - | - | May(Mar(Aug, −), Nov) |
| Apr | BF(Mar) = +2, L-L | LL at Mar | May(Aug(Apr, Mar), Nov) |
| Jan | BF(May) = +2, L-R | LR at May | Mar(Aug(Apr, Jan), May(−, Nov)) |
| Dec | - | - | Mar(Aug(Apr, Jan(Dec, −)), May(−, Nov)) |
| July | - | - | Mar(Aug(Apr, Jan(Dec, July)), May(−, Nov)) |
| Feb | BF(Aug) = −2, R-L | RL at Aug | Mar(Dec(Aug(Apr, −), Jan(Feb, July)), May(−, Nov)) |
| June | BF(Mar) = +2, L-R | LR at Mar | Jan(Dec(Aug(Apr, −), Feb), Mar(July(−, June), May(−, Nov))) |
| Oct | BF(May) = −2, R-R | RR at May | Jan(Dec(Aug(Apr, −), Feb), Mar(July(−, June), Nov(May, Oct))) |
| Sep | - | - | Jan(Dec(Aug(Apr, −), Feb), Mar(July(−, June), Nov(May, Oct(−, Sep)))) |
Final tree - height 4 for 12 keys:
Jan
/ \
Dec Mar
/ \ / \
Aug Feb July Nov
/ \ / \
Apr June May Oct
\
SepInorder: Apr Aug Dec Feb Jan July June Mar May Nov Oct Sep - sorted ✓
- Trace 3 - Letters: B H A G W N P R S U M D E L I
| Insert | Rotation | Insert | Rotation |
|---|---|---|---|
| B, H, A, G, W | none | S | LR at W |
| N | RR at B | U | RR at P |
| P | LR at W | M | RL at H |
| R | none | D | none |
| E | LR at G | L | LL at N |
| I | none |
Final tree (levels: P / H S / B M R W / A E L N U / D G I):
P
/ \
H S
/ \ / \
B M R W
/ \ / \ /
A E L N U
/ \ /
D G I5. Deletion Trace
Deletion first removes the key as in a BST, then walks up to the root rebalancing. The rotation is chosen by the balance factor of the taller child of the unbalanced node Z:
| BF(Z) | BF of taller child | Rotation |
|---|---|---|
| +2 | 0 or +1 | Right rotation (R0 / R1 - like LL) |
| +2 | −1 | Left-right (R−1 - like LR) |
| −2 | 0 or −1 | Left rotation (L0 / L−1 - like RR) |
| −2 | +1 | Right-left (L1 - like RL) |
Start tree (built by inserting 50, 20, 60, 10, 30, 70, 5, 15, 25, 40, 35):
30
/ \
20 50
/ \ / \
10 25 40 60
/ \ / \
5 15 35 70Delete 70. It is a leaf; remove it. Walking up: BF(60) = 0, BF(50) = +1 (left 40-subtree height 2, right 60 height 1), BF(30) = 0. No rotation.
Delete 60. It is now a leaf; remove it. BF(50) = 2 − 0 = +2. Its taller child 40 has BF = +1 → right rotation at 50 (R1, like LL).
Before fixing After right rotation at 50
30 30
/ \ / \
20 50 (+2) 20 40
/ \ / / \ / \
10 25 40 (+1) 10 25 35 50
/ \ / / \
5 15 35 5 15Delete 30 (the root, two children). Replace it with its inorder successor 35 (the smallest key in the right subtree), then delete the old 35 (a leaf). BF(40) = 0 − 1 = −1 - still balanced. The root becomes 35:
35
/ \
20 40
/ \ \
10 25 50
/ \
5 15BF(35) = 3 − 2 = +1 - balanced. ✓
Why deletion may need several rotations: a rotation after deletion can make the subtree shorter than it was, which can unbalance an ancestor higher up, so the check continues all the way to the root (O(log n) rotations at most).
6. Common Mistakes
| Mistake | Correct approach |
|---|---|
| Rotating at the lowest unbalanced node you notice first from the top | Fix the first unbalanced ancestor found walking up from the new leaf |
| Naming the case from the root of the whole tree | Name it from the pivot Z: the first two moves from Z towards the new key |
| Forgetting to move the middle subtree (T2 / T3) | The child's inner subtree always moves across to the other node |
| Not writing balance factors | Mark BF at every node after each rotation - examiners look for it |
Key points to remember - Find the first ancestor with BF ±2; read the path L/R twice from it to choose LL, RR, LR or RL. - Insertion needs at most one rotation; deletion may need one per level. - 10, 20, 30, 40, 50, 25 → root 30; 50, 20, 60, 10, 8, 15, 32, 46, 11, 48 → root 20; the months → root Jan. - Always verify: inorder is sorted and every |BF| ≤ 1.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Construct an AVL tree by inserting Mar, May, Nov, Aug, Apr, Jan, Dec, July, Feb, June, Oct, Sep. (10 Marks)
- Write: the tree after each insertion with balance factors, naming every rotation (RR, LL, LR, RL, LR, RR), and the final tree rooted at Jan.
- Q2. Insert 50, 20, 60, 10, 8, 15, 32, 46, 11, 48 into an empty AVL tree. (7.5 Marks)
- Q3. Explain deletion in an AVL tree with an example. Why can deletion need more than one rotation? (7.5 Marks)
- Write: the R0/R1/R−1 table, the deletion of 70 and 60 from the start tree, and the height-shrinking argument.