Compression Demos

HuffmanCoding

Classic lossless data compression. Builds an optimal prefix code tree where frequently-used characters get shorter bit codes.

0 chars
FREQUENCY TABLE
HUFFMAN TREE
CODES
ENCODED BIT STRING
0
Unique Chars
0
Original (bits)
0
Encoded (bits)
Compression Ratio
0
Total Chars

How to use this tool

  1. Type or paste text into the Input Text box — the frequency table, tree, codes, and encoded bits recalculate instantly on every keystroke.
  2. Click hello world or Lorem ipsum to load sample text and see a complete tree without typing.
  3. Read the HUFFMAN TREE panel: leaf nodes show a character and its count, while internal nodes () show the combined frequency of their subtree.
  4. Look up each symbol's bit code in the CODES panel, then hit Copy Codes to copy the whole table to your clipboard.
  5. Compare the ENCODED BIT STRING against the stat grid — Original (bits) versus Encoded (bits) and the resulting Compression Ratio.
  6. Click Clear any time to reset the input and start over.

Why this tool is helpful

Learn how Huffman coding works

Watch a greedy algorithm give the shortest codes to the most frequent symbols and merge the rarest into a binary tree — exactly as described in algorithms textbooks.

Understand real-world compression

The same core algorithm powers JPEG, PNG, MP3, and ZIP (Deflate). See the encoding step those formats perform under the hood.

Visualize prefix codes

Confirm by eye that no code is a prefix of another, which is what lets a decoder split a bit stream back into symbols without delimiters.

Estimate compressed size

Compare Original (bits) (8 bits per character) with Encoded (bits) to see how much a given string actually shrinks.

Spot why compression fails

Short or uniform text yields little or no savings — and can even expand — which is a key reason real codecs combine Huffman with other techniques.

Stay private

Everything runs locally in your browser. Your text is never uploaded, stored, or sent to a server.

FAQ

What is Huffman coding?

It's a greedy algorithm for building an optimal prefix-free binary code. Symbols that occur more often get shorter bit sequences, minimizing the average number of bits per symbol for a given frequency distribution.

How does this tool build the tree?

It counts how many times each character appears, then repeatedly merges the two least-frequent nodes into a parent whose frequency is their sum, until one root remains. Left edges are labeled 0 and right edges 1; each character's code is the path from the root to its leaf.

Why is "Original (bits)" always 8 × the character count?

This tool measures the original size as 8 bits per character — the width of an ASCII byte. The Compression Ratio stat shows the encoded size as a percentage of that 8-bit baseline.

Why does the compression ratio sometimes exceed 100%?

Huffman only wins when symbols are unevenly distributed. For very short or uniformly random text, the codes average out to roughly 8 bits or more per character, so the "compressed" size can equal or exceed the original.

What happens with a single unique character?

A one-symbol tree has no branch to build, so the tool assigns it the trivial code 0. The frequency table and tree show just that single character.

Why do spaces show as ␣ and newlines as \n?

To make invisible characters visible, the tree, frequency table, and code list render space as (U+2423), tab as \t, and line breaks as \n. The encoding itself still uses the real characters.

Does any of my text leave my browser?

No. All counting, tree building, and encoding happen locally in JavaScript. Nothing is uploaded, logged, or sent to a server.