GCSE · Computer Science · AQA · Spec 8525
Data compression
E turns up everywhere, Q almost never. So why give them codes of the same length? Shorten the busy ones and the message shrinks, losing nothing.
Computer Science · Huffman tree
Walk the tree, collect the code
Guess first: which character lives closest to the root? Walk there and see. Then hunt down the rarest one and count your turns. Each code is just the 0s and 1s of your route. Bonus: follow 1, 1, 0 from the root. Where do you land?
1st bit → 2nd bit → 3rd bit → 4th bit
Every code starts here. Each bit you read is a turn: 0 means left, 1 means right.
5 characters, one at each end of the tree.
Characters sit only at the ends of branches, never at a fork. Land on one and you're done with it, which is exactly why no code can ever be the start of another.
Computer Science · Algorithms
Trace table — dry run the code
That tree didn't appear from nowhere. Here's how it's built from the table E 8, T 5, A 3, N 2, Q 1. Before each join, guess which two get picked, then step on. A bracket like (QN) is a joined node, and the number after the colon is its frequency.
Ready when you are — step through one line at a time.
Computer Science · Run length encoding
Squash the runs
The row to encode is R R R R G G B B B B B. Step along it and watch the pairs appear.
Before you step: how many runs of identical values can you spot in this row? Hold that number and check it at the end.
1 count = 12 FOR each next value in the row3 IF it matches the previous value THEN count = count + 14 ELSE write the pair (count, previous value) and set count = 15 After the last value, write the final pair
Variables
Output
Step 1: The first value is R, and we've seen one of it so far.
Step through the trace — every value is the lesson’s, not run by the page.
Predict, then check
Both rows are 12 values long. RLE writes each run as a frequency/data pair.
Row X is A A A A A A A A A A A A. Row Y is A B A B A B A B A B A B. Which row gets BIGGER after RLE?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Fewer bits, same data: why common characters get short codes, and when repeats can be squashed.
What you need to know
- Compression means storing the same data in fewer bits.
- Fewer bits means less storage space, and data that crosses a network more quickly, using less bandwidth.
- Huffman coding and run length encoding (RLE) are both lossless: the original can be rebuilt exactly.
- Huffman coding gives frequent characters short codes and rare characters long codes.
Have a goYour mate insists B should get the shortest code in the word BANANA, "because it comes first". Which letter really deserves it?
A. It appears 3 times, against 2 for N and 1 for B.
Huffman coding goes by how often a character appears, not where it sits, and a bit saved on A is saved every one of the 3 times A turns up.
- To build a Huffman tree, keep joining the two lowest-frequency nodes under a parent holding their sum, until one root is left.
Have a goFrequencies: W 6, X 4, Y 2, Z 1. Which two nodes are joined first, and what number does their new parent hold?
Y and Z, under a parent holding 3.
You always join the two LOWEST frequencies (2 and 1, summed to 3), not the first two in the list, which would wrongly pick W and X.
- A character's code is the route from the root down to its leaf, one bit per branch.
- Characters only sit at leaves, so no code is the start of another and decoding never gets confused.
- Huffman size: add up frequency × code length for every character. Uncompressed size: number of characters × bits per character.
- RLE swaps each run of identical values for a pair: how many times it repeats, then the value.
Have a goTurn the row G G G G R into RLE pairs.
(4, G) (1, R)
The lone R is still a run, just a run of one, so it needs its own pair; leave it out and the row can no longer be rebuilt exactly.
- RLE loves long runs. On data that keeps changing it saves little, or can even make the data bigger.
The big picture
Compression stores the same data in fewer bits, so it takes less storage and is quicker to send, using less bandwidth. Huffman coding gives common characters short codes and rare ones long codes, and you read each code by walking a tree from the root to a leaf. Run length encoding swaps each run of identical values for a frequency/data pair. Both are lossless, and RLE can backfire on data that keeps changing.
Key points
Worked example
Problem
Using the tree from the top of the page (E = 0, T = 10, A = 110, Q = 1110, N = 1111), decode the bit stream 0101101111.
⚠ Watch out
Treating a code as a number. The code 10 is a route (right, then left) with a length of 2, not the value ten. In size sums, multiply each frequency by its own code's length, and for the uncompressed size count every character, repeats included.
Memory hook
Busy letters live by the front door (the root). Rare letters live at the bottom of the garden, and it takes a long walk to reach them.
Check yourself
Without looking back: why does E get a one-bit code while Q gets four bits, and what would go wrong if one code were the beginning of another?
Flashcards
(11)What does data compression do, and why bother?
What does 'lossless' mean?
ASCII codes are all the same length. How are Huffman codes different?
How do you build a Huffman tree from a frequency table?
How do you read a character's code from a Huffman tree?
While decoding, what do you do the moment you land on a leaf?
Why can a decoder tell where one Huffman code ends?
How do you work out the size of data after Huffman coding?
How do you work out the uncompressed size of text in ASCII?
What does run length encoding (RLE) do?
When does RLE help, and when can it hurt?
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 29 October 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.