Home / Science / Maths, statistics and computing / Information theory and cryptography / Huffman and arithmetic coding visualiserTool #579

Huffman coding and arithmetic coding visualiser: tree, code table, encode and decode, step-by-step intervals

Build a Huffman code from a message or from symbol probabilities: see the tree, the code table, the average length against the entropy, and encode or decode bits. Then watch the same message arithmetic coded, interval by interval, with exact fractions.

Keep it one tap away for everyday use.

Bookmark Huffman and arithmetic coding visualiser?

Press Ctrl + D now. Your browser opens its own bookmark box, where you choose the name and folder.

  1. Message
  2. Tree
  3. Code table
  4. Intervals
Nothing is uploadedWorks offline once open

Source

What it does

Compression works by giving common symbols short codes and rare ones long codes. This tool shows the two classic ways to do it, step by step, for any message you type:

  • Huffman coding joins the two least likely symbols again and again until one tree is left. Reading the path from the top to each symbol (left 0, right 1) gives its code. The tool draws the tree, lists every code, and compares the average code length L with the entropy H, the smallest average any code can reach. Huffman always lands within one bit: H ≤ L < H + 1.
  • Encode and decode: your message as a string of bits, and a box to turn bits back into symbols with the same code.
  • Canonical codes: the same code lengths with codes given out in order, so a file only needs to store the lengths. DEFLATE (zip, PNG) and JPEG do this.
  • Arithmetic coding gives the whole message one number. It starts with the interval from 0 to 1, and for each symbol shrinks it to that symbol’s share. The final interval’s width is the probability of the whole message, and the code is the shortest binary fraction whose range fits inside it. The tool does every step with exact fractions, draws the first six, and lists them all.

How to use it

  1. Type a message, or switch to Symbols and probabilities and list each symbol with its probability or count, one per line (for example a 0.4). Write space for a space. Symbols can be whole words; then a message is split at spaces.
  2. Read the tree and the code table. Tick Canonical codes to see the canonical version.
  3. The encoded bits appear under the table. Type or paste any bits into Decode bits to read them back.
  4. Scroll down for the arithmetic coding of the same message (up to 1,000 symbols).
  5. Download the code table or the steps as CSV, or everything as an image, PDF or Word file.

Worked example

The example, abracadabra, has 11 letters: a five times, b and r twice, c and d once. Huffman gives a the one-bit code 0, and b, r, c and d three-bit codes, so the average is 23 / 11 = 2.0909 bits per letter, against an entropy of 2.0404 bits: 97.58 % efficient. The whole word takes 23 bits, against 33 with a fixed 3-bit code and 88 as UTF-8 text.

Arithmetic coding of the same word ends with the interval from 0.2650551279 to 0.2650553031 (exactly 6874847395/25937424601 to 75623371345/285311670611). Its width is 2^−22.444, so the message carries 22.44 bits of information, and the shortest code that fits is 23 bits, 01000011110110101010100. On longer messages arithmetic coding pulls ahead, because it is not limited to a whole number of bits per symbol.

With the probabilities 0.4, 0.2, 0.2, 0.1 and 0.1 the average Huffman length is 2.2 bits against an entropy of 2.1219.

Tips

  • Ties can be broken in different ways, giving different codes with the same average length. This tool always takes the lighter node first and, on a tie, the one made earliest; the first one taken gets 0.
  • A symbol with probability above 0.4 always gets a code of one or two bits; one with probability 1 gets the code 0.
  • The arithmetic coder here uses the counts of the message itself as its model, so the decoder needs those counts too. Real coders send them, or learn them as they go (adaptive coding).

What it does not do

It is a teaching tool for short messages, not a file compressor: there is no adaptive model, no range-coder rounding and no file output. For compressing real files, use a compressor such as the file compressor on this site.

FAQ

Why is Huffman sometimes as good as arithmetic coding? When every probability is a power of one half (1/2, 1/4, 1/8 …), Huffman lengths equal −log₂ p exactly and both reach the entropy.

Is my message sent anywhere? No. Everything is worked out in your browser.

Where the numbers come from

  • Methods: Huffman (1952), canonical Huffman codes as used in DEFLATE (RFC 1951), and arithmetic coding as in Cover and Thomas, Elements of Information Theory (general textbook methods).
  • Checked against a Python reference written for this tool: heapq Huffman with the same tie rule, and an exact fractions.Fraction arithmetic coder, on four messages and a probability list (codes, bits, final intervals and tags all equal).
  • The example messages are made up or traditional (abracadabra, a tongue twister).

Standard scientific values, such as constants, atomic weights and melting points, are facts found in every textbook; we name where ours were checked. If you are an author or publisher and want a value, a credit or a table changed or removed, tell us and we will act quickly.

Last updated 2026-11-09.

Related tools

File compressor #1 One tool for PDF, Word, Excel, PowerPoint, JPG, PNG, CSV, ZIP and more. Files are compressed on your device and never uploaded. Entropy and channel capacity calculator #578 Shannon entropy of a probability distribution, of text or of any file's bytes; joint and conditional entropy and mutual information from a table; Kullback-Leibler and Jensen-Shannon divergence; and the capacity of binary symmetric, erasure, Z and any discrete channel by the Blahut-Arimoto algorithm. Nothing is uploaded. Error-correcting codes playground #580 Encode a message, damage it by tapping bits or bytes, and watch the decoder find and fix the errors: parity bits, Hamming (7,4) and (15,11) with SECDED, CRC-8, CRC-16 and CRC-32 with the long division shown, and Reed-Solomon over GF(2⁸) with Berlekamp-Massey and Forney. Classical cipher and hash teaching tool #581 Encrypt and decrypt with the classical ciphers (Caesar, Vigenère, affine, Atbash, rail fence) and see the working and the letter frequencies of your text. Then follow SHA-256 through padding, the message schedule and all 64 rounds, and watch one flipped bit change about half the hash. 1D Schrödinger solver #923 Choose a textbook potential, type a formula or draw your own, and get the lowest energy levels and wavefunctions of the one-dimensional Schrödinger equation by the matrix (finite-difference) method, with the exact levels alongside for the box, harmonic oscillator, finite well, Morse, linear and Pöschl–Teller potentials. Adduct and neutral loss calculator #984 Type a formula or a neutral monoisotopic mass and get the m/z of 25 common electrospray adducts in positive and negative mode, the ions left after common neutral losses (water, ammonia, CO2, sugars, phosphate and your own), and match a list of observed m/z values within a ppm tolerance. With no molecule, it turns observed m/z values into candidate neutral masses.