MCS-208 · Unit 7

Advanced Trees

Section 3 of 9

Block 3 · Unit 7Advanced Trees10 min · Section 3 of 9

7.3AVL Trees and Rotations

Covers IGNOU MCS-208 §7.3, 7.3.3

An AVL tree is a BST that keeps itself balanced after every insertion and deletion, so its height is always O(log⁡n)O(\log n). It was the first such tree, invented by Adelson-Velsky and Landis in 1962.

Balance

The balance factor of a node is bf=h(right)−h(left)\text{bf} = h(\text{right}) - h(\text{left}) (some books use left − right). In an AVL tree every node has bf −1-1 (left-high), 00 (balanced) or +1+1 (right-high). After an insertion or deletion a node may reach ±2\pm 2; a rotation then repairs it.

An AVL tree with nn nodes has height less than about 1.44log⁡2(n+2)1.44 \log_2 (n + 2), so search, insertion and deletion are O(log⁡n)O(\log n) in the worst case.

Rotations

A rotation changes the shape of a small part of the tree but keeps the inorder (sorted) order of the keys. Let kk be the lowest node with bf =±2= \pm 2. The four cases are named after the path from kk to the inserted node:

CaseWhere the new node wentFix
LLLeft subtree of kk‘s left childSingle right rotation at kk
RRRight subtree of kk‘s right childSingle left rotation at kk
LRRight subtree of kk‘s left childLeft rotation at the child, then right rotation at kk (double rotation)
RLLeft subtree of kk‘s right childRight rotation at the child, then left rotation at kk (double rotation)
Single right rotation (LL case) at k2
        k2                  k1
       /  \               /    \
      k1   C     ==>     A      k2
     /  \                      /  \
    A    B                    B    C
(A is too tall)        (B moves across to k2)

The left rotation (RR case) is the mirror image. A double rotation is two single rotations: for LR, first rotate left at k2k_2‘s left child, which turns the bend into a straight line, then rotate right at k2k_2.

Node structure

AVL node
struct avl {
    struct avl *left;      /* pointers to the same structure type */
    int info;
    int bf;                /* balance factor (or store the height) */
    struct avl *right;
};

Key takeaways

  • AVL: heights of the two subtrees differ by at most 1 at every node.
  • bf =h(right)−h(left)∈{−1,0,+1}= h(\text{right}) - h(\text{left}) \in \{-1, 0, +1\}; ±2\pm 2 triggers a rotation.
  • LL → right rotation; RR → left rotation; LR and RL → double rotations.
  • Height <1.44log⁡2(n+2)< 1.44 \log_2 (n + 2), so all operations are O(log⁡n)O(\log n) worst case.

Practice questions

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

Question 1 · multiple choice

A node’s left subtree has height 3 and its right subtree height 1. Using bf =h(R)−h(L)= h(R) - h(L), what is its balance factor, and is the node AVL-balanced?

Show answer

Answer: B. −2; not balanced

1−3=−21 - 3 = -2, outside {−1,0,+1}\{-1, 0, +1\}. A left-heavy node has a negative factor in this convention.

Question 2 · multiple choice

A node becomes unbalanced after a key is inserted into the right subtree of its left child. Which case is this?

Show answer

Answer: C. LR

The path from the unbalanced node goes left, then right. It needs a double rotation (left at the child, then right at the node).

Question 3 · multiple choice

Which rotation fixes an RR imbalance at node kk?

Show answer

Answer: A. A single left rotation at kk

The right side is too tall in a straight line, so kk‘s right child moves up with one left rotation. The double rotation is for RL.

Question 4 · multiple choice

Keys 10, 20, 30 are inserted into an empty AVL tree. What is the result?

Show answer

Answer: D. 20(10, 30)

After 30, node 10 has bf +2 (RR case); a left rotation at 10 makes 20 the root. The first option is the unbalanced chain before the rotation.

Question 5 · multiple choice

Why is the AVL tree guaranteed to have O(log⁡n)O(\log n) height?

Show answer

Answer: C. Because every node’s subtrees differ in height by at most 1, which forces the tree to be bushy

The balance rule means a tree of height hh needs at least about 1.618h1.618^h nodes, so h=O(log⁡n)h = O(\log n). An AVL tree need not be complete.

Question 6 · short answer

Insert 30, 10, 20 into an empty AVL tree, showing the balance factors and the rotation(s) used.

Show worked answer

Insert 30: bf(30) = 0. Insert 10 (left of 30): bf(30) = −1. Insert 20: 20 < 30, go left to 10; 20 > 10, so 20 is the right child of 10. Now bf(10) = +1 and bf(30) = −2: unbalanced at 30, path left then right: LR case. Double rotation: left rotation at 10 gives 30(20(10, -), -), then right rotation at 30 gives 20(10, 30). All balance factors are 0.

↑ ↓ move↵ openTab switch type