Lesson 23 +35 XP

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

  1. Insert the key as in a BST (as a new leaf).
  2. Walk back up from the new leaf, recomputing BF = h(left) − h(right) of each ancestor.
  3. Stop at the first ancestor with BF = +2 or −2. Call it Z (the pivot).
  4. Read the first two moves from Z towards the new key: L-L, R-R, L-R or R-L.
  5. 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.
  6. 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.

  1. Trace 1 - Numbers: 50, 20, 60, 10, 8, 15, 32, 46, 11, 48
InsertImbalanceRotationTree after the step
50--50
20--50(20, −)
60--50(20, 60)
10--50(20(10, −), 60)
8BF(20) = +2, path L-LLL: right at 2050(10(8, 20), 60)
15BF(50) = +2, path L-RLR: left at 10, right at 5020(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))
48BF(32) = −2, path R-RRR: left at 3220(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.

DIAGRAM
 Before fixing (insert 15)             After LR rotation

            50 (+2)                           20
           /     \                          /    \
       10 (-1)    60                      10      50
       /   \                             /  \       \
      8     20                          8    15      60
           /
         15

Final tree (levels: 20 / 10 50 / 8 15 46 60 / 11 32 48):

DIAGRAM
                  20
              /        \
            10          50
           /  \        /   \
          8    15     46    60
              /      /  \
            11      32   48

Every 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).

InsertImbalanceRotationTree after the step
Mar--Mar
May--Mar(−, May)
NovBF(Mar) = −2, R-RRR at MarMay(Mar, Nov)
Aug--May(Mar(Aug, −), Nov)
AprBF(Mar) = +2, L-LLL at MarMay(Aug(Apr, Mar), Nov)
JanBF(May) = +2, L-RLR at MayMar(Aug(Apr, Jan), May(−, Nov))
Dec--Mar(Aug(Apr, Jan(Dec, −)), May(−, Nov))
July--Mar(Aug(Apr, Jan(Dec, July)), May(−, Nov))
FebBF(Aug) = −2, R-LRL at AugMar(Dec(Aug(Apr, −), Jan(Feb, July)), May(−, Nov))
JuneBF(Mar) = +2, L-RLR at MarJan(Dec(Aug(Apr, −), Feb), Mar(July(−, June), May(−, Nov)))
OctBF(May) = −2, R-RRR at MayJan(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:

DIAGRAM
                    Jan
              /             \
           Dec               Mar
          /    \           /     \
        Aug    Feb      July      Nov
        /                  \     /    \
      Apr                 June  May    Oct
                                          \
                                          Sep

Inorder: Apr Aug Dec Feb Jan July June Mar May Nov Oct Sep - sorted ✓

  1. Trace 3 - Letters: B H A G W N P R S U M D E L I
InsertRotationInsertRotation
B, H, A, G, WnoneSLR at W
NRR at BURR at P
PLR at WMRL at H
RnoneDnone
ELR at GLLL at N
Inone

Final tree (levels: P / H S / B M R W / A E L N U / D G I):

DIAGRAM
                     P
               /           \
             H               S
          /     \          /   \
        B         M       R     W
       / \       / \           /
      A   E     L   N         U
         / \   /
        D   G I

5. 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 childRotation
+20 or +1Right rotation (R0 / R1 - like LL)
+2−1Left-right (R−1 - like LR)
−20 or −1Left rotation (L0 / L−1 - like RR)
−2+1Right-left (L1 - like RL)

Start tree (built by inserting 50, 20, 60, 10, 30, 70, 5, 15, 25, 40, 35):

DIAGRAM
                   30
              /          \
            20             50
           /  \          /    \
         10    25      40      60
        /  \          /          \
       5    15      35            70

Delete 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).

DIAGRAM
 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   15

Delete 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:

DIAGRAM
            35
          /    \
        20      40
       /  \       \
      10   25      50
     / \
    5   15

BF(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

MistakeCorrect approach
Rotating at the lowest unbalanced node you notice first from the topFix the first unbalanced ancestor found walking up from the new leaf
Naming the case from the root of the whole treeName 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 factorsMark BF at every node after each rotation - examiners look for it
💡 Tip

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.

🎯 END-TERM EXAM PREP

📝 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.