Learn
Lesson · Data Structures · Search trees

AVL Insertion

After an insertion, at most one node is out of balance — the lowest unbalanced ancestor — and one of four cases applies. The outside cases need a single rotation, the inside cases need two, and one fix restores balance all the way to the root. Ten keys in increasing order give height 3 with six rebalances, against 9 for the plain tree on the same order.

after an insert, walk back up; fix the LOWEST unbalanced ancestor balance +2, key in the left child's LEFT subtree LL rotate right balance −2, key in the right child's RIGHT subtree RR rotate left balance +2, key in the left child's RIGHT subtree LR left, then right balance −2, key in the right child's LEFT subtree RL right, then left 1 … 10 in order: AVL height 3, 6 rebalances plain tree height 9
The four cases, and what one fix costs

Core ideas

What this lesson establishes

One decision procedure, one counting fact, and one thing that is genuinely surprising the first time: fixing a single node fixes the whole tree.

1

Only the ancestors of the new key can be unbalanced

An insertion changes the height of nothing except the nodes on the path from the new leaf to the root, because every other subtree is untouched. So the repair walks back up that path, and the node to fix is the lowest one whose balance factor has reached ±2.

2

The case is decided by two turns, not by the keys

Look at the direction from the unbalanced node to its taller child, and then from that child toward the new key. Same direction twice — left-left or right-right — and one rotation suffices. Different directions — left-right or right-left — and one rotation moves the problem sideways rather than fixing it, so two are needed.

3

One fix restores the whole tree

After the rebalance the subtree is exactly as tall as it was before the insertion, so every ancestor’s balance factor returns to what it was, and none of them needs anything. That is why an AVL insertion costs at most one single or one double rotation regardless of n, and it is a property of insertion specifically — deletion can need a rotation at every level, which is one reason this course states AVL deletion and does not develop it.

The material

The four cases, the double rotation, and the height against the plain tree

Why the inside cases need two rotations, why one fix is enough, and what the measured heights are on the same insertion order.

Insert the key exactly as in a plain search tree: walk to where the search fails and attach a leaf. Then walk back up the path updating heights. If some node’s balance factor reaches +2 or −2, rebalance there and stop. The lowest such node is the one to fix, and there is at most one of them.

The two outside cases

Suppose node z has balance +2 — its left subtree is two taller — and the new key went into the LEFT subtree of z’s left child. That is the LL case, and one right rotation at z fixes it: the left child rises, z drops to its right, and the two sides come out equal. RR is the mirror image, fixed by one left rotation.

LL, keys 30 20 10 one right rotation at 30 30 (+2) 20 (0) 20 10 30 10 RR, keys 10 20 30 one left rotation at 10 10 (−2) 20 (0) 20 10 30 30

The two inside cases, and why one rotation is not enough

Now suppose z has balance +2 and the new key went into the RIGHT subtree of z’s left child — the LR case. A single right rotation at z lifts the left child, but the offending subtree was on that child’s right, so it lands back on the left of z and the tree is now −2 the other way. The fix is to rotate the child left first, converting LR into LL, and then rotate z right.

LR, keys 30 10 20 30 (+2) rotate 10 LEFT: 30 then 30 RIGHT: 10 20 20 20 10 10 30 the grandchild 20 ends up as the root, which is what a double rotation always does: the INNER grandchild rises two levels RL, keys 10 30 20 rotate 30 RIGHT, then 10 LEFT same result

The lab names the case at every rebalance and animates it. On the three-key sequences above it reports LL, LR and RL respectively, each with one rebalance, each ending at the same tree 20 with children 10 and 30 — the same final shape reached three different ways, which is worth seeing because it makes the case analysis feel less arbitrary.

Theorem · One rebalance per insertion suffices

After an AVL insertion, rebalancing the lowest unbalanced ancestor restores the invariant everywhere.

Before the insertion the subtree rooted at that node had some height h. The insertion made it h + 1, which is what unbalanced it. Both the single and the double rotation return the subtree to height h. So from that node upward nothing has changed height at all, and every ancestor’s balance factor is what it was before the insertion — which was legal. Hence one fix, at most two rotations, regardless of how tall the tree is.

The measurement, on the input that breaks a plain tree

Insert 1 through 10 in increasing order — the order that turns a plain search tree into a path. The lab runs both:

keys 1 2 3 4 5 6 7 8 9 10, in order AVL plain BST height 3 9 rebalances 6 — comparisons 25 the six fixes, in order: RR at 3, RR at 5, RR at 6, RR at 7, RR at 9, RR at 10 height bound from N(h) at n = 10: N(3) = 7 ≤ 10, N(4) = 12 > 10 so h ≤ 3, and the tree achieves it

Every one of the six fixes is the RR case, because every key arrives larger than everything present. Height 3 against 9, on the input that is worst for the plain tree and costs the AVL tree six rotations — one per rebalance, since RR is a single rotation. The bound from the N(h) table is 3, and the tree reaches it.

A mixed order shows all four cases and a smaller gap. On the thirteen keys 50 25 75 10 5 30 27 60 90 80 70 98 62 the lab reports four rebalances — LL at 5, LR at 30, RR at 80, RL at 62 — giving height 3 against the plain tree’s 4, with 33 comparisons. The gap is one level, because that order was already reasonably balanced. Which is the caution: the measured benefit of balancing depends entirely on the order you measured it on, and the guarantee — h ≤ 1.44 log₂ n for every order — is the reason to pay for it.

Interactive

AVL insertion and the four cases

One fix at the lowest unbalanced ancestor restores the invariant everywhere

AVL insertion, with the case namedfour cases, two of which need two rotations
a double rotationbalance factors above each node

Method

Inserting into an AVL tree

Insert, then walk up, then name the case before rotating. Naming it first is what stops a double rotation being attempted as a single one.

1

Insert as an ordinary search tree, then walk back up

The key goes where the search failed. Update the height at each node on the way back to the root and watch the balance factors. Stop at the first node — the lowest — whose factor reaches ±2.

2

Name the case from two directions

From the unbalanced node to its taller child: left or right. From that child toward the new key: left or right. Same twice is LL or RR; different is LR or RL. The keys’ values play no part beyond telling you which way the walk went.

3

Rotate once for an outside case, twice for an inside one

LL: rotate right at the unbalanced node. RR: rotate left. LR: rotate the left child left, then the node right. RL: rotate the right child right, then the node left. In the double cases the inner grandchild ends up as the subtree root, which is the thing to check afterwards.

4

Stop, and verify you may

One fix is enough: the subtree is back to its pre-insertion height, so no ancestor has changed. Confirm it by checking the balance factor at the parent of where you rotated — it should be what it was before the insertion, and if it is not, the rotation was wrong.

Cases and counts

Score 0 / 0

Four keys that produce an inside case

The LR case in full, because it is the one people attempt as a single rotation. Keys arriving 30, 10, 20.

insert 30 30 balance 0 insert 10 30 (+1) still legal 10 insert 20 30 (+2) lowest unbalanced node: 30 10 from 30 to taller child: LEFT 20 from 10 toward 20: RIGHT → LR, a double rotation the single rotation that does NOT work: rotate 30 right 10 balance at 10 is now −2 30 the problem moved, unfixed 20 step 1: rotate 10 LEFT 30 now it is an LL case 20 10 step 2: rotate 30 RIGHT 20 balance 0 everywhere 10 30 in-order 10 20 30 throughout height 2 → 1 one rebalance, named LR

The failed single rotation is the useful half of this example. Rotating 30 right lifts 10, but 20 was hanging on 10’s right, so it comes along and lands under 30 again — the tree is now left-light instead of left-heavy and is still illegal. The first rotation of the double exists to move 20 onto the outside, where a single rotation can reach it.

The inner grandchild ends as the subtree root. That is true of every double rotation and is the quickest way to check one: in the LR case the right child of the left child rises two levels. If something else ended up on top, the two rotations were done in the wrong order.

For a faded rehearsal, insert 10, 30, 20 — the RL case, the mirror of this one. The supplied observation is that it must end at the same tree, 20 with children 10 and 30, because there is only one AVL tree on three keys. Name the two rotations and their nodes, then run the lab’s ascending preset and say why all six of its rebalances are the same case.

Common mistakes

1
Attempting an inside case with a single rotationRotating 30 right in the tree 30, 10, 20 lifts 10 and brings 20 along with it, leaving the tree unbalanced in the opposite direction. The first rotation of a double exists to move the offending subtree to the outside. The check afterwards is that the inner grandchild is now the subtree root.
2
Rebalancing the highest unbalanced node instead of the lowestWalk up from the new leaf and fix the first node that reaches ±2. Fixing a higher one first can leave the lower one illegal, and the argument that one fix suffices depends on the subtree returning to the height it had before the insertion — which is only true of the lowest such node.
3
Reading the measured height gap as the value of balancingOn increasing keys the gap is 3 against 9; on a reasonably mixed order it is 3 against 4. Both are real measurements and neither is the reason to use an AVL tree. The reason is the guarantee — h ≤ 1.44 log₂ n on every input, including the one you did not test — and a measured gap on a chosen order is not that.

Check your understanding

✓
Finish when you can name the case from two turns before touching a pointer.

You should be able to insert a key sequence by hand, identify the lowest unbalanced ancestor, name the case, perform the right number of rotations with the inner grandchild ending on top in the double cases, and say why one fix suffices for an insertion in terms of the subtree’s height.

Note: A balanced tree gives search, predecessor and an ordered walk in O(log n), and one extra integer per node gives it two more operations. “Augmenting a Tree” stores the subtree size at every node, gets rank and select in O(log n) from it, and shows the rotation updating the field — which is the part that goes stale if it is forgotten.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.