codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

15 / 65 topics
13Binary Search Trees (BSTs)14Balanced Binary Search Trees15AVL Trees16Red-Black Trees17B-Trees18Graphs Basics19Graph Representations (Adjacency List, Matrix)64AVL Trees Advantages and Disadvantages65Red-Black Trees Advantages and Disadvantages
Tutorials/Data Structures & Algorithms/AVL Trees
🧮Data Structures & Algorithms

AVL Trees

Updated 2026-04-20
3 min read

AVL Trees

AVL trees are a self-balancing binary search tree where the difference between heights of left and right subtrees cannot be more than one for all nodes. They were invented by Adelson-Velsky and Landis in 1962. The main advantage of AVL trees over other balanced BSTs like Red-Black Trees is that they are more strictly balanced, which leads to faster lookups.

Introduction

AVL trees ensure that the height of the tree remains logarithmic with respect to the number of nodes. This property ensures that operations such as search, insert, and delete can be performed in O(log n) time complexity, making AVL trees highly efficient for applications requiring frequent lookups.

Structure of an AVL Tree

An AVL tree is a binary search tree where each node has an additional attribute called balance factor. The balance factor of a node is defined as the height difference between its left and right subtrees:

  • Balance Factor = Height(Left Subtree) - Height(Right Subtree)

For an AVL tree to be balanced, the balance factor for every node must be either -1, 0, or +1.

Operations in AVL Trees

Insertion

Inserting a new key into an AVL tree is similar to inserting it into a binary search tree. However, after insertion, we need to check if the tree remains balanced by updating the balance factors and performing rotations if necessary.

Steps for Insertion:

  1. Perform standard BST insert.
  2. Update height and balance factor of all ancestors of the inserted node.
  3. If any ancestor becomes unbalanced (balance factor > 1 or < -1), perform rotations to restore balance.

Example Code:

class Node {
    constructor(key) {
        this.key = key;
        this.left = null;
        this.right = null;
        this.height = 1;
    }
}

function getHeight(node) {
    return node ? node.height : 0;
}

function getBalanceFactor(node) {
    return node ? getHeight(node.left) - getHeight(node.right) : 0;
}

function rightRotate(y) {
    const x = y.left;
    const T2 = x.right;

    // Perform rotation
    x.right = y;
    y.left = T2;

    // Update heights
    y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1;
    x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1;

    return x;
}

function leftRotate(x) {
    const y = x.right;
    const T2 = y.left;

    // Perform rotation
    y.left = x;
    x.right = T2;

    // Update heights
    x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1;
    y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1;

    return y;
}

function insert(node, key) {
    if (!node) return new Node(key);

    if (key < node.key) {
        node.left = insert(node.left, key);
    } else if (key > node.key) {
        node.right = insert(node.right, key);
    } else {
        // Duplicate keys are not allowed in BST
        return node;
    }

    // Update height of this ancestor node
    node.height = 1 + Math.max(getHeight(node.left), getHeight(node.right));

    // Get the balance factor of this ancestor node to check whether
    // this node became unbalanced
    const balance = getBalanceFactor(node);

    // If this node becomes unbalanced, then there are 4 cases

    // Left Left Case
    if (balance > 1 && key < node.left.key) {
        return rightRotate(node);
    }

    // Right Right Case
    if (balance < -1 && key > node.right.key) {
        return leftRotate(node);
    }

    // Left Right Case
    if (balance > 1 && key > node.left.key) {
        node.left = leftRotate(node.left);
        return rightRotate(node);
    }

    // Right Left Case
    if (balance < -1 && key < node.right.key) {
        node.right = rightRotate(node.right);
        return leftRotate(node);
    }

    // Return the (unchanged) node pointer
    return node;
}

Deletion

Deleting a node from an AVL tree involves standard BST deletion followed by rotations to maintain balance.

Steps for Deletion:

  1. Perform standard BST delete.
  2. Update height and balance factor of all ancestors of the deleted node.
  3. If any ancestor becomes unbalanced, perform rotations to restore balance.

Search

Searching in an AVL tree is identical to searching in a binary search tree, with O(log n) time complexity due to the balanced nature of the tree.

Rotations

Rotations are used to maintain the balance of the AVL tree after insertions or deletions. There are four types of rotations:

  1. Left Rotation: Used when there is a right-heavy subtree.
  2. Right Rotation: Used when there is a left-heavy subtree.
  3. Left-Right Rotation (LR): A combination of left and right rotations used to handle complex imbalance scenarios.
  4. Right-Left Rotation (RL): A combination of right and left rotations used to handle complex imbalance scenarios.

Best Practices

  1. Always maintain balance: After every insertion or deletion, ensure the tree remains balanced by performing necessary rotations.
  2. Use height attribute: The height attribute is crucial for calculating balance factors and determining when rotations are needed.
  3. Avoid duplicate keys: AVL trees do not allow duplicate keys, as they can disrupt the balance of the tree.

Conclusion

AVL trees are a powerful data structure that ensures efficient operations by maintaining strict balance. Understanding their properties, operations, and rotations is essential for implementing them in real-world applications. By following best practices and adhering to the principles of AVL trees, developers can create highly optimized solutions for scenarios requiring frequent lookups and dynamic updates.

References

  • "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein.
  • GeeksforGeeks: AVL Tree
  • Wikipedia: AVL Tree

This comprehensive guide provides a deep dive into AVL trees, covering their structure, operations, and best practices. By mastering these concepts, you'll be well-equipped to implement and utilize AVL trees in your software projects.


PreviousBalanced Binary Search TreesNext Red-Black Trees

Recommended Gear

Balanced Binary Search TreesRed-Black Trees