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 cbdegecb, the earlier letter first on a tie?

the answer, and why

the answer is A

In letter order the weights are b 2, c 2, d 1, e 2, g 1. The sort lines them up lightest first, the earlier letter first on a tie, with 7 comparisons.

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 numbersthe test page's numbers

question 2

How many joins does the method make on gcbcgehba?

the answer, and why

the answer is D

gcbcgehba has 6 different letters, a, b, c, e, g, h. Each join leaves one tree fewer, from 6 to 1.

if you chose another

  • That counts the different letters; the first join already leaves one tree fewer.
  • That counts the letters of the text.
  • That is the comparisons the sort makes.

new numbersthe test page's numbers

question 3

Which word makes the sort compare the most?

the answer, and why

the answer is A

The sort compares 5 times on cgbeeebb, the most of the four.

if you chose another

  • On that text it compares 4 times, fewer than 5.
  • On that text it compares 4 times, fewer than 5.
  • On that text it compares 4 times, fewer than 5.

new numbersthe test page's numbers