T Tsherniak Lab

Information theory side quest

Huffman Codes Lab

Build a real compression idea from scratch: count what shows up, grow a tree, and turn the shortest paths into the cheapest codes.

The idea before the name

A binary code is a dictionary from symbols to bits. For example, a code might say A = 0, B = 10, and N = 11.

The trick is that code lengths are allowed to differ. A letter that appears 6 times pays for its code 6 times, so saving one bit on that letter matters 6 times.

Goal: spend the fewest total bits, not the fewest different codes. total cost = count x code length

A Huffman code is the code you get from a specific greedy recipe: keep joining the two least common symbols, then read 0s and 1s from the tree. The result gives common symbols short codes while staying decodable.

MessageBANANA
Code ideaA=0, N=10, B=11
Encoded11 0 10 0 10 0

What you will build

  1. 1
    Count the letters that actually appear, because missing letters cost nothing.
  2. 2
    Merge the two smallest piles so rare letters get pushed farther away.
  3. 3
    Read each root-to-letter path as a bit code: left is 0, right is 1.
  4. 4
    Test that the final bitstream can be decoded without spaces or separators.

Checkpoint

Count letters

Ready

Advanced sections unlock later

After the walkthrough, this space will hold challenge mode, editable prefix codes, and the proof side quest.