Shannon-Fano coding orders symbols by decreasing probability and recursively splits each consecutive block into two groups with probability totals as close as possible, appending bits zero and one. Huffman coding instead repeatedly merges the two least probable nodes, constructs a binary tree upwards, and then assigns the branch bits. Both yield prefix codes, so a concatenation can be decoded without separators; Huffman coding minimizes expected length among binary prefix codes.
For the specified probabilities, the first Shannon-Fano coding split is of mass versus the remaining mass . Split the latter into of mass and of mass , and then split off . One resulting code is
Its expected word length is .
The Huffman coding merges are , , , and . Reading the resulting tree gives the same code lengths , and it may give exactly the displayed code if the branch bits are chosen accordingly. Its expected length is also bits. The methods agree here; their agreement on this particular distribution is not a claim that the two procedures always coincide.
For Shannon-Fano coding, order the symbols by decreasing probability and recursively split each current list into two contiguous groups with total probabilities as close as possible. Label the two branches and ; the path to a symbol is its prefix code word. Here the first split is of weight against of weight . The latter splits into and , each of weight . One answer is
For Huffman coding, repeatedly merge the two least-probable nodes and then read the resulting binary tree from the root. Merge to weight , merge this node with to weight , and merge with . The same displayed prefix code results. Its mean length is . Choices at ties and exchange of binary labels yield other equivalent codes. Huffman coding minimizes mean length among binary prefix codes; the top-down Shannon-Fano construction does not have that general optimality guarantee.
In Shannon-Fano coding, a symbol of probability is assigned length
and codewords of those lengths are chosen in lexicographic probability intervals; the Kraft inequality guarantees that a binary prefix code exists. Huffman coding repeatedly merges the two least probable current symbols, builds a binary tree from the resulting merges, and labels its two branches by zero and one. Reading from the root to each leaf gives an optimal prefix code.