Skip to content

Huffman coding

planning ahead

Level 3 of 3

Also called Huffman code and Optimal prefix code.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from huffman coding's own run on the numbers shown.

choose one

question 1

How many comparisons does the sort make lining up the letters of deadbeef, the earlier letter first on a tie?

the answer, and why

the answer is D

In letter order a 1, b 1, d 2, e 3, f 1; on a tie, the earlier letter goes first. Round 1: a with b and d with e, 1 each. Round 2: a, b with d, e takes 2, as both of the right run weigh more. Round 3: a, b, d, e with f takes 3, f going after a and b. That is 7.

if you chose another

  • That counts the 8 letters of the text, but the sort lines up the 5 different letters.
  • That is the joins, one fewer than the 5 letters.
  • That counts the different letters, not the comparisons between them.

new numbers

question 2

How many joins does the method make on committee?

the answer, and why

the answer is A

Committee has 6 different letters, c, e, i, m, o and t. Each join leaves one tree fewer, from 6 to 1.

if you chose another

  • That counts the different letters; the first join already leaves 5 trees.
  • That counts the letters of the text.
  • That is the comparisons the sort makes.

new numbers

question 3

Which word makes the sort compare the most?

the answer, and why

the answer is B

Its weights in letter order are 1, 3, 2, 4, the dearest order of 4. Round 1 makes 1 comparison in each of its 2 merges, and round 2 makes 3, so 5.

if you chose another

  • Weights falling, 4, 3, 2, 1, the fewest. 1 in each merge of round 1, then 2, so 4.
  • Weights 1, 2, 3, 4, already in order. 1 in each merge of round 1, then 2, so 4.
  • All weigh 2, and ties go in letter order, already in order. 4.

new numbers