AVL Tree Data Structure (original) (raw)

Last Updated : 20 Dec, 2025

An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one.

Balance Factor = left subtree height - right subtree height
For a Balanced Tree(for every node): -1 ≤ Balance Factor ≤ 1

Example of an AVL Tree:

The balance factors for different nodes are: 12 : +1, 8 : +1, 18 : +1, 5 : +1, 11 : 0, 17 : 0 and 4 : 0. Since all differences are lies between -1 to +1, so the tree is an AVL tree.

Example-of-an-AVL-Tree-11

Example of a BST which is not an AVL Tree:

The Below Tree is **not an AVL Tree as the balance factor for nodes **8 and 12 is more than **1.

Example-of-an-AVL-Tree-22

Important Points about AVL Tree:

Operations on an AVL Tree:

Please refer Insertion in AVL Tree and Deletion in AVL Tree for details.

Rotating the subtrees (Used in Insertion and Deletion)

An AVL tree may rotate in one of the following four ways to keep itself balanced while making sure that the BST properties are maintained.

1. **Left-Left Case:

**2. **Right-Right Case:

3. **Left-Right Case:

**4. **Right-Left Case:

Applications of AVL Tree:

  1. AVL Tree is used as a first example self balancing BST in teaching DSA as it is easier to understand and implement compared to Red Black
  2. Applications, where insertions and deletions are less common but frequent data lookups along with other operations of BST like sorted traversal, floor, ceil, min and max.
  3. Red Black tree is more commonly implemented in language libraries like map in C++, set in C++, TreeMap in Java and TreeSet in Java.
  4. AVL Trees can be used in a real time environment where predictable and consistent performance is required.

Advantages of AVL Tree:

  1. AVL trees can self-balance themselves and therefore provides time complexity as O(log n) for **search, **insert and **delete.
  2. As it is a balanced BST, so items can be traversed in sorted order.
  3. Since the balancing rules are strict compared to Red Black Tree, AVL trees in general have relatively less height and hence the search is faster.
  4. AVL tree is relatively less complex to understand and implement compared to Red Black Trees.

Disadvantages of AVL Tree:

  1. It is difficult to implement compared to normal BST.
  2. Less used compared to Red-Black trees. Due to its rather strict balance.
  3. AVL trees provide complicated insertion and removal operations as more rotations are performed.

**Related Articles: