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 frequency2 WHILE more than one node is left3 take the two lowest-frequency nodes4 join them under a new parent node (frequency = their sum)5 ENDWHILE6 the one node left is the root
Variables
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.
Same move every pass: two lowest, join, sum, back into the list.
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.
Here is the finished Mississippi tree. Walk from the root to each character and write down the bits you pick up on the way.
Benefit and cost
Reason it through
Why does Huffman coding make files smaller, and why isn't it free?
First link · your turn
What does Huffman coding give to characters that occur often?
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
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?
Give three benefits of compressing a file.
What are the two categories of compression, and which one is Huffman coding?
Which characters does Huffman coding give the shortest bit patterns to?
What is a frequency table?
Name the parts of a Huffman tree: node, branch, root, parent.
What is the rule for building a Huffman tree?
What should the root's frequency be, and why?
How do you read a character's bit pattern from the tree?
Why do rarer characters get longer bit patterns?
In the Mississippi tree, which character has the shortest pattern, and what is it?
How do you compress data once you have the tree?
Is the compressed file the Huffman tree?
Why is the tree needed to decompress, and what does that cost?
What happens when two nodes tie for lowest frequency?
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 postedMore AQA GCSE Computer Science topics
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.