Huffman coding (understood)

Huffman coding (Huffman Coding), also known as Huffman coding, coding is a variable word length coding (VLC) a. Huffman proposed in 1952, an encoding method, which is completely based on the probability of occurrence of characters to construct a different average length of the shortest prefix code word, sometimes referred to as the best encoding, usually called Huffman coding (also sometimes referred to as Hough Manchester encoding).

Huffman coding, the main purpose is to maximize the frequency of use save character (code) of the storage space .

 

Easy to understand is, if I have A, B, C, D, E five characters, frequency (ie, weight) were 5,4,3,2,1 arise, then we take the first step, the two smallest left and right subtrees weights as a new tree structure, i.e., 1,2 configuration taking new tree, which is node 2 + 1 = 3, as shown:

 

The dashed line is newly generated node, the second step and then a weight of the newly generated node 3 is set into the rest, the set becomes {5,4,3,3}, then according to the second step , taken two smallest weight to form a new tree, as shown:

 

In turn build the Huffman tree, as shown below:

 

Wherein each weight value corresponding to the replacement character is the following:

 

Therefore, the code corresponding to each character: A-> 11, B-> 10 , C-> 00, D-> 011, E-> 010
Huffman coding is a prefix-free code. It will not be confused when decoding. The main applications in data compression, encryption and decryption and other occasions.

If you take into account the further save storage space, the probability of occurrence should be large (multi-accounting) character is encoded as few 0-1, which is closer to the root (less node), which is optimal binary tree - Huff Man tree.
why? -----> big weights in the upper small weights in the lower layer. Appear to meet the high frequency code length.

 

Reprinted from above https://blog.csdn.net/qq_36653505/article/details/81701181

Guess you like

Origin www.cnblogs.com/-citywall123/p/11297523.html
Recommended