Download Huffman Encoding, Properties of Binary Tree - Data Stuctures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity! 1 Huffman Encoding Huffman code is method for the compression for standard text documents. It makes use of a binary tree to develop codes of varying lengths for the letters used in the original message. Huffman code is also part of the JPEG image compression scheme. The algorithm was introduced by David Huffman in 1952 as part of a course assignment at MIT. Docsity.com 2 Huffman Encoding To understand Huffman encoding, it is best to use a simple example. Encoding the 32-character phrase: "traversing threaded binary trees", If we send the phrase as a message in a network using standard 8-bit ASCII codes, we would have to send 8*32= 256 bits. Using the Huffman algorithm, we can send the message with only 116 bits. Docsity.com 5 Huffman Encoding Original text: traversing threaded binary trees size: 33 characters (space and newline) NL : 1 SP : 3 a : 3 b : 1 d : 2 e : 5 g : 1 h : 1 i : 2 n : 2 r : 5 s : 2 t : 3 v : 1 y : 1 Docsity.com 6 Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 is equal to sum of the frequencies of the two children nodes. Docsity.com 7 Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 There a number of ways to combine nodes. We have chosen just one such way. Docsity.com 10 Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 6 Docsity.com 11 Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 8 6 9 10 Docsity.com 12 Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 8 6 14 9 19 10 Docsity.com 15 Huffman Encoding Make a new node out of these two, and make the two nodes its children. This new node is assigned the sum of the frequencies of its children. Continue the process of combining the two nodes of lowest frequency until only one node, the root, remains. Docsity.com 16 Huffman Encoding Start at the root. Assign 0 to left branch and 1 to the right branch. Repeat the process down the left and right subtrees. To get the code for a character, traverse the tree from the root to the character leaf node and read off the 0 and 1 along the path. Docsity.com 17 Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 8 6 14 9 19 10 33 1 0 Docsity.com 20 Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 8 6 14 9 19 10 33 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 Docsity.com 21 Huffman Encoding Huffman character codes NL 10000 SP 1111 a 000 b 10001 d 0100 e 101 g 10010 h 10011 i 0101 n 0110 r 110 s 0111 t 001 v 11100 y 11101 • Notice that the code is variable length. • Letters with higher frequencies have shorter codes. • The tree could have been built in a number of ways; each would yielded different codes but the code would still be minimal. Docsity.com 22 Huffman Encoding Original: traversing threaded binary trees Encoded: 00111000011100101110011101010110100 10111100110011110101000010010101001 11110000101011000011011101111100111 010110101110000 t r a v e Docsity.com 25 Properties of Binary Tree Property: A binary tree with N internal nodes has N+1 external nodes. Docsity.com 26 Properties of Binary Tree A binary tree with N internal nodes has N+1 external nodes. D F B C G A E F E internal nodes: 9 external nodes: 10 external node internal node Docsity.com 27 Properties of Binary Tree Property: A binary tree with N internal nodes has 2N links: N-1 links to internal nodes and N+1 links to external nodes. Docsity.com