What is Huffman coding?
Huffman coding compresses data by giving short binary codes to common characters and longer codes to rare ones. A Huffman tree shows each code: go left for 0 and right for 1 to reach a character. ASCII uses 7 bits per character. Example: ABRACADABRA has 11 characters, so ASCII needs 11 Γ 7 = 77 bits. With A = 0, B = 100, R = 101, C = 110, D = 111 it needs 23 bits. Answer: it saves 54 bits.
Quick answer: Huffman coding is a way to compress data by using fewer bits for characters that appear often and more bits for characters that appear rarely. This page covers GCSE Computer Science, AQA (8525), specification 3.3.8 Data compression.
Key rule
- Count how often each character appears (its frequency).
- Common characters get short codes; rare characters get longer codes.
- In a Huffman tree, each branch is 0 or 1. Follow the branches from the top to a character to read its code.
- Bits in ASCII = number of characters Γ 7. Bits in Huffman = sum of (frequency Γ code length).
- Bits saved = ASCII bits β Huffman bits.
Extra example
The text ABRACADABRA is stored using this Huffman code: A = 0, B = 100, R = 101, C = 110, D = 111. Work out the bits saved compared with 7-bit ASCII.
- Frequencies: A appears 5 times, B 2, R 2, C 1, D 1. That is 11 characters.
- ASCII: 11 Γ 7 = 77 bits.
- Huffman: A 5 Γ 1 = 5, B 2 Γ 3 = 6, R 2 Γ 3 = 6, C 1 Γ 3 = 3, D 1 Γ 3 = 3. Total = 23 bits.
- Saving: 77 β 23 = 54 bits.
Answer: Huffman coding uses 23 bits instead of 77, saving 54 bits.
Want one-to-one help? Find an online Computer Science tutor with ExpertTutor.
Want one-to-one support?
One-to-one online tuition can help you build confidence in your subject. Find a tutor who teaches your course.
Find a TutorLearn face to face in Glasgow
Prefer in-person lessons? Visit the ExpertTutor Glasgow Southside centre.
See the centre