ABSTRACT

Named after Adelson-Velsky and Landis, the AVL Tree is a self-balancing BST that ensures a worst-case time complexity of for search, insertion, and deletion. It achieves this by maintaining a strict balance property through structural rotations.


1. Core Properties

An AVL tree must satisfy the Balance Condition: For every node , the height of its left and right subtrees can differ by at most 1.

  • Balance Factor ():
  • Validity: A node is balanced if .
Valid AVL TreeInvalid AVL Tree

2. Mathematical Proof of Height

We can prove the worst-case height is by finding the minimum number of nodes required to form an AVL tree of height .

  • Recurrence:
  • Base Cases: , (using a 1-based height definition).
  • Result: This recurrence grows faster than the Fibonacci sequence. Since (where is the golden ratio), it follows that , confirming .

3. Rebalancing: AVL Rotations

When an insertion or removal causes a node to have a of , rotations are used to restore balance.

Single Rotations (The “Straight Line” Case)

Used when the imbalance is caused by an insertion into the “outside” child (Left-Left or Right-Right).

  • Right Rotation: Fixes Left-Left imbalance.
  • Left Rotation: Fixes Right-Right imbalance.

AVLRight(b): // Perform a right AVL rotation on node b
    a = left child of b
    y = right child of a (or NULL if a does not have a right child)
    p = parent of b (or NULL if b does not have a parent)
    if p is not NULL and b is the right child of p:
        make a the right child of p
    otherwise, if p is not NULL and b is the left child of p:
        make a the left child of p
    make y the left child of b
    make b the right child of a

AVLLeft(a): // Perform a left AVL rotation on node a
    b = right child of a
    y = left child of b (or NULL if b does not have a left child)
    p = parent of a (or NULL if a does not have a parent)
    if p is not NULL and a is the right child of p:
        make b the right child of p
    otherwise, if p is not NULL and a is the left child of p:
        make b the left child of p
    make y the right child of a
    make a the left child of b

Double Rotations (The “Kink” Case)

Used when the imbalance is caused by an insertion into the “inside” child (Left-Right or Right-Left). A single rotation cannot fix a “kink” shape.

Solution: Double Rotations:

  1. First Rotation: Rotate the child to transform the “kink” into a “straight line.”
  2. Second Rotation: Rotate the parent to restore overall balance.

DoubleAVLLeftKink(a): // Shape: <
    AVLLeft(a.leftChild)
    AVLRight(a)

DoubleAVLRightKink(a): // Shape: >
    AVLRight(a.rightChild)
    AVLLeft(a)

4. Basic Operations

BST operations in an AVL tree are enhanced with a “rebalancing” phase to ensure the height is maintained.

find(element)

  • Logic: Identical to the standard Binary Search Tree find algorithm.
  • Process: Compare the target to the current node; traverse left if smaller or right if larger.
  • Complexity: Guaranteed because the tree height is strictly controlled.

insert(element)

  • BST Phase: Perform a standard BST insertion to place the new node as a leaf.
  • Update Phase: Traverse upwards from the new leaf toward the root.
  • Balance Phase: At each ancestor, update the balance factor. If any node reaches a factor of , perform the appropriate Single or Double Rotation.
  • Complexity: for the initial search plus for the upward rebalancing pass.

remove(element)

  • BST Phase: Perform standard BST removal (handling the 0, 1, or 2-child cases).
  • Update Phase: Start at the parent of the physically removed node and traverse upwards to the root.
  • Balance Phase: Check balance factors at every level. Unlike insertion (where one rotation fix is often enough), removal may require multiple rotations along the path to the root.
  • Complexity: for both the search and the potential multi-step rebalancing pass.


5. Performance Comparison

FeatureStandard BSTAVL Tree
Average Search
Worst Search
Balancing StrategyNoneHeight-based (Strict)
Passes per Update1 (Down)2 (Down and Up)

NOTE

While AVL trees provide the best lookup times due to their strict balance, they require two passes (one down to find/insert, one up to rebalance). The Red-Black Tree is often preferred in high-performance libraries because it can rebalance in a single pass.