An AVL tree is a binary search tree that rebalances itself after each insertion and deletion, so its height always stays close to log n.

Definition and Balance Factor

An AVL tree, named after its inventors Adelson-Velsky and Landis (1962), is a binary search tree in which, for every node, the heights of the left and right subtrees differ by at most one. The balance factor of a node is BF = height(left subtree) − height(right subtree), and in an AVL tree every BF is −1, 0 or +1. If an insertion or deletion makes some BF equal to +2 or −2, the tree is rebalanced by rotations. As a result, the height of an AVL tree with n nodes is at most about 1.44 log₂ n, and search, insertion and deletion all take O(log n) in the worst case.

Definition: AVL tree. A self-balancing binary search tree in which the heights of the two subtrees of every node differ by at most one, restored by rotations after insertions and deletions.

Rotations

Let A be the nearest ancestor of the new node whose BF becomes ±2. There are four cases. LL case (inserted into the left subtree of A's left child): a single right rotation at A. RR case (into the right subtree of A's right child): a single left rotation at A. LR case (into the right subtree of A's left child): a left rotation at the left child, then a right rotation at A. RL case (into the left subtree of A's right child): a right rotation at the right child, then a left rotation at A. A rotation changes a few pointers, takes O(1) time, and keeps the BST order.

Insertion

To insert, do a normal BST insertion, then go back up the path to the root, updating the heights and balance factors. At the first node that is unbalanced, apply the right rotation. After one (single or double) rotation, the whole tree is balanced again.

Deletion

To delete, do a normal BST deletion, then go up the path updating balance factors and rotating at each unbalanced node. For BF +2, a left child with BF ≥ 0 needs a right rotation, and one with BF −1 needs a left-right rotation, with mirror rules for −2. A deletion may need several rotations.

Table 4.6: AVL rotation cases

Case Imbalance Fix
LL Left child's left subtree Right rotation
RR Right child's right subtree Left rotation
LR Left child's right subtree Left then right rotation
RL Right child's left subtree Right then left rotation

Example 4.6: Inserting into an AVL tree

Problem: Insert 10, 20, 30, 25, 28 into an empty AVL tree.

Solution:

  1. Insert 10, 20, 30: the tree leans right, BF(10) = −2. RR case: left rotation at 10 gives root 20 with children 10 and 30.
  2. Insert 25: it goes left of 30. All BFs are within limits.
  3. Insert 28: it goes right of 25. BF(30) = +2, and the new node is in the right subtree of 30's left child: LR case.
  4. Left rotation at 25, then right rotation at 30, makes 28 the parent of 25 and 30.

Result: The tree has root 20, left child 10, right child 28, and 28 has children 25 and 30.

#include <stdio.h>
#include <stdlib.h>
typedef struct N { int k, h; struct N *l, *r; } N;
int H(N *t) { return t ? t->h : 0; }
int mx(int a, int b) { return a > b ? a : b; }
void fix(N *t) { t->h = 1 + mx(H(t->l), H(t->r)); }
N *rr(N *y) {
    N *x = y->l;
    y->l = x->r;
    x->r = y;
    fix(y);
    fix(x);
    return x;
}
N *rl(N *x) {
    N *y = x->r;
    x->r = y->l;
    y->l = x;
    fix(x);
    fix(y);
    return y;
}
N *ins(N *t, int k) {
    if (!t) {
        t = calloc(1, sizeof *t);
        t->k = k;
        t->h = 1;
        return t;
    }
    if (k < t->k) t->l = ins(t->l, k); else t->r = ins(t->r, k);
    fix(t);
    int b = H(t->l) - H(t->r);
    if (b > 1 && k < t->l->k) return rr(t);
    if (b < -1 && k > t->r->k) return rl(t);
    if (b > 1) { t->l = rl(t->l); return rr(t); }
    if (b < -1) { t->r = rr(t->r); return rl(t); }
    return t;
}
void pre(N *t) {
    if (t) { printf("%d ", t->k); pre(t->l); pre(t->r); }
}
int main(void) {
    int a[] = {10, 20, 30, 25, 28};
    N *t = NULL;
    for (int i = 0; i < 5; i++) t = ins(t, a[i]);
    printf("preorder: "); pre(t); printf("\n");
    return 0;
}

Output

preorder: 20 10 28 25 30

Common mistakes

  • Computing the balance factor as right minus left in one place and left minus right in another.
  • Applying a single rotation to an LR or RL case, which does not balance the tree.
  • Stopping after one rotation during deletion, which may need more.

Key takeaways

  • In an AVL tree, every balance factor is −1, 0 or +1.
  • BF = height(left) − height(right).
  • LL and RR need single rotations; LR and RL need double rotations.
  • All AVL operations are O(log n).

Practice questions

  1. What is the balance factor of a node?
  2. Which rotation fixes an RR imbalance?
  3. Why are AVL trees preferred over plain BSTs?

Answers to the practice questions

  1. The height of its left subtree minus the height of its right subtree.
  2. A single left rotation.
  3. They stay balanced, so search, insertion and deletion are always O(log n).

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit IV