%3C/text%3E%3C/svg%3E">
💻
💻 Tech & IT

Huffman Code Generator

Results update as you type.

Your numbers

Understanding the Huffman code generator — concept and principle

Huffman coding assigns short bit strings to frequent characters and longer ones to rare characters, producing the shortest possible prefix code for a given set of symbol frequencies. This tool builds the code tree for your text, lists each character's code and counts the encoded length. Huffman coding is a building block of DEFLATE (ZIP, PNG, gzip) and baseline JPEG.

Huffman code treeMerging the rarest symbols first gives a the code 0 and b, r, c, d three-bit codes, so abracadabra needs 5 + 18 = 23 bits instead of 88.Huffman tree for "abracadabra"0101010111624a×5c×1d×1b×2r×2a0b110r111c100d10123 bits instead of 11 × 8 = 88 (26.14%)
Read the 0/1 labels from the top to a leaf: frequent a gets the shortest code.

How it is calculated

  1. Count how often each character appears.
  2. Put every character in a list as a leaf weighted by its count.
  3. Repeatedly remove the two lightest nodes and join them under a new node whose weight is their sum, until one tree remains.
  4. Read each code from the root: a left branch adds 0, a right branch adds 1.

The size of the code table itself is not included.

The principle

Because no code is a prefix of another, a decoder can read the bit stream left to right without separators. Merging the two least frequent nodes first pushes the rarest symbols deepest in the tree, where long codes cost little because they are seldom used. David Huffman proved in 1952 that this greedy choice gives an optimal prefix code: no other symbol-by-symbol code has a smaller total. The average code length lies within one bit of the Shannon entropy.

Worked example

  1. Text: "abracadabra", 11 characters. Counts: a = 5, b = 2, r = 2, c = 1, d = 1.
  2. Merge c(1) + d(1) = 2; then b(2) + r(2) = 4; then cd(2) + br(4) = 6; finally a(5) + 6 = 11.
  3. Codes: a = 0, c = 100, d = 101, b = 110, r = 111.
  4. Encoded bits: 5×1 + 1×3 + 1×3 + 2×3 + 2×3 = 23 bits.
  5. ASCII needs 11 × 8 = 88 bits, so the size is 23 ÷ 88 = 26.14% and the saving 73.86%.

How to read the result

A lower percentage means better compression. Average bits per character here are 23 ÷ 11 ≈ 2.09, just above the entropy of about 2.04 bits. Ties can be broken differently, so another tool may give different codes, but the total length of an optimal code is the same.

Common mistakes

Keep in mind: Huffman coding exploits only single-symbol frequencies. Practical compressors combine it with dictionary methods such as LZ77, and with only one distinct symbol this tool simply assigns the code "0".

Frequently asked questions

Is Huffman coding lossless?

Yes. Decoding the bit stream with the same tree returns exactly the original text.

How is Huffman different from arithmetic coding?

Huffman uses a whole number of bits per symbol; arithmetic coding can spend fractional bits and gets closer to the entropy, especially for skewed distributions.

Why are Huffman codes prefix-free?

Symbols sit only at the leaves of the tree, so the path to one symbol never passes through another.

Sources: D. A. Huffman, "A Method for the Construction of Minimum-Redundancy Codes", Proceedings of the IRE, 1952 · RFC 1951, DEFLATE Compressed Data Format Specification version 1.3

Checked 8 Nov 2026. Standard formulas and RFC/IEEE definitions, computed locally in your browser — nothing you type is sent anywhere.

💻 All tech & IT tools →🌍 Calculators by country →