Huffman coding is a lossless compression technique that assigns shorter binary codes to the most frequent characters in a file, reducing its size without losing any data. For GCSE Computer Science, you need to construct a Huffman tree from a frequency table and calculate the total bits needed before and after compression.
Why use variable-length codes?
Standard ASCII encoding gives every character exactly 8 bits, regardless of how often it appears. In the word "MISSISSIPPI", the letter I appears five times but still costs 8 bits each time — the same as a character that never appears. Huffman coding challenges this: why spend equal resources on common and rare characters?
The insight is to give shorter codes to frequent characters and longer codes to rare ones. The average bit-length per character falls, so the overall file shrinks. As long as no code is the prefix of another (the prefix property), the codes can be decoded unambiguously without needing separators.
How do you build a Huffman tree?
Building the tree is an algorithm you must be able to execute step by step.
Worked example — compress the string "AABBBBCD".
Step 1: Count frequencies.
| Character | Frequency |
|---|---|
| B | 4 |
| A | 2 |
| C | 1 |
| D | 1 |
Step 2: Create leaf nodes, one per character, and sort by frequency (lowest first).
Nodes: C(1), D(1), A(2), B(4)
Step 3: Repeatedly merge the two lowest-frequency nodes into a parent node whose frequency is their sum. Repeat until one node remains.
- Merge C(1) and D(1) → parent node (2)
- Nodes now: (2), A(2), B(4)
- Merge (2) and A(2) → parent node (4)
- Nodes now: (4), B(4)
- Merge (4) and B(4) → root node (8)
Step 4: Label left branches 0 and right branches 1 (or consistently the other way — either is valid).
The tree structure (left = 0, right = 1):
Root(8)
/ \
(4) B(4)
/ \
(2) A(2)
/ \
C(1) D(1)
Step 5: Read each character's code by tracing from root to leaf.
| Character | Path | Code | Code length |
|---|---|---|---|
| B | Right | 1 | 1 bit |
| A | Left → Right | 01 | 2 bits |
| C | Left → Left → Left | 000 | 3 bits |
| D | Left → Left → Right | 001 | 3 bits |
How do you calculate the number of bits before and after compression?
Without compression (fixed 2-bit ASCII for 4 characters):
For a 4-character alphabet you need at least 2 bits per character in a fixed scheme.
| Character | Frequency | Bits each | Total bits |
|---|---|---|---|
| B | 4 | 2 | 8 |
| A | 2 | 2 | 4 |
| C | 1 | 2 | 2 |
| D | 1 | 2 | 2 |
| Total | 8 chars | — | 16 bits |
With Huffman coding:
| Character | Frequency | Bits each | Total bits |
|---|---|---|---|
| B | 4 | 1 | 4 |
| A | 2 | 2 | 4 |
| C | 1 | 3 | 3 |
| D | 1 | 3 | 3 |
| Total | 8 chars | — | 14 bits |
Compression saving: 16 − 14 = 2 bits saved (a 12.5% reduction for this small example). With real text files, savings are typically 20–50%.
What makes Huffman coding lossless?
Lossless means the original data can be reconstructed exactly from the compressed version. Huffman coding achieves this because:
- The codes satisfy the prefix property — no code is the start of another code. This means a decoder can read bits one at a time and know unambiguously when a code is complete.
- The Huffman tree itself must be transmitted alongside the compressed data so the receiver can decode it. Without the tree, the bit stream is unreadable.
Compare this with lossy compression (such as JPEG for images or MP3 for audio), which permanently discards some data to achieve higher compression ratios. Huffman coding is appropriate when exact reproduction is required — for text, executable programs, and database backups.
When is Huffman coding most effective?
Huffman compression is most effective when character frequencies are highly uneven — when some characters appear very frequently and others rarely. In natural language text in English, the letter E appears roughly 13% of the time, while Z appears roughly 0.07%. This skew is exactly what Huffman exploits.
If all characters appear with equal frequency, Huffman coding produces codes of equal length and achieves no compression at all.
How is Huffman coding used in practice?
Huffman coding is a component inside several real-world compression formats:
| Format | Where Huffman is used |
|---|---|
| DEFLATE (ZIP, PNG) | One stage of a two-pass algorithm (with LZ77 dictionary compression) |
| JPEG | Compresses the quantised frequency coefficients after DCT |
| MP3 | One stage of audio compression pipeline |
In each case, Huffman coding is combined with other techniques to maximise compression.
Frequently asked questions
Do I need to draw the tree in an exam or just state the codes?
For AQA GCSE Computer Science, exam questions typically ask you to complete or construct a Huffman tree from a frequency table, assign codes by tracing root-to-leaf paths, and calculate total bits before and after. Showing the tree structure is usually required for full marks, and the codes you assign must be consistent with your tree.
What if there is a tie in frequency when merging nodes?
When two pairs of nodes have equal frequencies, the order in which you merge them may differ — and this produces a valid but different tree. Both resulting trees are correct as long as no code is a prefix of another. In an exam, if a tie exists, the question usually specifies which node to merge first, or either valid answer is accepted. Always check the mark scheme guidance if it is not clear.
Does the Huffman tree need to be stored with the compressed file?
Yes. Without the tree, the compressed bit stream cannot be decoded. In practice, the tree is stored as a header at the start of the compressed file. This overhead means Huffman compression is less useful for very short files — the tree header can actually make a tiny file larger. Compression is worth it only when the data being compressed is substantially larger than the tree header.
What is the difference between Huffman coding and run-length encoding?
Run-length encoding (RLE) replaces repeated consecutive values with a count and value pair, making it effective for data with long runs of the same value (such as bitmap images with large areas of one colour). Huffman coding reduces the bit-length of frequent symbols regardless of whether they appear consecutively. They solve different patterns of redundancy: RLE targets repeated runs; Huffman targets uneven character frequencies.
Work through Huffman tree construction problems with Professor Turing's hint-by-hint guidance at aitutors.me.