Huffman

powered by FreeFind

Modified: 

Huffman (C)
-- pre:  C is the characters that comprise the alphabet
-- post: return the root of the optimal encoding tree 
   n ¬ |C|
   Q ¬ C    -- insert all the elements of C into Q, using the frequency as the priority
   for i ¬ 1 to n-1 do
		z ¬  a new tree node
		x ¬  Extract-Minimum (Q) 
		y ¬  Extract-Minimum (Q)
		left[ z ] ¬  x
		right[ z ] ¬ y
		f[z] ¬  f[x] + f[y]
		Insert (Q, z)
   return Extract-Minimum (Q)        -- the root of the encoding tree