Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recitation Nov. 15. HW5: Huffman Encoding/Decoding Task: –Read a text file (i.e. “message.text”) and figure out character frequencies. Don’t forget ‘\n’

Similar presentations


Presentation on theme: "Recitation Nov. 15. HW5: Huffman Encoding/Decoding Task: –Read a text file (i.e. “message.text”) and figure out character frequencies. Don’t forget ‘\n’"— Presentation transcript:

1 Recitation Nov. 15

2 HW5: Huffman Encoding/Decoding Task: –Read a text file (i.e. “message.text”) and figure out character frequencies. Don’t forget ‘\n’ (newline) and other possible characters. –Build a Huffman Tree (use HW 4) for the found character frequencies. –Encode the file using Huffman Encoding –Decode the Encoded message. –Make Sure Decoding matches original! –Compare length of Huffman encoding to bit-size of original.

3 HW5 How to count the frequency of each character? What data-structure do we use for encoding? What data-structure do we use for decoding? What is the size of the original file? What is the size of the Huffman encoding of the file?

4 buildHeap Task: –Start with N items (unordered) and build a heap containing these items. Simple Solution –Start with an empty heap and insert the items one at a time. –Running time O(N log(N)) But, there’s a better way….

5 buildHeap Create a complete binary tree of the items, but do not care about order. –Using array implementation, just put the items into the array. Run time? Now need to recreate the order property: –The child of any node is greater than the parent.

6 buildHeap Start from bottom up –Don’t care about lowest level since the leafs are already good heaps. Use percolateDown() method –percolateDown() property: If T has heap property in all nodes but the root then, after calling percolateDown(), T is a heap. Theorem 1: buildHeap works Proof: Induction on tree size.

7 buildHeap Analyzing the running time: –Perfect binary tree of height h contains 2 h+1 -1 nodes. Theorem: The sum of the heights of the nodes is < 2 h+1 Proof: S = h + 2(h-1) + 4(h-2) +…+2 h-2 (2)+ 2 h-1 (1) = 2 h (1/2 + 2/4 + 3/8 + 4/16 + 5/32 + …+h/2 h ) < 2 h+1

8 D-airy (Dairy) Heap Take 10 mins

9 Solution public int parent(int i) { return (i-1)/d; } public int kthChild(int i, int k) { return d*i + k; }

10 Solution private int smallestChild( int hole ) { int bestChildYet = kthChild(hole, 1); int k = 2; int candidateChild = kthChild(hole, k); while ((k <= d) && (candidateChild < currentSize)) { if (array[candidateChild].compareTo(array[bestChildYet]) < 0) bestChildYet = candidateChild; k++; candidateChild = kthChild(hole, k); } return bestChildYet; }

11 Solution private void percolateUp ( int hole ) { Comparable tmp = array[hole]; for (; hole > 0 && tmp.compareTo(array[parent(hole)] ) < 0; hole = parent(hole)) array[ hole ] = array[ parent(hole) ]; array[ hole ] = tmp; }

12 Solution private void percolateDown( int hole ) { int child; Comparable tmp = array[ hole ]; for( ; kthChild(hole, 1) < currentSize; hole = child ) { child = smallestChild(hole); if( array[ child ].compareTo( tmp ) < 0 ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp; }


Download ppt "Recitation Nov. 15. HW5: Huffman Encoding/Decoding Task: –Read a text file (i.e. “message.text”) and figure out character frequencies. Don’t forget ‘\n’"

Similar presentations


Ads by Google