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.

Sec. 5.6 Huffman Coding Trees 193/** Binary tree node implementation with just an elementfield (no key) <strong>and</strong> pointers <strong>to</strong> children */class HuffNode implements BinNode {private E element;// Element for this nodeprivate HuffNode left; // Pointer <strong>to</strong> left childprivate HuffNode right; // Pointer <strong>to</strong> right child}/** Construc<strong>to</strong>rs */public HuffNode() {left = right = null; }public HuffNode(E val){ left = right = null; element = val; }public HuffNode(E val, HuffNode l, HuffNode r){ left = l; right = r; element = val; }/** Return <strong>and</strong> set the element value */public E element() { return element; }public void setElement(E v) { element = v; }/** Return <strong>and</strong> set the left child */public HuffNode left() { return left; }public HuffNode setLeft(HuffNode p){ return left = p; }/** Return <strong>and</strong> set the right child */public HuffNode right() { return right; }public HuffNode setRight(HuffNode p){ return right = p; }/** Return true if this is a leaf node */public boolean isLeaf(){ return (left == null) && (right == null); }Figure 5.27 Implementation for Huffman tree nodes. Internal nodes <strong>and</strong> leafnodes are represented by separate classes, each derived from an abstract base class.have selected node V in place of node X as the child of node U. However, this isimpossible because l 1 <strong>and</strong> l 2 are the letters with least frequency.✷Theorem 5.3 Function buildHuff builds the Huffman tree with the minimumexternal path weight for the given set of letters.Proof: The proof is by induction on n, the number of letters.• Base Case: For n = 2, the Huffman tree must have the minimum externalpath weight because there are only two possible trees, each with identicalweighted path lengths for the two leaves.

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

Saved successfully!

Ooh no, something went wrong!