Presentation is loading. Please wait.

Presentation is loading. Please wait.

Huffman code uses a different number of bits used to encode characters: it uses fewer bits to represent common characters and more bits to represent rare.

Similar presentations


Presentation on theme: "Huffman code uses a different number of bits used to encode characters: it uses fewer bits to represent common characters and more bits to represent rare."— Presentation transcript:

1 Huffman code uses a different number of bits used to encode characters: it uses fewer bits to represent common characters and more bits to represent rare characters. Huffman codes are used for compressing data files. Huffman Code

2 Binary tree where characters are at leafs. To determine character code you form a binary string by traversing the tree from the root node to a leaf, e.g. a = 000 b = 001 Huffman Tree

3 Traverse Huffman tree starting from the root until you reach a letter. When you reach a letter print it and start traversing from the root again. 000001100 a b e Decoding Huffman Bit String

4 1)Count the number of times each character appears in the sentence (character weight) 2)Build a priority queue containing TreeNodes initialized with character and its weight such that lowest weight characters are at the top 3)While the priority queue has more than one element -Pop two elements from the priority queue -Combine them into a binary tree in which the weight of the tree root is the sum of the weights of its children -Insert the newly created tree back into the priority queue Building Huffman Tree

5 Write a program that: -reads a sentence from cin -Builds a Huffman tree and encodes the sentence into Huffman code string Encode(const string& message, TreeNode *& huffman); -Decodes the Huffman code back into a sentence string Decode(const string& code, const TreeNode *& huffman); Exercise: Huffman Codec

6 Encode function: -Use the code from the previous lab to count character appearances and build the priority queue -Build Huffman tree by rearranging the priority queue -Write a string HuffmanCode(char letter, const TreeNode *& huffman) function that returns Huffman code of a specified character by walking the Huffman tree -For each character in the sentence call HuffmanCode and concatenate the result. Tips: Huffman Encoder

7 Read chapter 8, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -2%.Assignment


Download ppt "Huffman code uses a different number of bits used to encode characters: it uses fewer bits to represent common characters and more bits to represent rare."

Similar presentations


Ads by Google