Skip to the content
Software Made Clear Diagrams that show the mechanism About

VISUALISERS · FOUNDATIONS

← All visualisers

AVL tree visualiser

The AVL tree answers what does keeping it balanced actually buy?

Exercise: compare insertion orders

Predict both heights in node levels before each illustrative load (a single node has height 1), then compare the current result below.

  1. 30, 10, 20: predict the AVL root and which lower node turns first.
  2. 20, 10, 30: same values, another order. Predict the heights and rotation.
  3. 30, 10, 20, 40: predict the final rotation message after adding 40.
Use or share an insertion sequence

Replace the values and choose Load sequence. Both trees receive the same insertion order.

Up to 12 distinct whole numbers from 1 to 999, separated by commas. Blank starts an empty pair. Load sequence replaces both trees.

Current insertion sequence: 10, 20, 30, 40, 50.

The starting sequence is shown. Load your values or copy a link to the drawing.

5 values, in this insertion order: the plain tree stands 5 levels tall, the AVL tree 3. Inserting 50 unbalanced the AVL tree, so it rotated: right-right, rotated left.

Check the three answers

This static key describes the named sequences, rather than whatever is currently drawn.

  1. 30, 10, 20: plain height 3, AVL height 2. Inserting 20 needs a left-right repair: rotate left at 10, then right at 30. The AVL root is 20, with children 10 and 30.
  2. 20, 10, 30: plain height 2, AVL height 2. The final insertion needs no rotation. The AVL has the same final shape as the first example, reached without that repair.
  3. 30, 10, 20, 40: plain height 3, AVL height 3. Inserting 20 already caused the left-right repair; inserting 40 needs none. Equal final heights do not imply the same history.

The rotations preserve sorted order: the first two examples still contain 10, 20, 30; the third also contains 40.

Loading reports only the final insertion, not every earlier pointer change. Without JavaScript, the drawing stays at the starting five-value example; the static key remains readable. Heights are not speed measurements.

Use Copy example link after loading. Open it in a fresh tab, or share that exact insertion order with a partner.

Whole numbers 1–999; no duplicates.

10203040502010403050Plain search treeHeight 5AVL treeHeight 31020304050Plain search treeHeight 52010403050AVL treeHeight 3

PLAIN TREE HEIGHT

5

AVL TREE HEIGHT

3

Each press of “Add values in order” appends a short run of ascending values, so the plain tree gains a level per value while the AVL tree rotates and stays close to log n. Three presses fill the figure at twelve values; after that the control reports that it is full instead of inserting, and Reset starts the sequence again.

What a rotation actually does

After every insert the tree checks one thing: does any node now have one side more than a level taller than the other? If so, that node is rotated — its subtrees are re-hung so the taller side gives a level back.

Which way it turns depends on where the new value landed. Two levels down the same side, left-left or right-right, is one turn against that side. Down one side and back the other, left-right or right-left, needs two: the lower node is turned first to make the imbalance straight, then the upper one is turned as before.

That is the whole difference between the two trees above. Both hold the same values in the same order; only one of them pays a few pointer swaps per insert to keep its height near the minimum.

Want an explanation to go with the picture? Find a guided starting point →