Skip to main content
. 2021 Dec 30;24(1):65. doi: 10.3390/e24010065
Algorithm 1: Huffman Code

Input: P={p(1),p(2),,p(m)},G={1,2,,m}

 1  If |P|=1,C(1):=0
 2  Otherwise, while |P|>1
   –   define
      ∗  I:=argmin{p(i)P:iG}
      ∗  J:=argmin{p(i)P{p(k):kI}:iG}
      ∗  H:=IJ
   –   If |H|=2 set l(h):=0hH,C(I):=0 and C(J):=1
   –   Otherwise, if hI,C(h):=0C(h) and if hJ,C(h)=1C(h).
   –   Set I:=H, P:={p(k):kI}, p(I)=kIp(k), G={k:kI}
   –   Define P:={PP}{p(I)},G:={GG}{I} and return to 1.
Output: {C(1),C(2),,C(m)}