Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Advanced

Data Compression: RLE and Huffman Coding

3.3.8 Data compression

Aligned to the AQA 8525 specification

Level
Advanced
Reading time
7 min
Published
3 June 2026
Updated
1 July 2026
On this page
  1. 1.Why Compress Data?
  2. 2.Run-Length Encoding (RLE)
  3. 3.RLE: A Worked Example
  4. 4.Huffman Coding: The Principle
  5. 5.Reading a Huffman Tree
  6. 6.Calculating Bits Saved
  7. 7.Common Exam Mistakes

Key takeaways

  • Data compression reduces file size by encoding the same information more efficiently; lossless methods reconstruct the original data perfectly, while lossy methods permanently discard some data.
  • AQA 8525 assesses two lossless methods: run-length encoding (RLE) and Huffman coding.
  • RLE replaces consecutive repeated values with a frequency/data pair, so it only saves space when runs are longer than the overhead and can increase size when runs are short, as with ABCABC.
  • Huffman coding assigns shorter binary codes to more frequent characters and longer codes to less frequent ones, so frequently used characters consume fewer bits.
  • A Huffman code is read as the path from the root down to a character's leaf, with each left branch a 0 and each right branch a 1, and total compressed size is the sum of frequency × code length.

Why Compress Data?

Uncompressed files consume significant storage and take longer to transmit across networks. A single minute of uncompressed CD-quality audio is approximately 10 MB; an uncompressed full HD video frame is around 6 MB. Without compression, streaming, email attachments, and cloud storage as they exist today would be impractical.

Data compression reduces file size by encoding the same information more efficiently. There are two broad categories:

TypeDescriptionData loss?Examples
LosslessOriginal data can be perfectly reconstructedNoneRLE, Huffman coding
LossySome data is permanently discardedYes — irreversibleJPEG images, MP3 audio

AQA 8525 requires knowledge of run-length encoding (RLE) and Huffman coding, both of which are lossless methods. The existence of lossy compression is worth knowing for comparison, but RLE and Huffman are the assessed techniques.

Lossless compression is essential wherever the original data must be preserved exactly — executable programs, text documents, medical scans.

Run-Length Encoding (RLE)

Run-length encoding replaces consecutive repeated values with a single frequency/data pair: the number of times the value appears followed by the value itself.

RLE achieves good compression when data contains long runs of the same value — solid-colour regions in images, silence in audio, or repeated characters in text.

Format: each pair is written as (frequency, data) or frequency data depending on the system.

Original dataRLE encodedPairs
AAAAAABBBCCA(6,A)(3,B)(2,C)(1,A)4
000000001111(8,0)(4,1)2
ABCABC(1,A)(1,B)(1,C)(1,A)(1,B)(1,C)6

The third example shows a case where RLE increases the data size — it only saves space when runs are longer than the overhead of storing the frequency/data pair.

RLE: A Worked Example

Encoding — compress the pixel row WWWWWWWWBBBBWWWW (16 pixels: 8 white, 4 black, 4 white):

RLE pairs: (8, W)(4, B)(4, W)

Original: 16 values
Compressed: 3 pairs (6 values total) — 62.5% fewer values to store

Decoding — expand (5,A)(3,B)(2,C)(1,A):

PairExpansion
(5,A)AAAAA
(3,B)BBB
(2,C)CC
(1,A)A

Result: AAAAABBBCCA — 11 characters ✓

Verification: count the characters — 5 + 3 + 2 + 1 = 11. Always verify your decode length matches what the pairs predict.

RLE is effective for bitmap images with large uniform areas (logos, icons, screenshots of text editors) and ineffective for photographs, where almost every pixel has a different colour.

Huffman Coding: The Principle

Huffman coding assigns shorter binary codes to characters that appear more often and longer codes to characters that appear less often. This means frequently used characters consume fewer bits, reducing the total size of the encoded data.

Standard ASCII uses 8 bits per character regardless of how often the character appears. Huffman coding tailors the code length to the actual frequency distribution.

Key principle: the more frequently a character appears, the shorter its Huffman code.

CharacterFrequencyFixed code (2 bits)Huffman codeHuffman bits
A50011
B301012
C2100013
D1110003

The total character count is 11. With fixed 2-bit codes: 11 × 2 = 22 bits. With Huffman codes: (5×1) + (3×2) + (2×3) + (1×3) = 5 + 6 + 6 + 3 = 20 bits — a saving even on this small example.

Studying this for an exam?

Generate a personalised learning path for this subject. Free to get started.

Create a learning path

Reading a Huffman Tree

A Huffman tree encodes the code for each character as a path from the root to that character's leaf node. Each left branch represents a 0 and each right branch represents a 1.

Example tree for characters A(5), B(3), C(2), D(1):

To find the code for any character, trace the path from the root and read the branch labels:

CharacterPathCode
Aroot → right1
Broot → left → right01
Croot → left → left → right001
Droot → left → left → left000

Encoding the message "AAAABBC":

CharacterCodeBits
A1× 4 = 4
A1
A1
A1
B01× 2 = 4
B01
C001× 1 = 3

Encoded: 1 1 1 1 01 01 001 = 11 bits

Fixed 2-bit encoding of the same message: 7 × 2 = 14 bits.

Calculating Bits Saved

To calculate how much compression Huffman coding achieves, compare the total bits in the compressed and uncompressed representations.

Worked example — text with character frequencies A=5, B=3, C=2, D=1 (11 characters total):

Uncompressed (ASCII, 8 bits per character):

Huffman compressed (using codes from the previous slide):

Bits saved:

The compression ratio here is — the compressed file is less than a quarter of the original size. In practice, the Huffman tree itself must also be transmitted alongside the compressed data so the receiver can decode it, which reduces the net saving slightly.

Common Exam Mistakes

1. Decoding RLE in the wrong order

The format is (frequency, data) — frequency first, then the value. Decoding (3,A) gives AAA, not three separate (A) entries or A repeated three times the wrong way round.

2. Saying RLE always reduces file size

RLE increases file size when runs are short. Data like ABCDEF has no repeated values, so each pair stores one value but adds the overhead of a frequency counter — the encoded form is larger than the original.

3. Reading the Huffman tree in the wrong direction

Always start at the root and trace down to the leaf. Reading from leaf to root reverses the code. The root is the node at the top with no parent.

4. Assigning 0 to left and 1 to right inconsistently

The assignment of 0 to one branch and 1 to the other is a convention that must be applied consistently throughout the same tree. An exam question will typically label the branches or specify the convention — use whatever the question gives you.

5. Forgetting to multiply frequency by code length when totalling bits

The total compressed size is — not just the sum of code lengths. A character that appears 5 times contributes bits, not just .

Key terms

Data compression
Reducing file size by encoding the same information more efficiently.
Lossless compression
Compression from which the original data can be perfectly reconstructed, with no data loss, such as RLE and Huffman coding.
Lossy compression
Compression in which some data is permanently discarded and cannot be recovered, such as JPEG images and MP3 audio.
Run-length encoding (RLE)
A lossless method that replaces consecutive repeated values with a frequency/data pair stating how many times the value appears.
Frequency/data pair
The unit of RLE output, written as the count of repetitions followed by the repeated value, such as (8,W).
Huffman coding
A lossless method that assigns shorter binary codes to more frequent characters and longer codes to less frequent ones.
Huffman tree
A tree that encodes each character's code as the path from the root to its leaf, with left branches as 0 and right branches as 1.

Frequently asked questions

Lossless compression lets the original data be perfectly reconstructed with no data loss, as in RLE and Huffman coding, while lossy compression permanently discards some data and is irreversible, as in JPEG images and MP3 audio.

No. RLE only saves space when data contains long runs of the same value. When runs are short, as in ABCABC, each pair stores one value but adds the overhead of a frequency counter, so the encoded form is larger than the original.

Start at the root and trace the path down to that character's leaf node, reading each left branch as 0 and each right branch as 1. Reading from leaf to root reverses the code, so always start at the root, which is the node at the top with no parent.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Representing Sound: Sampling Rate and Bit Depth

Next

Software Classification: System Software and Applications

Related lessons

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.