Huffman code
Suppose we want to solve the following problem: finding a way to represent text in binary code in the shortest way possible. It will be optimized, reducing the number of strings to as few as possible to encode text.
We have already seen a way to encode letters, numbers, and notations with ASCII code in Chapter 1, Deep Dive into Cryptography, in the Binary numbers, ASCII code, and notations section. But here, the problem is different: we want to be much quicker and more efficient in terms of encoding the text using as few bits as possible.
So, we can use an elegant method ideated by David Huffman, which operates by implementing a special tree graph.
There are 26 letters in the English alphabet, but not all the letters hold the same frequency in a text. We want to implement a tree graph that is able to codify, for example, six letters: a, o, q, u, y, and z: whose relative frequencies in a given text are as follows:
- a = 20
- o = 28
- q = 4 ...