Huffman tree structure and Huffman encoding 06
Huffman coding code implementation for students beginners data structure may be some difficulties, there is no need to lose heart, nothing in fact, learning is like climbing a mountain and one, after every time we overcome a difficulty, back view, but also such a thing. We need to do is to continue to climb the mountain on learning constantly better their own past. In particular, we programmers, fall behind, China does not lack is people, certainly no shortage of substitute your programmer, there is no more popular the older programmers, China's old programmers all go? Either turn management, career change or be eliminated, of course, there is a small part of a technical expert continues to be active in IT circles. Era of progress, technology is constantly updating the update, but the computer still some inherent things have not changed, the programming language is only weapons in our hands, we can only put to practice good internal strength, then took the weapon can be invincible! Data structures and algorithms is the internal strength of our programmers practice on the one hand.
Suppose there are n weights [W1, W2, ... .WN] , configured with n binary tree leaves, each leaf weight is one of the n weights, the binary tree may be configured so that a number, which must be a it is the minimum weighted length path, called optimal binary tree or a binary tree Huffman tree.
1. Construction Huffman
Here is my own personally constructed Huffman tree process (please ignore the word embarrassed):
2. Huffman coding
First, we look good Huffman tree structure left paths through :( is 0, 1 via the right path)
there are A: 11 B: 001 C: 011 D: 101, you can write their own later.
] WPL (A) = 2x5 = 10;
] WPL (B) = 3x5 = 15;
] WPL © = 3x4 = 12 is;
] WPL (D) = 3x3 =. 9;
] WPL (E) = 4x2 =. 8;
] WPL (F.) = 4x2 = . 8;
] WPL (G) = 4x2 =. 8;
] WPL (H) = 4X1 =. 4;
] WPL (the I) = = 4X1. 4;
] WPL (J) = 4X1 =. 4;
WPL = WPL (A) + WPL (B) + ... + WPL (J) = 82;
therefore weighted path length of the tree is the WPL 82 on Tuhafuman
The next section, using the code Huffman tree.