12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

190 Chap. 5 Binary TreesLetter C D E K L M U ZFrequency 32 42 120 7 42 24 37 2Figure 5.24 The relative frequencies for eight selected letters.the weighted path length of a leaf <strong>to</strong> be its weight times its depth. The binary treewith minimum external path weight is the one with the minimum sum of weightedpath lengths for the given set of leaves. A letter with high weight should have lowdepth, so that it will count the least against the <strong>to</strong>tal path length. As a result, anotherletter might be pushed deeper in the tree if it has less weight.The process of building the Huffman tree for n letters is quite simple. First,create a collection of n initial Huffman trees, each of which is a single leaf nodecontaining one of the letters. Put the n partial trees on<strong>to</strong> a min-heap (a priorityqueue) organized by weight (frequency). Next, remove the first two trees (the oneswith lowest weight) from the heap. Join these two trees <strong>to</strong>gether <strong>to</strong> create a newtree whose root has the two trees as children, <strong>and</strong> whose weight is the sum ofthe weights of the two trees. Put this new tree back on the heap. This process isrepeated until all of the partial Huffman trees have been combined in<strong>to</strong> one.Example 5.8 Figure 5.25 illustrates 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 <strong>to</strong>be the first trees joined <strong>to</strong>gether. 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 <strong>to</strong> take values 9<strong>and</strong> 24 off the heap (corresponding <strong>to</strong> the partial tree with two leaf nodesbuilt in the last step, <strong>and</strong> the partial tree s<strong>to</strong>ring the letter M, respectively)<strong>and</strong> join them <strong>to</strong>gether. The resulting root node has weight 33, <strong>and</strong> so thistree is placed back in<strong>to</strong> the heap. Its priority will be between the trees withvalues 32 (for letter C) <strong>and</strong> 37 (for letter U). This process continues until atree whose root has weight 306 is built. This tree is shown in Figure 5.26.Figure 5.27 shows an implementation for Huffman tree nodes. This implementationis similar <strong>to</strong> the VarBinNode implementation of Figure 5.10. There is anabstract base class, named HuffNode, <strong>and</strong> two subclasses, named LeafNode

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

Saved successfully!

Ooh no, something went wrong!