AlgoScope

Huffman Coding

AlgorithmintermediateGreedy

Repeatedly merge the two least frequent symbols; the merge tree gives prefix-free codes.

Decision · step 2 of 15Huffman Coding: The classic: five characters, very skewed
a:5b:2c:1d:1r:2

There are 5 roots: c:1, d:1, b:2, r:2, a:5. The two lightest are c:1 and d:1, so they merge. Burying a root one level deeper costs one extra bit for every character under it, which is why the lightest go first.

Open in the player →or start at step 2

What you will see

A forest of weighted leaves; the two lightest roots merge into a parent until one tree remains.

How huffman coding works →

Cost

BestO(n log n)
AverageO(n log n)
WorstO(n log n)
SpaceO(n)

How you work with it here

play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next.

Screen readers: Candidates announce their key and state (candidate, chosen, rejected); each step announces the local choice and its consequence.

Reduced motion: Chosen and rejected states crossfade; no travelling emphasis.