11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 5.6 Huffman Coding Trees 185Letter Freq Code BitsC 32 1110 4D 42 101 3E 120 0 1K 7 111101 6L 42 110 3M 24 11111 5U 37 100 3Z 2 111100 6Figure 5.31 The Huffman codes for the letters of Figure 5.24.nodes in the tree were deeper, we could reduce their weighted p<strong>at</strong>h length byswapping them with w 1 or w 2 . But the lemma tells us th<strong>at</strong> no such deepernodes exist. Call T ′ the Huffman tree th<strong>at</strong> is identical to T except th<strong>at</strong> nodeV is replaced with a leaf node V ′ whose weight is w 1 + w 2 . By the inductionhypothesis, T ′ has minimum external p<strong>at</strong>h length. Returning the children toV ′ restores tree T, which must also have minimum external p<strong>at</strong>h length.Thus by m<strong>at</strong>hem<strong>at</strong>ical induction, function buildHuff cre<strong>at</strong>es the Huffmantree with minimum external p<strong>at</strong>h length.✷5.6.2 Assigning <strong>and</strong> Using Huffman CodesOnce the Huffman tree has been constructed, it is an easy m<strong>at</strong>ter to assign codesto individual letters. Beginning <strong>at</strong> the root, we assign either a ‘0’ or a ‘1’ to eachedge in the tree. ‘0’ is assigned to edges connecting a node with its left child, <strong>and</strong>‘1’ to edges connecting a node with its right child. This process is illustr<strong>at</strong>ed byFigure 5.26. The Huffman code for a letter is simply a binary number determinedby the p<strong>at</strong>h from the root to the leaf corresponding to th<strong>at</strong> letter. Thus, the codefor E is ‘0’ because the p<strong>at</strong>h from the root to the leaf node for E takes a single leftbranch. The code for K is ‘111101’ because the p<strong>at</strong>h to the node for K takes fourright branches, then a left, <strong>and</strong> finally one last right. Figure 5.31 lists the codes forall eight letters.Given codes for the letters, it is a simple m<strong>at</strong>ter to use these codes to encode <strong>at</strong>ext message. We simply replace each letter in the string with its binary code. Alookup table can be used for this purpose.Example 5.9 Using the code gener<strong>at</strong>ed by our example Huffman tree,the word “DEED” is represented by the bit string “10100101” <strong>and</strong> the word“MUCK” is represented by the bit string “111111001110111101.”Decoding the message is done by looking <strong>at</strong> the bits in the coded string fromleft to right until a letter is decoded. This can be done by using the Huffman tree in

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!