GCSE · Computer Science · AQA · Spec 8525

Huffman coding

Mississippi has 11 letters but only four different ones. Huffman coding turns that lopsidedness into a smaller file, using a tree that grows from the rarest letters up.

Computer Science · Huffman coding

Grow a tree from the rarest letters up

The word Mississippi has 11 letters but only four different ones. Press Next to run the merge loop and watch the nodes list shrink until one root is left.

Before each Next, say out loud which two nodes will be taken. Then check yourself.

1 list every character as a node, with its frequency
2 WHILE more than one node is left
3 take the two lowest-frequency nodes
4 join them under a new parent node (frequency = their sum)
5 ENDWHILE
6 the one node left is the root

Variables

nodes=M(1) P(2) S(4) I(4)characters in total=11

Output

 

Step 1: Mississippi: M once, P twice, S four times, I four times. That is the frequency table, and every row starts life as its own node.

1 / 12

Same move every pass: two lowest, join, sum, back into the list.

Step 1 of 12: Mississippi: M once, P twice, S four times, I four times. That is the frequency table, and every row starts life as its own node..

Watch out: The new parent goes back into the list and competes like any other node. Forget that and you will join the wrong pair on the next pass.

Computer Science · Reading the tree

A code is just a route

Start at the root. At each fork choose left (write 0) or right (write 1) until you reach a character.

1st move → 2nd move → 3rd move

  • Every route starts here. A node with nodes beneath it is a parent node, and the root is the top parent.

4 bit patterns.

On Root. 2 branches to choose from.

Here is the finished Mississippi tree. Walk from the root to each character and write down the bits you pick up on the way.

Computer Science · Your turn

Now you build one

A short message uses four characters: W appears 1 time, X appears 2 times, Y appears 3 times and Z appears 6 times. Build its Huffman tree. The steps are written out; fill in the gaps by choosing the right move.

  1. Start: W(1), X(2), Y(3), Z(6). That is 12 characters in total, so the finished root must be worth 12.
  2. missing step
Which line is step 2?

Computer Science · Compressing

Find the slip in the compression

Using the Mississippi codes (S = 0, I = 10, M = 110, P = 111), a student compresses the word MISS. One line has a slip. Which one?

A student's working — which line goes wrong?

Computer Science · Huffman coding

Which of these do you believe?

Three students are talking about a Huffman-coded message after seeing the Mississippi tree.

Which one is closest to what you think right now?
How sure are you?

Benefit and cost

?

Reason it through

Why does Huffman coding make files smaller, and why isn't it free?

Link 1 of 4

First link · your turn

What does Huffman coding give to characters that occur often?

2
Locked — reveal the link above first
3
Locked — reveal the link above first
4
Locked — reveal the link above first

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Give common characters the shortest codes, and shrink a file without losing a thing.

What you need to know

  • Compression makes a file smaller, using fewer bits. That saves storage and bandwidth, and speeds up uploads and downloads.
  • Compression is lossy or lossless. Huffman coding is lossless, so the original data comes back exactly.
  • Huffman coding gives frequent characters short bit patterns and rare characters longer ones.
  • Have a goYour friend Sam says: 'Z appears 50 times and Q only twice, so obviously Z gets the longest code.' Sam is very sure. Is Sam right?

    No. Z, the more frequent character, gets the shorter code.

    Short patterns go to the characters that occur most often, because every time they appear you save bits.

  • A frequency table counts how many times each character appears. The tree is built from that table.
  • A Huffman tree is a binary tree: the top node is the root, and a node with nodes beneath it is a parent.
  • To build it, repeatedly join the two lowest-frequency nodes under a new parent worth their sum.
  • Stop when one root node is left. Its frequency equals the total number of characters.
  • Have a goA tree is being built from the nodes 2, 5 and 9. Join the two lowest. Which nodes are left?

    7 and 9.

    The 2 and 5 are replaced by their parent, worth 2 + 5 = 7, and that parent goes back in the list with the 9.

  • To read a pattern, walk from the root to the character, writing 0 for each left move and 1 for each right.
  • Have a goOne character is reached after two moves from the root. Another needs four moves. Which has the longer bit pattern, and by how much?

    The four-move one, by two bits.

    Each move writes exactly one bit (0 for left, 1 for right), so the pattern is as long as the route.

  • To compress, swap every character for its bit pattern and join them in order. That compressed file is bits, not the tree.
  • The tree is needed to decompress. Sending it with the data adds overhead, which can cut the saving, especially for small files.

The big picture

Huffman coding is a lossless way to compress data. Count how often each character appears, then build a tree by repeatedly joining the two lowest-frequency nodes under a parent worth their sum, until one root remains. Read each character's bit pattern from root to character (0 for left, 1 for right), so frequent characters get short patterns and rare ones get long patterns. Swap each character for its pattern to compress. The tree is still needed to decompress, and sending it costs bits.

Key points

1Build from the bottom up: take the two lowest-frequency nodes, join them under a parent worth their sum, and repeat until one root is left.
2The root's frequency equals the total number of characters, which makes it a quick check on your tree.
3Each left move writes 0 and each right move writes 1, so a character's code is the route to it.
4Frequent characters sit near the root with short codes. Rare characters sit deep with long codes.
5Huffman coding is lossless: nothing is lost, but the tree is needed to get the data back.

Worked example

Problem

A message uses W (1 time), X (2 times), Y (3 times) and Z (6 times), and its Huffman tree has been built. Z hangs on the left of the root; the right branch leads to a parent node N6. Under N6, Y is on the left and a parent N3 is on the right. Under N3, W is on the left and X is on the right. Find the bit pattern for every character.

⚠ Watch out

Thinking the most common character needs the longest code. It is the opposite: frequent characters get the shortest patterns, and rare ones, joined first, end up deepest. Also remember a new parent goes back into the list.

🧠

Memory hook

Lowest two, join, add, repeat. Then walk the tree: left is 0, right is 1. The rarest letters sit deepest, so they pay the most bits.

✓

Check yourself

Try it cold: A(1), B(1), C(2), D(4). Which two nodes join first, what must the root come to, and which character gets the shortest pattern?

Flashcards

(15)
What is data compression?
Reducing the size of a file, by removing unnecessary data or rearranging information more efficiently, so that fewer bits are needed.
Give three benefits of compressing a file.
It needs less storage space (locally or in the cloud), uses less bandwidth when sent over a network, and gives faster upload and download times.
What are the two categories of compression, and which one is Huffman coding?
Lossy and lossless. Huffman coding is lossless, so the original data can be recovered exactly.
Which characters does Huffman coding give the shortest bit patterns to?
The characters that occur most often. Rarer characters get longer patterns, so the total number of bits falls.
What is a frequency table?
A count of how many times each character appears in the data. The characters can then be ordered from most to least frequent.
Name the parts of a Huffman tree: node, branch, root, parent.
Nodes joined by branches. Each node has at most two child nodes. The node at the top is the root, and a node with links to nodes beneath it is a parent node.
What is the rule for building a Huffman tree?
Repeatedly take the two lowest-frequency nodes and join them under a new parent whose frequency is their sum, until one root node remains.
What should the root's frequency be, and why?
The total number of characters, because every merge adds frequencies together. It is the quick check that you built the tree correctly.
How do you read a character's bit pattern from the tree?
Follow the path from the root to the character, writing 0 for each move left and 1 for each move right.
Why do rarer characters get longer bit patterns?
They are joined first, so they end up further from the root, and every extra move down adds one more bit to the pattern.
In the Mississippi tree, which character has the shortest pattern, and what is it?
S, with the pattern 0. The others are I = 10, M = 110 and P = 111.
How do you compress data once you have the tree?
Replace each character by its bit pattern and join the patterns in order.
Is the compressed file the Huffman tree?
No. The compressed file is a sequence of bits. The tree is a separate thing that is needed to decompress.
Why is the tree needed to decompress, and what does that cost?
Without it the encoding cannot be reversed. Sending it with the data adds overhead, which can reduce the saving, especially for small files.
What happens when two nodes tie for lowest frequency?
They can be joined in more than one order, giving different but equally valid trees and codes. Any tree built by the rule is correct.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 24 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.