Huffman coding gives frequent characters short codes and rare characters long
codes. In English text the space and the letter e appear far more often than
q or z, so a variable-length prefix code stores the same text in fewer bits.
No code is a prefix of another code, which is why the decoder can read the bit
stream left to right and always knows where one character ends and the next
begins. David Huffman invented the method in 1952 as a student at MIT.

The algorithm is greedy. It starts with one leaf per character, weighted by
how often the character appears, and repeatedly joins the two lightest trees
into a new tree whose weight is their sum. When only one tree is left, every
left branch is labelled 0 and every right branch 1, and the path from the
root to a leaf is that character's code. A priority queue (a binary min-heap)
makes each step fast, so building the tree takes O(n log n) time for n
distinct characters.

Huffman coding is used inside DEFLATE, the format behind ZIP files, PNG
images and HTTP compression, usually after another step called LZ77 that
replaces repeated phrases with short references to earlier text. On its own,
Huffman coding typically shrinks English text to a little over half its size.
