Huffman Coding
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
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.
What you will see
A forest of weighted leaves; the two lightest roots merge into a parent until one tree remains.
Cost
| Best | O(n log n) |
|---|---|
| Average | O(n log n) |
| Worst | O(n log n) |
| Space | O(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.