---
title: "Huffman codes with examples"
subject: "Communication Systems"
chapter: 6
key: "semester_6/communication_systems:huffman-codes-examples"
source: https://www.bctengineer.com/q/semester_6/communication_systems/huffman-codes-examples
site: BCT Engineering
---

# Huffman codes with examples

Communication Systems · Chapter 6: Baseband Digital Data Transmission

- Asked in: 2072 Chaitra (CS II), 4 marks
- Video: [Huffman Coding (Basics, Algorithm, Procedure & Example) Explained in Digital Communication](https://www.youtube.com/watch?v=HmBH30NrM7c) — Engineering Funda
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/communication_systems/huffman-codes-examples
- Whole chapter: https://www.bctengineer.com/md/semester_6/communication_systems/chapter-6

## Question

Explain Huffman codes with examples.

## Short answer

Huffman coding is an optimal prefix code: repeatedly combine the two lowest probabilities and assign bits from the final merge. For symbols with probabilities 0.4,0.2,0.2,0.1,0.1, codes are A=00, B=10, C=11, D=010, E=011; average length 2.2 bits/symbol yields 96.45% efficiency vs 70.7% for a 3-bit fixed code.

## Answer

A **Huffman code** is an optimal variable-length prefix code: for a given set of symbol probabilities, no other symbol-by-symbol code has a smaller average length $\bar L$. Frequent symbols get short code words.

**Procedure:**

1. List the symbols in decreasing order of probability.
2. Combine the two lowest probabilities into a new probability equal to their sum.
3. Put the sum in the reordered list, as high as possible among equal values.
4. Repeat until only two (then one) probabilities remain.
5. Going back from the last stage, assign 0 to one branch and 1 to the other at each merge; a symbol's code is the sequence of bits on its path.

**Example:** five symbols A–E with $P = \{0.4, 0.2, 0.2, 0.1, 0.1\}$.

**Reduction (sum of two lowest moved up as high as possible):**

| Stage | Probabilities (descending) | Combined |
|---|---|---|
| 1 | 0.4, 0.2, 0.2, 0.1, 0.1 | 0.1 + 0.1 = 0.2 |
| 2 | 0.4, 0.2, 0.2, 0.2 | 0.2 + 0.2 = 0.4 |
| 3 | 0.4, 0.4, 0.2 | 0.4 + 0.2 = 0.6 |
| 4 | 0.6, 0.4 | 0.6 + 0.4 = 1 |

| Symbol | $p_i$ | Huffman code | $l_i$ |
|---|---|---|---|
| A | 0.4 | 00 | 2 |
| B | 0.2 | 10 | 2 |
| C | 0.2 | 11 | 2 |
| D | 0.1 | 010 | 3 |
| E | 0.1 | 011 | 3 |

$$
\begin{aligned}
\bar L &= 0.4(2)+0.2(2)+0.2(2)+0.1(3)+0.1(3) = 2.2\ \text{bits/symbol} \\
H &= 2.1219\ \text{bits/symbol}, \qquad \eta = \frac{2.1219}{2.2} = 96.45\%
\end{aligned}
$$

A fixed-length code would need 3 bits (efficiency 70.7 %). No code word is a prefix of another, so "0001110" decodes uniquely as 00 | 011 | 10 = A, E, B. Huffman coding is used in JPEG, MP3 and ZIP.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which statement describes a Huffman code?
   A. The optimal prefix code: no other symbol-by-symbol code has smaller average length
   B. A fixed-length code
   C. A code that adds redundancy for error correction
   D. A code where all lengths are equal
   Answer: A. Huffman gives the smallest possible average length for given probabilities. (Key point: Huffman optimality)

2. What is done at each step of the Huffman procedure?
   A. Combine the two highest probabilities
   B. Combine the two lowest probabilities and reinsert the sum into the list
   C. Split the list in two equal halves
   D. Remove the least probable symbol
   Answer: B. The two lowest probabilities are merged, the sum is reordered into the list, and repeated until one remains. (Key point: Huffman procedure)

3. For P = {0.4,0.2,0.2,0.1,0.1} Huffman gives lengths 2,2,2,3,3. What is the average length?
   A. 2.0
   B. 2.2
   C. 2.4
   D. 3.0
   Answer: B. 0.4(2)+0.2(2)+0.2(2)+0.1(3)+0.1(3) = 2.2 bits. (Key point: Example efficiency)

4. Why can 0001110 be decoded uniquely using the example codes?
   A. No code word is a prefix of another
   B. All code words have equal length
   C. A separator is sent between symbols
   D. The codes are random
   Answer: A. Being a prefix code, decoding proceeds without ambiguity: 00 | 011 | 10 = A, E, B. (Key point: Prefix property)

## Related in Communication Systems

- [Shannon-Fano and Huffman codes for six messages](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Asf-huffman-six-messages) — Communication Systems, chapter 6
- [Source coding aim; Huffman "Kun Mandir Ma Janchhau Yatri"](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Ahuffman-kun-mandir) — Communication Systems, chapter 6

## Related in other subjects

- [Source coding; fixed vs variable length codes](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Asource-coding-fixed-variable) — Data Communication, chapter 8
