Huffman Coding Calculator

See how Huffman coding compresses text by giving common symbols short codes, and check homework or exam answers step by step.

S'executa al teu navegador. Els fitxers mai no surten del dispositiu. Mira com comprovar-ho

…

Com es fa

  1. 1
    Choose the input

    Text or frequencies.

  2. 2
    Enter the data

    One symbol and weight per line.

  3. 3
    Read the code table

    Codes and lengths.

  4. 4
    Compare with entropy

    Efficiency and redundancy.

  5. 5
    Check the saving

    Against a fixed-length code.

  6. 6
    View the bit stream

    For text input.

Sobre aquesta eina

Huffman coding is one of the foundations of data compression. Invented by David Huffman in 1952 as a student project, it gives each symbol a binary code whose length depends on how often the symbol appears: frequent symbols get short codes and rare ones long codes, so the total number of bits shrinks. Variants of it are still inside ZIP, gzip, PNG, JPEG and MP3 today. This calculator builds a Huffman code from either a piece of text, counting each character, or from a list of symbols with their frequencies or probabilities, as in textbook exercises. It shows the code table sorted by frequency with each symbol's count, probability, code and code length, then the average code length in bits per symbol, the Shannon entropy that sets the theoretical minimum, the efficiency and redundancy of the code, and how many bits the whole message takes compared with a fixed-length code. For text input it also shows the encoded bit stream, so you can see the compression happening.

How the algorithm works

1. Count how often each symbol occurs. 2. Treat each symbol as a small tree with its count as weight. 3. Repeatedly join the two lightest trees under a new node whose weight is their sum. 4. When one tree remains, label left branches 0 and right branches 1; each symbol's code is the path from the root to its leaf.

Why it works

No code is the beginning of another (the code is prefix-free), so a stream of bits can be decoded without separators. Huffman proved that no other symbol-by-symbol prefix code has a shorter average length.

Entropy and efficiency

Shannon entropy, H = −Σ p log₂ p, is the minimum average number of bits per symbol any code can achieve. A Huffman code's average length L always satisfies H ≤ L < H + 1. Efficiency is H ÷ L, and redundancy is L − H. Codes approach the entropy when probabilities are powers of one half.

Ties and different answers

When two weights are equal, the choice of which to join first is arbitrary. Different choices give different codes, but the average length is always the same minimum. If your textbook's codes differ from these, compare the lengths and average instead.

Limits

Huffman coding cannot give a symbol less than one bit, so it is inefficient for very skewed sources; arithmetic coding and ANS do better. Real compressors also model repeated phrases, which this calculator does not, so real files usually compress much better than these figures suggest.

Exemple resolt

The textbook frequencies A 45, B 13, C 12, D 16, E 9 and F 5 give A a 1-bit code and an average length of 2.24 bits per symbol against an entropy of about 2.22, an efficiency above 99% and a 25% saving over a 3-bit fixed-length code.

En resum

Preguntes freqüents

What is Huffman coding?

A way to give frequent symbols short binary codes to compress data.

Why are my codes different from my textbook?

Ties can be broken differently; the average length is still the same.

What is entropy?

The theoretical minimum average bits per symbol for the given probabilities.

Can I enter probabilities instead of counts?

Yes. Any positive weights work.

Is Huffman used today?

Yes, inside DEFLATE (ZIP, gzip, PNG) and JPEG, among others.

Is my text uploaded?

No. The code is built in your browser.

Totalment gratisSense cost, sense registre
Els fitxers es mantenen privatsTot funciona al teu navegador
Fàcil en el teu idiomaDisponible en molts idiomes