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 1813060 1120 186E 0790 110107137U42D42L06532 33C0 19 240 1 M2Z17KFigure 5.26 A Huffman tree for the letters of Figure 5.24.Example 5.8 Figure 5.25 illustr<strong>at</strong>es part of the Huffman tree constructionprocess for the eight letters of Figure 5.24. Ranking D <strong>and</strong> L arbitrarily byalphabetical order, the letters are ordered by frequency asLetter Z K M C U D L EFrequency 2 7 24 32 37 42 42 120Because the first two letters on the list are Z <strong>and</strong> K, they are selected tobe the first trees joined together. 6 They become the children of a root nodewith weight 9. Thus, a tree whose root has weight 9 is placed back on thelist, where it takes up the first position. The next step is to take values 9<strong>and</strong> 24 off the list (corresponding to the partial tree with two leaf nodesbuilt in the last step, <strong>and</strong> the partial tree storing the letter M, respectively)<strong>and</strong> join them together. The resulting root node has weight 33, <strong>and</strong> so thistree is placed back into the list. Its priority will be between the trees withvalues 32 (for letter C) <strong>and</strong> 37 (for letter U). This process continues until <strong>at</strong>ree whose root has weight 306 is built. This tree is shown in Figure 5.26.Figure 5.27 shows an implement<strong>at</strong>ion for Huffman tree nodes. This implement<strong>at</strong>ionis similar to the VarBinNode implement<strong>at</strong>ion of Figure 5.10. There is anabstract base class, named HuffNode, <strong>and</strong> two subclasses, named LeafNode6 For clarity, the examples for building Huffman trees show a sorted list to keep the letters orderedby frequency. But a real implement<strong>at</strong>ion would use a heap to implement the priority queue forefficiency.

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

Saved successfully!

Ooh no, something went wrong!