AVL Tree Simulator

An AVL Tree is a self-balancing Binary Search Tree. Every node stores a subtree height, and the balance factor height(left) − height(right) must remain −1, 0, or 1.

SearchO(log n)
InsertO(log n)
DeleteO(log n)
Balance factor−1, 0, or 1

Tree operations

400 ms

Example AVL Tree loaded. Choose an operation.

Nodes0
Height0
Maximum |BF|0
Rotations0

Operation path

—

Rotation history

  1. No rotation was required.

AVL repair cases

LL: rotate right
RR: rotate left

LR:
  rotate left on child
  rotate right on node

RL:
  rotate right on child
  rotate left on node

BF = height(left) - height(right)

AVL concepts

Stored heights

Each node stores its subtree height. Heights are updated after recursive insertion, deletion, and every rotation.

Single rotations

LL imbalance uses a right rotation. RR imbalance uses a left rotation.

Double rotations

LR and RL imbalances require two rotations because the heavy child leans in the opposite direction.

Deletion repair

Deletion can unbalance several ancestors, so balance checks continue all the way back to the root.

AVL invariants

InvariantRequirement
BST orderingLeft values are smaller; right values are larger.
Correct stored height1 + max(left height, right height)
BalanceAbsolute balance factor is at most 1.
Duplicate policyDuplicate keys are rejected.