MCS-208 · Unit 7

Advanced Trees

Section 4 of 9

Block 3 · Unit 7Advanced Trees12 min · Section 4 of 9

7.4AVL Insertion, Deletion and Applications

Covers IGNOU MCS-208 §7.3.1, 7.3.2, 7.3.4

With rotations available, AVL insertion and deletion are ordinary BST operations followed by a walk back up the search path, checking and fixing balance factors.

Insertion

  1. Insert the key as in a plain BST (it becomes a leaf).
  2. Go back up the path towards the root, updating each node’s height (or balance factor).
  3. At the first node with bf =±2= \pm 2, apply the matching rotation (LL, RR, LR or RL).

After an insertion, at most one single or double rotation is needed: it restores the subtree to its height before the insertion, so the nodes above are balanced again.

Deletion

  1. Delete the key as in a BST (for two children, use the inorder successor or predecessor).
  2. Go back up the path from the removed node to the root, updating heights.
  3. Rotate at every node that reaches bf =±2= \pm 2.

Unlike insertion, a deletion may need rotations at several levels, because a rotation after a deletion can make the subtree one level shorter, which may unbalance its parent. Each step is O(1)O(1) and there are O(log⁡n)O(\log n) levels, so deletion is still O(log⁡n)O(\log n).

Applications and trade-offs

AVL trees suit applications that search much more often than they update, because they are the most strictly balanced of the common search trees (shortest height), while insertion and deletion pay for rotations:

  • in-memory dictionaries, symbol tables in compilers, telephone directories;
  • indexes where data arrives already sorted (a plain BST would degenerate into a chain);
  • databases and file systems that need guaranteed O(log⁡n)O(\log n) look-up for data that fits in memory.

When updates are frequent, red-black trees (fewer rotations) are often preferred; for data on disk, B-trees are used.

Key takeaways

  • Insert: BST insert, update heights upwards, at most one (single or double) rotation.
  • Delete: BST delete, update heights upwards, possibly a rotation at each level.
  • Both are O(log⁡n)O(\log n) in the worst case.
  • AVL trees are best when searches far outnumber updates.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

After inserting one key into an AVL tree, how many rotation steps (single or double) can be needed at most?

Show answer

Answer: A. One

The rotation restores the subtree’s original height, so nothing above it changes. Deletion is the operation that may need O(log⁡n)O(\log n) rotations.

Question 2 · multiple choice

In the example, inserting 80 caused an RL case at 70. Why?

Show answer

Answer: B. 80 went into the left subtree of 70’s right child (90).

80 > 70 (go right to 90), 80 < 90 (go left): right then left = RL, fixed by a double rotation.

Question 3 · multiple choice

Keys 1, 2, 3, 4, 5, 6, 7 are inserted in order into an AVL tree. What is its final height (levels)?

Show answer

Answer: C. 3

The repeated RR rotations produce the perfect tree 4(2(1, 3), 6(5, 7)), with 3 levels. A plain BST would have height 7.

Question 4 · multiple choice

For which workload is an AVL tree the best of the trees in this unit?

Show answer

Answer: B. Many searches and few insertions or deletions, all in memory

AVL trees are the most tightly balanced, so searches are fastest; disks favour B-trees, frequent updates favour red-black trees, and repeated accesses favour splay trees.

Question 5 · multiple choice

What does this program print?

C · q7-height.c
#include <stdio.h>

int main(void)
{
    /* minimum number of nodes N(h) in an AVL tree of height h:
       N(1) = 1, N(2) = 2, N(h) = N(h-1) + N(h-2) + 1            */
    int n[8] = {0, 1, 2};
    for (int h = 3; h < 8; h++)
        n[h] = n[h - 1] + n[h - 2] + 1;
    printf("%d %d %d\n", n[3], n[5], n[7]);
    return 0;
}
Show answer

Answer: B. 4 12 33

N(3) = 2 + 1 + 1 = 4, N(4) = 7, N(5) = 12, N(6) = 20, N(7) = 33. These grow like Fibonacci numbers, which is why AVL height is O(log⁡n)O(\log n). 7 31 127 are the counts for perfect trees.

Question 6 · short answer

Construct an AVL tree by inserting 40, 20, 10, 25, 30, 22 one at a time, naming each rotation.

Show worked answer

40; 40(20); insert 10: bf(40) = −2, LL → right rotation at 40: 20(10, 40). Insert 25: 20(10, 40(25, -)), balanced. Insert 30: goes 20 → 40 → 25 → right; bf(40) = −2 with path left (25) then right (30): LR at 40 → left rotation at 25, right rotation at 40: 20(10, 30(25, 40)). Insert 22: goes 20 → 30 → 25 → left; bf(20) = +2, path right (30) then left (25): RL at 20 → right rotation at 30, then left rotation at 20: 25(20(10, 22), 30(-, 40)). Final balance factors: 25: 0, 20: 0, 30: +1, others 0.

↑ ↓ move↵ openTab switch type