Hauffman Coading
Hauffman Coading
Initial string
Each character occupies 8 bits. There are a total of 15 characters in the above string.
Thus, a total of 8 * 15 = 120 bits are required to send this string.
Using the Huffman Coding technique, we can compress the string to a smaller size.
Huffman coding first creates a tree using the frequencies of the character and then
generates code for each character.
Once the data is encoded, it has to be decoded. Decoding is done using the same tree.
Huffman Coding prevents any ambiguity in the decoding process using the concept
of prefix code ie. a code associated with a character should not be present in the
prefix of any other code. The tree created above helps in maintaining the property.
Huffman coding is done with the help of the following steps.
1. Calculate the frequency of each character in the string.
Frequency of string
8. For each non-leaf node, assign 0 to the left edge and 1 to the right
edge.
A 5 11 5*2 = 10
B 1 100 1*3 = 3
C 6 0 6*1 = 6
D 3 101 3*3 = 9
Without encoding, the total size of the string was 120 bits. After encoding the
size is reduced to 32 + 15 + 28 = 75 .
Let 101 is to be decoded, we can traverse from the root as in the figure below.
Decoding