Files never leave your device

Huffman coding calculator

Build a prefix code and estimate the average number of bits per symbol. Your inputs stay on your device.

Set up your task

Ready. Processing stays in this browser.

How to use Huffman coding calculator

Build a prefix code and estimate the average number of bits per symbol. The pre-filled problem gives you a worked starting point. Compare the response with your own working before trying the self-check below.

  1. Check the example inputs: Symbol and frequency.
  2. Change the inputs for your problem. Results update as you type, so finish entering the values before copying an answer.
  3. Use the working to check an intermediate value or assumption. Then change the example as suggested in the self-check and compare the result.

Supported inputs and limits

Enter 2 to 24 unique short names, each followed by a positive whole-number frequency on its own row.

How it works

Huffman coding gives shorter binary codewords to more frequent symbols. It constructs a tree by repeatedly joining the two nodes with the lowest combined frequencies available at that stage. Traversing the left and right branches with zero and one supplies a codeword for every leaf.

A prefix code allows a decoder to recognise each symbol as soon as it reaches a leaf. No complete codeword is a prefix of another. The reported mean length weights each codeword by its frequency. The payload length omits the description of the tree, padding and file format overhead.

Example

Run the pre-filled example. Its first result is “Mean code length: 1.66666667 bits/symbol”.

Symbol and frequency
A 5
B 2
C 1
D 1
Mean code length: 1.66666667 bits/symbol
A: 1
B: 00
C: 010
D: 011
Payload length for the supplied counts: 15 bits.

Questions about Huffman coding calculator

Can I interpret the payload bits as the size of a compressed file?

Use it as a payload estimate. A real file must also describe the codebook and may include padding, metadata and a container.

Can two correct Huffman codes differ?

Yes. Tied frequencies and branch order can yield different bit patterns at the same optimum weighted length.

Use four equal frequencies.

Each codeword has two bits. The mean length is two, matching a fixed-width code for four symbols.

Related tools

Year 3: Advanced and applied maths

Last updated