The first step in Huffman's approach is to create a series of source reductions by ordering the probabilities of the symbols under consideration and combining the lowest probability symbols into a single symbol that replaces them in the next source reduction.
The second step is to code each reduced source, starting with the smallest source and working back to original source.
Huffman coding is variable-length coding.
|character||No. of occurrences||probability|
To decode the stream, start at the root of the encoding tree, and follow a left-branch for a 0, a right branch for a 1. When you reach a leaf, write the character stored at the leaf, and start again at the top of the tree. (Very slow, not used in practice)
If we have an image file like:
20 20 20 30 30
40 40 40 40 40
50 50 50 ......
40 (40%) ____________________0____
20 (24%) ___________10___ |_____1___
50 (23%) ___110___ |____1___ |
30 (16%) ___111___|--11-- |
The matlab implementation can be found in file exchange.