How many bits could a Leeds school save when it sends the message AAABC?
A school club in Leeds sends a short message: A, A, A, B, C. The message has five letters, but the letters don't all need the same number of bits. How many bits could the club save?
In standard ASCII, each character uses seven bits. Using fewer bits to represent the same message is a form of compression. Fewer bits take up less storage space and mean you have less data to send.
Start at the root and read the bits along the path to each letter. A takes zero. To reach B, take one then zero. To reach C, take one then one. This is Huffman coding. It's lossless: you can recover the original message exactly.
Look at the message B, B, A, C. B still has the code one zero. Focus on the two Bs and leave A and C out for now. How many bits do the Bs contribute altogether? Pick an answer.
Join three zeros, then B's one zero and C's one one. Multiply each count by its code length: three times one plus one times two plus one times two gives seven bits. Five times seven gives thirty-five ASCII bits. Subtract seven from thirty-five to save twenty-eight bits.
Now use the same tree for A, B, B, C, C. The message still has five characters. Work out the Huffman size, then compare it with seven-bit ASCII. How many bits do you save? Pick an answer.
If you add each code length once, you get one plus two plus two, or five bits. That would encode A, B, C. Our message has two extra As, so count one more bit for each. The full message takes seven bits.
Read the message from left to right. Four As in a row give the pair four, A. Three Bs give three, B. The last two As give two, A, since the Bs split the As into separate runs. To recover the message, repeat each letter according to its count.