Huffman tree and construction method of Huffman coding

Knowledge points:

Nodes with smaller weights are to the left of nodes with larger weights

The value on the root node of the left index must be less than the value on the root node of the right index

Huffman encoding and decoding are both unique

Decoding prefix encoding with Huffman tree:
means that the encoding of any character is not a prefix of the encoding of another character in the same character set.
Huffman coding is a type of prefix code.

The decoding process is the inverse of the encoding process:

Starting from the root of the Huffman tree, each bit of the binary code is discriminated from left to right,

If it encounters 0, choose the left branch to go to the next node;

If it encounters 1, select the right branch to go to the next node until it reaches a leaf node, and then the corresponding character is obtained.
 

Question 1:

It is known that 8 characters are used in an information communication contact: a, b, c, d, e, f, g and h, and the frequency of use of each character is: 6, 30, 8, 9, 15, 24, 4 and 12, try to design the Huffman encoding of each character

Problem solving ideas:

(1) Try to design the Huffman encoding of each character:

Let's look at the numbers given in the title first, and generally put the smaller numbers at the bottom and on the left.

First 4 and 6 make up 10

Then find the next smallest and smallest number, then 8 and 9, and then form a binary tree, the root is 17

Find the smallest and next smallest among the remaining numbers, which is 12, 12 and the smallest number 10 to form 22

Find the smallest and second smallest number, which is 15. It should be placed between 12 and 17, because the smallest number is on the left, and 15 and 17 form 32.

Then find the smallest and second smallest number, which is 24, 24 and 22 form 46

 Now there are only 30 left unallocated, so 30 and 32 make up 62

 This is clear at a glance, so 46 and 62 form 108, and this Huffman tree comes out

(2) Please encode for aabcffh 

Write the letters corresponding to the numbers below

 Then how about Huffman coding? Write 0 1 first, remember, the left branch is 0, the right branch is 1

Next, it can be seen from the above figure that the Huffman encoding is:

a:0001       b:10       c:1110       d:1111        e:110         f:01          g:0000           h:001

So aabcffh is encoded as: 0001 0001 10 1110 01 01 001

(3) Please translate the following codes:

00011111110010010000    ----------------->    adefhg
000100011011100101001    ------------------>    aabcffh

The string found in the decoding must be unique, such as the secret message sent in the war years

For example, people say that the counterattack is tomorrow, but what you translate is that you can both counterattack tomorrow and retreat tomorrow. Is that not a big problem?

Question two: 

Assuming that the character set used in the communication message is {a,b,c,d,e,f}, the frequency of each character in the message is:

0.34, 0.05, 0.12, 0.23, 0.08, 0.18, try to design Huffman encoding for these 6 characters.

(1) Please draw the constructed Huffman tree (the weight of the left child node in the tree is required to be less than the weight of the right child node)

Answer: The idea of ​​solving the problem is the same as the previous one. Let's first find the smallest and second smallest in the numbers.

First, 0.05 and 0.08 form 0.13

Then find the smallest and second decimals, which are 0.12 and 0.13, forming 0.25

Then find the smallest and next smallest among the remaining numbers, which are 0.18 and 0.23,

Next, 0.34, 0.41, and 0.25 are left unallocated, so 0.34 and 0.25 form 0.59

 Then there are 0.41 and 0.59 left, and because 0.41 is smaller than 0.59, it is to the left of 0.59

(2) Write the code corresponding to each character separately (requiring the left branch to represent the character "0" and the right branch to represent the character "1")

 

a:11           b:11010          c:100          d:01          e:1011          f:00 

 

Guess you like

Origin blog.csdn.net/rej177/article/details/124065594