An AVL tree as a binary search tree that refuses to get lopsided. The rule is simple: for every node, the left and right sides can differ in height by no more than 1. That difference is the balance factor (left height minus right height), and it has to stay at -1, 0, or +1. If an insert or delete pushes any node to +2 or -2, the tree steps in and fixes itself with rotations.

How the balancing works
Insert or delete just like you would in a normal BST.
Walk back up toward the root, updating heights as you go.
Spot the lowest node whose balance factor has gone out of range.
Work out which of the four cases it is.
Apply the matching rotation, and you’re done.
The four cases
Left-Left (LL): The new node went into the left side of the left child. One right rotation sorts it out.
Right-Right (RR): The new node went into the right side of the right child. One left rotation does it.
Left-Right (LR): The new node went into the right side of the left child. Here you need two moves: rotate the left child to the left, then rotate the unbalanced node to the right.
Right-Left (RL): The new node went into the left side of the right child. Again two moves: rotate the right child to the right, then rotate the unbalanced node to the left.
Let’s see it in action
Insert 30, then 20, then 10. The tree leans left twice, so node 30 reaches +2. That’s an LL case. Rotate right around 30, and now 20 sits at the top with 10 on its left and 30 on its right. Balanced.
Now try inserting 30, then 10, then 20. Node 30 is unbalanced again, but this time the path bends: left, then right. That’s an LR case. First rotate left around 10 to straighten the bend, then rotate right around 30. Once again, 20 becomes the root.
Single vs. double rotation
Single rotation | Double rotation | |
|---|---|---|
Cases | LL and RR | LR and RL |
Shape | Straight line | Zigzag |
Steps | One rotation | Two rotations |
Idea | Rotate the unbalanced node directly | Straighten the bend first, then rotate |
Here’s an easy way to remember it: if the heavy side goes in the same direction twice, one rotation is enough. If it changes direction, you need two, because a single rotation would only shift the problem to the other side rather than fix it.
Why all this effort?
A regular BST can collapse into something like a linked list in the worst case, which makes search, insert, and delete take O(n) time. An AVL tree keeps its height at O(log n), so all three operations stay quick. Each rotation takes constant time, and an insertion needs at most one single or double rotation to restore balance.
Bottom line
After every insert or delete, check the balance factors, find the lowest unbalanced node, and look at its shape. A straight line means a single rotation, and a zigzag means a double. Once you can recognize the shape, the rest is just following steps, which makes this a very scorable topic in exams and interviews.


