AVL Trees

How to Draw an AVL Tree in Data Structures

Draw an AVL tree by inserting keys like a binary search tree, checking balance factors, and rotating at any node that reaches plus or minus 2.

On this page
  1. Short answer
  2. Step 1: Insert the key like a binary search tree
  3. Step 2: Check balance factors on the way up
  4. Step 3: Choose the rotation
  5. Worked example: insert 10, 20, 30, 40, 50, then 25
  6. Checking your drawing
  7. Common mistakes
  8. Summary

Short answer

To draw an AVL tree, insert each key as you would in a binary search tree. Then check balance factors on the way back up, and rotate at the first node where a balance factor reaches +2 or -2.

Step 1: Insert the key like a binary search tree

Start at the root. Move left for a smaller key and right for a larger one, until you reach an empty spot. Attach the new key there as a leaf.

Step 2: Check balance factors on the way up

The balance factor of a node is the height of its right subtree minus the height of its left subtree. Write it next to each node you check. Start at the new leaf and move up toward the root. Stop at the first node with a balance factor of +2 or -2. That is the node you rotate at.

Step 3: Choose the rotation

Look at the unbalanced node and at the child on the taller side. After an insertion, that child’s balance factor is always +1 or -1.

  • Balance +2, right child +1: rotate left at the unbalanced node. This is the right-right case.
  • Balance +2, right child -1: rotate right at the child, then left at the unbalanced node. This is the right-left case.
  • Balance -2, left child -1: rotate right at the unbalanced node. This is the left-left case.
  • Balance -2, left child +1: rotate left at the child, then right at the unbalanced node. This is the left-right case.

Worked example: insert 10, 20, 30, 40, 50, then 25

After 10, 20, and 30

Insert 10, then 20 as its right child. Then insert 30 as the right child of 20. Going up from 30, the node 20 is fine, but 10 has a balance factor of +2. Its right child, 20, has balance +1, so this is the right-right case. A single left rotation at 10 puts 20 at the top.

Before the rotation:

10
  \
   20
     \
      30

After the rotation:

    20
   /  \
 10    30

After 40 and 50

Insert 40 as the right child of 30. Every balance factor is still in range. Node 30 has +1, and node 20 has +1.

    20
   /  \
 10    30
         \
          40

Insert 50 as the right child of 40. Going up from 50, node 40 is fine, but node 30 now has a balance factor of +2. Its right child, 40, has balance +1, so this is again the right-right case. A single left rotation at 30 moves 40 up.

Before the rotation:

    20
   /  \
 10    30
         \
          40
            \
             50

After the rotation:

      20
     /  \
   10    40
        /  \
      30    50

After 25

Insert 25 as the left child of 30. Going up from 25, node 30 has a balance factor of -1, and node 40 has -1. Both are fine. Node 20 now has +2, because its right subtree has height 3 and its left subtree has height 1. The right child of 20 is 40, and it has balance -1, which leans the other way. So this is the right-left case: rotate right at 40, then left at 20.

Before the rotations:

    20
   /  \
 10    40
      /  \
    30    50
   /
 25

After the right rotation at 40, node 30 moves up to take its place. After the left rotation at 20, node 30 moves to the top. The final tree is:

        30
      /    \
    20      40
   /  \        \
 10    25      50

Checking your drawing

  • Read the keys in order, left subtree first, then the node, then the right subtree. They should come out sorted: 10, 20, 25, 30, 40, 50.
  • Recompute every balance factor. Each one should be -1, 0, or +1.
  • Confirm the root. In the final tree it is 30.

Common mistakes

  • Rotating at the new node instead of at the first unbalanced node on the way up.
  • Forgetting to update heights or balance factors after a rotation.
  • Confusing single and double rotations. If the child leans the opposite way from its parent, you need a double rotation.
  • Drawing the tree without labeling balance factors, which makes errors hard to spot.

A heap is also drawn as a tree, but it follows different ordering rules, so these steps do not apply to it. If you are studying heaps next, what is a heap data structure covers their rules.

Summary

Insert the key as in a binary search tree, check balance factors upward, and rotate at the first node that reaches +2 or -2. Confirm the result with an in-order read and a final check of every balance factor.

The complete guide to avl trees

AVL Trees in Data Structures
Read the guide

Keep exploring

Guides from other topics

All topics →

Recursive Algorithm Examples

Recursive algorithm examples in Python: factorial, binary search, tree traversal, Tower of Hanoi, and merge sort, with base cases explained for each.

Recursion 7 min read