Medium1 markStructured
Fundamentals of data representationGeneralcompressionHuffman codingtree

AQA GCSE · Question 17.3 · Fundamentals of data representation

Figure 3CharacterBinary codeM100I0S11P1011173M

Another method for compressing data is Huffman coding. In Huffman coding, the codes for the characters can be created based on their position in a tree.
Figure 3 shows a Huffman code for each different character in the string in Figure 2.
Complete the Huffman tree below to show the position of the characters I, S and P using the codes from Figure 3.

How to approach this question

1. **Understand Huffman Trees:** In a Huffman tree, moving down a left branch represents a `0` and moving down a right branch represents a `1`. The code for a character is the sequence of 0s and 1s you follow from the root (top) node to reach that character's leaf node. 2. **Trace the codes:** * **I (code 0):** Starting from the root (11), the code `0` means we take the left branch. This leads directly to the first empty box. So, **I** goes in the box connected to the root node on the left. * **S (code 11):** Starting from the root (11), `1` means take the right branch to node (7). The next `1` means take the right branch from node (7). This leads to the empty box on the far right. So, **S** goes in the box connected to node 7 on the right. * **P (code 101):** Starting from the root (11), `1` means right branch to node (7). `0` means left branch to node (3). `1` means right branch from node (3). This leads to the empty box next to M. So, **P** goes in the box connected to node 3 on the right. 3. **Check M (code 100):** Root(11) -> right(1) to node(7) -> left(0) to node(3) -> left(0) to M. This matches the diagram.

Full Answer

The completed tree should have: - **I** in the box connected to the node with value 7. - **P** in the box connected to the node with value 3. - **S** in the box to the right of M, also connected to the node with value 3.
In a Huffman tree, the binary code for a character is determined by the path taken from the root node to the leaf node containing that character. By convention, a left branch is assigned a `0` and a right branch is assigned a `1`. Let's trace the paths for the given codes: - **I (code 0):** From the root node (11), we take a single left branch (`0`). This leads to the leaf node on the left. Therefore, **I** belongs in the top-left box. - **P (code 101):** 1. From the root (11), take the right branch (`1`) to the node (7). 2. From node (7), take the left branch (`0`) to the node (3). 3. From node (3), take the right branch (`1`) to the leaf node. Therefore, **P** belongs in the box to the right of M. - **S (code 11):** 1. From the root (11), take the right branch (`1`) to the node (7). 2. From node (7), take the right branch (`1`) to the leaf node. Therefore, **S** belongs in the box on the far right. The completed diagram is: <svg width="700" height="700" xmlns="http://www.w3.org/2000/svg"><style>text{font-family: Arial, sans-serif; font-size: 24px; text-anchor: middle; dominant-baseline: middle;}</style><circle cx="350" cy="150" r="20" fill="none" stroke="black" stroke-width="2"/><text x="350" y="150">11</text><line x1="350" y1="170" x2="250" y2="250" stroke="black" stroke-width="2"/><line x1="350" y1="170" x2="450" y2="250" stroke="black" stroke-width="2"/><text x="290" y="200">0</text><text x="410" y="200">1</text><rect x="200" y="250" width="100" height="50" fill="none" stroke="black" stroke-width="2"/><text x="250" y="275">I</text><circle cx="450" cy="250" r="20" fill="none" stroke="black" stroke-width="2"/><text x="450" y="250">7</text><line x1="450" y1="270" x2="350" y2="350" stroke="black" stroke-width="2"/><line x1="450" y1="270" x2="550" y2="350" stroke="black" stroke-width="2"/><text x="390" y="300">0</text><text x="510" y="300">1</text><circle cx="350" cy="350" r="20" fill="none" stroke="black" stroke-width="2"/><text x="350" y="350">3</text><rect x="500" y="350" width="100" height="50" fill="none" stroke="black" stroke-width="2"/><text x="550" y="375">S</text><line x1="350" y1="370" x2="250" y2="450" stroke="black" stroke-width="2"/><line x1="350" y1="370" x2="450" y2="450" stroke="black" stroke-width="2"/><text x="290" y="400">0</text><text x="410" y="400">1</text><rect x="200" y="450" width="100" height="50" fill="none" stroke="black" stroke-width="2"/><text x="250" y="475">M</text><rect x="400" y="450" width="100" height="50" fill="none" stroke="black" stroke-width="2"/><text x="450" y="475">P</text></svg>

Common mistakes

✗ Placing characters in the wrong boxes. ✗ Confusing the left (0) and right (1) branches. ✗ Not starting from the root node for each character.

Practice the full AQA GCSE Computer Science Paper 2

46 questions · hints · full answers · grading

More questions from this exam