Browse curriculum

AVL Tree in Data Structure: Visualization

AVL tree in data structure: an AVL tree is a self-balancing binary search tree in which the heights of the left and right subtrees of every node differ by at most one. Step through it in the interactive AVL Tree visualization below.

READY

 

Insert values to watch the tree rebalance. Each node shows its balance factor (left height minus right height) which an AVL tree keeps within ±1.

 

Value

Intuition

A self-balancing Binary Search Tree (BST) named after its inventors Adelson-Velsky and Landis. It maintains O(log n) height by automatically performing rotations during insertion and deletion if the tree becomes unbalanced.

Concept

In a normal BST, skewed data (like 1, 2, 3, 4...) can degrade performance to O(n). AVL Trees fix this by tracking a Balance Factor (Left Height - Right Height) for every node. If |BF| > 1, it rotates to restore balance.

How AVL Tree Works

  • Balance Factor (BF): Height(Left) - Height(Right).
  • Valid BF: -1, 0, 1.
  • Rotations: Operations to fix imbalance:
    • LL Cases: Right Rotation.
    • RR Cases: Left Rotation.
    • LR Cases: Left then Right Rotation.
    • RL Cases: Right then Left Rotation.

AVL Tree Visualization, Step by Step

Insertion Logic:
  1. Insert node like standard BST.
  2. Backtrack up to root, updating heights.
  3. Check Balance Factor of each node.
  4. If Node becomes unbalanced (|BF| > 1):
    • Left-Left (LL): Perform Right Rotation.
    • Right-Right (RR): Perform Left Rotation.
    • Left-Right (LR): Left Rotate left child, then Right Rotate current.
    • Right-Left (RL): Right Rotate right child, then Left Rotate current.

When to Use

  • Database indexing where lookups must be fast O(log n).
  • Symbol tables in compilers.
  • When search operations are more frequent than insertions/deletions.

When NOT to Use

  • When insertions/deletions are very frequent (Red-Black trees might be slightly better due to fewer rotations).
  • Simple scenarios where O(n) worst case is acceptable.

How to Identify

Key requirements: "Balanced BST", "Guaranteed O(log n) search", "Rotations".

Practice it on interview problems

LeetCode problems solved with the Depth-First Search pattern, each with a step-by-step visualizer:

All Depth-First Search problems

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description

Compare its cost with every other algorithm in the Big O cheat sheet.

Frequently Asked Questions

What is an AVL Tree?

An AVL tree is a self-balancing Binary Search Tree where the difference between heights of left and right subtrees (Balance Factor) cannot be more than one for all nodes.


What is a Balance Factor?

The Balance Factor of a node is the height of its left subtree minus the height of its right subtree. It must be -1, 0, or 1 in a valid AVL tree.


AVL vs BST?

AVL trees are always balanced, ensuring O(log n) performance for all operations, unlike standard BSTs which can become skewed and O(n).