Huffman coding for non-dyadic distribution
Huffman's algorithm solves the following problem. Given a distribution $P=(p_1,\ldots,p_n)$, find a prefix code $w_1,\ldots,w_n$ which minimizes the expected codeword length $$ \sum_{i=1}^n p_i |w_i|. $$
If $p_i = 2^{-\ell_i}$ then the optimal solution has $|w_i| = \ell_i$, and the resulting expected codeword length is exactly the entropy of $P$, but Huffman's algorithm works for any distribution.
If we call a distribution in which $p_i = 2^{-\ell_i}$ dyadic (there is no common term in the literature), then Huffman's algorithm &quot;rounds&quot; an arbitrary distribution $P$ into a dyadic distribution $Q$ which minimizes the Kullback–Leibler divergence $D(P\|Q)$.
We can get closer to the entropy of $P$ by encoding together groups of symbols. If you encode $m$-tuples of symbols together, then the expected codeword length per symbol (i.e., dividing the expression above by $m$) is at most $H(P) + 1/m$, and in particular, it tends to the entropy as $m \to \infty$. Shannon's source coding theorem shows that you cannot go below the entropy using any encoding scheme, so using Huffman coding in blocks is asymptotically optimal as the block size goes to infinity.