Huffman Coding:
A variable length encoding algorithm Huffman code was created by American, D. A. Huffman, in 1952. Huffman`s procedure is applicable for both Binary and Non- Binary encoding. It is based on the source symbol probabilities P(xi) i = 1, 2, ... , n. The algorithm is optimal in the sense that the average number of bits it requires to represent the source symbols is a minimum, and also meets the prefix condition. The steps of the Huffman coding algorithm are given below:
For binary encodings, when q = 2 (i.e. X = {0,1}). The algorithm constructs a binary tree, as follows.
p1,..., pm−2, pm−1 + pm.
We obtain a binary tree
The above algorithm can be explain in words without the use of mathematical symbols

A pictorial example are given bellow:

Consider a random variable X taking values in the set X = {1, 2, 3, 4, 5} with probabilities 0.25, 0.25, 0.2, 0.15, 0.15, respectively. Construct a Huffman optimal binary code and ternary code for X. Also Calculate average length of coding.
For binary code:

This code has average length =2*0.25+ 2*0.25+2*0.20+ 3*0.15 + 3*0.15 = 2.3 bits.
For binary code:

This code has an average length of 1*0.25+ 1*0.25+2*0.20+ 2*0.15 + 2*0.15 =1.5 ternary digits.
Home Work:
Suppose that letters i1,...,i5 are emitted with probabilities 0.45, 0.25, 0.2, 0.05, 0.05. Compute the expected word-length for Shannon– Fano and Huffman coding. Illustrate both methods by finding decipherable binary coding in each case
Statlearner
Statlearner