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.

On Root: start here. 2 branches to choose from.

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.

Watch out: A code is a route, not a number. 10 means 'right, then left', not 'ten'.

Computer Science · Compression

What do you reckon compression is?

You've just watched common characters get short codes and rare ones get long codes. Quick gut check on the big ideas behind it.

Which of these sounds most like what you think?
How sure are you?

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.

1Write every character as a node with its frequency
2REPEAT
3 pick the two nodes with the lowest frequencies
4 join them under a new parent node (frequency = their sum)
5UNTIL only one node is left: the root
6Label one branch from each parent 0 and the other 1
Nodes in the list
Two lowest
New parent
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Computer Science · Sizes

How many bits did Huffman save?

A message has 19 characters: E appears 8 times, T 5 times, A 3 times, N twice and Q once. The Huffman codes are E = 0, T = 10, A = 110, Q = 1110 and N = 1111. Taking ASCII as 7 bits per character, how many bits does Huffman coding save? Fill in the missing steps.

  1. E: 8 characters × 1 bit = 8 bitsEach character's frequency is multiplied by the length of its own code.
  2. missing step
Which line is step 2?

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 = 1
2 FOR each next value in the row
3 IF it matches the previous value THEN count = count + 1
4 ELSE write the pair (count, previous value) and set count = 1
5 After the last value, write the final pair

Variables

count=1pairs written=none yetprevious value=R

Output

 

Step 1: The first value is R, and we've seen one of it so far.

1 / 12

Step through the trace — every value is the lesson’s, not run by the page.

Step 1 of 12: The first value is R, and we've seen one of it so far..

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

1Compression means fewer bits for the same data: less storage, and faster sending with less bandwidth.
2Lossless means nothing is thrown away. Huffman coding and RLE are both lossless.
3Build a Huffman tree by joining the two lowest-frequency nodes again and again until one root remains.
4Read a code by walking from the root to the leaf, one bit per branch (one direction is 0, the other is 1). Decode by going back to the root after every leaf.
5Characters only sit at leaves, so no Huffman code is the start of another and decoding is never ambiguous.
6To compare sizes, multiply frequency by code length and add, then compare with number of characters × bits per character.
7RLE replaces each run with a frequency/data pair. It helps with long runs and can hurt when values keep changing.

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?
It stores the same data using fewer bits. That saves storage space and makes data quicker to send, using less bandwidth.
What does 'lossless' mean?
The original data can be rebuilt exactly from the compressed version. Huffman coding and run length encoding are both lossless.
ASCII codes are all the same length. How are Huffman codes different?
Huffman codes have different lengths. Common characters get short codes and rare characters get long ones.
How do you build a Huffman tree from a frequency table?
Each character starts as a leaf holding its frequency. Repeatedly join the two lowest-frequency nodes under a new parent whose frequency is their sum. Stop when one node, the root, is left.
How do you read a character's code from a Huffman tree?
Walk from the root to that character's leaf, writing one bit per branch: one direction is 0, the other is 1. The bits in order are the code.
While decoding, what do you do the moment you land on a leaf?
That character is finished. Write it down and go back to the root before reading the next bit.
Why can a decoder tell where one Huffman code ends?
Characters sit only at the ends of branches, so no code is the start of another. Landing on a leaf means that character is complete.
How do you work out the size of data after Huffman coding?
Multiply each character's frequency by the length of its own code, then add the results. Take that away from the uncompressed size to find the saving.
How do you work out the uncompressed size of text in ASCII?
Number of characters × bits per character. Use the bits per character the question gives you: standard ASCII codes are 7 bits, but characters are often stored in 8-bit bytes.
What does run length encoding (RLE) do?
It replaces each run of identical values with a frequency/data pair, such as (5, B) for five Bs in a row. It is lossless.
When does RLE help, and when can it hurt?
It helps when the data has long runs of the same value, like a simple image with big areas of one colour. When values keep changing, every run is short, so RLE saves little or makes the data bigger.

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 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.