Huffman coding generator

Type or paste a string to see how Huffman coding compresses it. You get the frequency and prefix code of each character, the tree drawn as text, the full encoded bit string, and how many bits it saves compared with 8-bit UTF-8.

Original size (bits, UTF-8)
88
Compressed (bits)
23
Compressed / original
26.14%
Average code length (bits per symbol)
2.09

Code table

SymbolCountCode
a50
b2110
r2111
c1100
d1101

Symbols are ordered by count. A space is shown as ␠, a line break as ↵ and a tab as ⇥. The size does not include the code table a decoder would need.

Huffman tree

● 11
├─0 a (5)  0
└─1 ● 6
   ├─0 ● 2
   │  ├─0 c (1)  100
   │  └─1 d (1)  101
   └─1 ● 4
      ├─0 b (2)  110
      └─1 r (2)  111

Encoded bits

01101110100010101101110

Runs in your browser: nothing you enter is sent anywhere.

How to use it

  1. Enter the text (up to 5,000 characters).
  2. Read the code table, the tree and the encoded bit string.
  3. Use Copy to take the full bit string; very long strings are cut on screen only.

How it works

Each distinct character becomes a leaf weighted by its count. The two lightest nodes are merged repeatedly. The first node taken becomes the 0 branch and the second the 1 branch. The code of a symbol is the path from the root to its leaf. The text is then replaced by the concatenation of these codes.

Example: aabbbc has a=2, b=3, c=1. c and a merge (weight 3); then b and that node merge. The codes are b = 0, c = 10, a = 11, and the text becomes 11 11 0 0 0 10, 9 bits instead of 48.

Tips and common mistakes

  • Texts with very uneven letter frequencies compress best; an even spread of symbols gives little gain.
  • A short text may not shrink once a table is added, which is why tiny files are rarely Huffman-coded.
  • The tree line ● 6 is an internal node with the total weight below it; leaves show the symbol, its count and its code.

Frequently asked questions

How does Huffman coding work?

It counts how often each symbol occurs, then repeatedly joins the two least frequent nodes into a new node whose weight is their sum, until one tree remains. Going left adds a 0 and right adds a 1, so frequent symbols get short codes and rare ones get long codes.

Why is the code table prefix-free?

Symbols sit only at the leaves of the tree, so no code is the start of another code. That lets a decoder read the bit stream without separators.

Are the codes unique?

No. When weights tie, other choices give different but equally short codes. This tool always picks the same one: nodes are ordered by weight, then by creation order (characters by code point first), and the first node taken becomes the 0 branch. The total length is the same for any valid choice.

Why is the compressed size not the real file size?

A decoder also needs the tree or the code table, and real formats add headers and pad to whole bytes. The numbers here count only the encoded bits against 8 bits per UTF-8 byte of the text.

What are symbols here?

Unicode code points, so an accented letter or emoji counts as one symbol. The original size counts its real UTF-8 bytes.

More in Developer

All tools in this category: Developer ·Updated November 3, 2026

Privacy choices

Google Analytics shows us which tools are used and whether they work. It may set cookies. Your choice is kept in this browser.