Data Str & Algorithm HW Solutions 39

Data Str & Algorithm HW Solutions 39 - n containing...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
39 The average code length is 3.23445 5.22 The set of sixteen characters with equal weight will create a Huffman coding tree that is complete with 16 leaf nodes all at depth 4. Thus, the average code length will be 4 bits. This is identical to the f xed length code. Thus, in this situation, the Huffman coding tree saves no space (and costs no space). 5.23 (a) By the pre f x property, there can be no character with codes 0, 00, or 001x where “x” stands for any binary string. (b) There must be at least one code with each form 1x, 01x, 000x where “x” could be any binary string (including the empty string). 5.24 (a) Q and Z are at level 5, so any string of length n containing only Q’s and Z’s requires 5 n bits. (b) O and E are at level 2, so any string of length
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: n containing only Os and Es requires 2 n bits. (c) The weighted average is 5 5 + 10 4 + 35 3 + 50 2 100 = 2 . 7 bits per character 5.25 This is a straightforward modi f cation. // Build a Huffman tree from minheap h1 template <class Elem>HuffTree<Elem>* buildHuff(minheap<HuffTree<Elem>*, HHCompare<Elem> >* hl) { HuffTree<Elem> *temp1, *temp2, *temp3; while(h1->heapsize() > 1) { // While at least 2 items hl->removemin(temp1); // Pull first two trees hl->removemin(temp2); // off the heap temp3 = new HuffTree<Elem>(temp1, temp2); hl->insert(temp3); // Put the new tree back on list delete temp1; // Must delete the remnants delete temp2; // of the trees we created } return temp3; }...
View Full Document

Ask a homework question - tutors are online