Let
L be the maximum length any code word is permitted to have. Let
p1, ...,
pn be the frequencies of the symbols of the alphabet to be encoded. We first sort the symbols so that
pi ≤
pi+1. Create
L coins for each symbol, of denominations 2−1, ..., 2−
L, each of numismatic value
pi. Use the package-merge algorithm to select the set of coins of minimum numismatic value whose denominations total
n − 1. Let
hi be the number of coins of numismatic value
pi selected. The optimal length-limited Huffman code will encode symbol
i with a bit string of length
hi. The
canonical Huffman code can easily be constructed by a simple bottom-up greedy method, given that the
hi are known, and this can be the basis for fast
data compression. ==Performance improvements and generalizations==