Loading...
Build an optimal prefix-free binary code by repeatedly merging the two least frequent symbols. Greedy is optimal via the greedy choice and optimal substructure.
1procedure HUFFMAN(freqs)2 create min-heap Q from freqs3 while |Q| > 1 do4 x ← extract-min(Q)5 y ← extract-min(Q)6 z ← new node with freq = x.freq + y.freq7 z.left ← x; z.right ← y8 insert(Q, z)9 end while10 return extract-min(Q) // root of tree11end procedure