Dr. O.Bushehrian ALGORITHM DESIGN HUFFMAN CODE
Fixed length code a: 00b: 01c: 11 Given this code, if our file is ababcbbbc our encoding is Variable length code: a: 01b: 1c:00 our encoding is
Prefix Codes In a prefix code no codeword for one character constitutes the beginning of the codeword for another character a: 0 b: 10 c: 11
Suppose our character set is {a,b,c,d,e,f} and each character appears in the file the number of times indicated in The following table. This table also shows three different codes we could use to encode the file. CharacterFrequencyC1(Fixed-length)C2(Variable-length)C3(Huffman) a b c d e f
C1 code tree: f: 25 d: a: 16 e: 10 b: 5 c:
C2 code tree: 1 a: 16 d: 17 f: 25 c: 12 b: 5e:
A Simple Ex: a: 4b: 5c: 6d: 7 ‘ab’:9 b:5 a:4
Huffman's Algorithm for (i = 1; i < = n-1; i++) { remove (Q, p); remove (Q, q); r = new nodetype; r.left = p; r.right = q; r.frequency = p.frequency + q.frequency; insert (Q, r); }
The state of the subtrees, constructed by Huffman's algorithm, after each pass through the for-i loop. The first tree is the state before the loop is entered.