# STM32F030CT86 timer 3 channel 1 to verify the PWM front and rear cut mode

2022-12-20   ES

# Huffman Tree Introduction

Huffman Tree, also known as the best binary tree, is a tree with the shortest path length. Suppose there is n rights {w1, w2, …, wn}. If a binary tree with n leaf nodes is constructed, the weight of the n leaves nodes is {w1, w2, …, wn, wn, wn }, The smallest binary tree constructed is called the Hofman tree.

Here the concept of the length of the right to the tree. The length of the tree belt path of the tree refers to the sum of the path length of all the leaves in the tree to the root node and the product value of the leaf node. For rights, li means the length of the path from the leaf node to the root node, then the binary tree’s right path length wpl = w1*l1 + w2*l2 + … wn*ln.

Depending on the number of nodes and power, the shape of Herfaman tree is also different. Hiffman tree has the following characteristics:

• For the same group of weights, the Hefman tree that can be obtained is not necessarily unique.
• The left and right sub -trees of the Hefman tree can be interchangeable because this does not affect the length of the tree’s right.
• nodes with weight value are leaf nodes, and nodes without power are the root nodes of a binary tree.
• The larger the node, the closer to the root node of the Hofman tree. The smaller the node, the less the node is away from the root node of the Hefman tree.

The

• Hefman tree is only the nodes with a leaf node and a degree of 2, and there is no node with a degree of 1.
• Hefman tree with n leaf nodes has a total of 2N-1 nodes.

# Huffman Tree Construction

The construction steps of the Hefman tree are as follows:
1. See the given Nable value as a binary tree with only node (no left and right children) to form a collection of HT. The weight of each tree is the weight of the node.
2. Select 2 binary trees with the smallest power from the collection HT to form a new binary tree. Their power is the sum of the weight of the two binary trees.
3. Delete the two binary trees selected in the step 2 from the collection HT, and add the new binary tree obtained in the step 2 to the collection HT.
4. Repeat steps 2 and step 3 until the collection HT contains only one tree. This tree is a Hofman tree.
If given the following 5 rights:
according to the above steps, it can construct the Hefman tree as shown in the left figure below. Of course, it may also construct the Hofman tree shown in the right picture below. This is not the only one.

# huffman coding

Hefman tree is widely used, such as well -known applications in communication electronic texts. When waiting for teleportation, we hope that the total length of the telephone text can be as short as possible, so the design length of each character can be designed, so that more characters appear in the electrical text as possible in the short encoding as possible. In order to ensure that no ambiguity occurs during decoding, we can adopt the encoding method as shown in the figure below:
is the left branch encoding to character 0, and the right branch is encoded as character 1. The character string consisting of the branch characters from the root node to the leaf node is encoded as the leaf node character. According to the above picture above, we can get the Hofman coded by the leaf nodes as follows:
Herfman coding with a node with a node of 5 is: 11
Herfman coding with a node with a node of 4 is: 10

Herfman coding with a node with a node is: 00
The Hofman coding with a node with a node is: 011
Herfman coded with a node with a weight of 1 is: 010
and the picture above on the right, you can get the Hefman code of each leaf node as follows:
Herfman coding with a node with a weight of 5 is: 00
The Hofman coding with a node with a node of 4 is: 01

The Hofman coding with a node with a node is: 10
The Hofman coding with a node with a node is: 110
Herfman coded with a node with a weight of 1 is: 111

# Huffman encoded C implementation

Since there is no node with a degree of 1 in the Hefman tree, a Hofman tree with N-leaf nodes has a total of 2N-1 nodes (the last feature). Therefore 2N-1 one-dimensional array. We can use the following data structures to express the encoding of Hefman tree and Hefman:

[cpp]
view plain
copy
1. /*
2. The storage structure of Hefman tree, it is also a binary tree structure,
3. This storage structure is suitable for both indicating trees and suitable for forests.
4. */
5. typedef struct Node
6. {
7.     int weight;                // rights value
8.     int parent;                // The serial number of the parent node is -1 is the root node
9.     int lchild,rchild;         // The serial number of the child node is -1 is the leaf node
10. }HTNode,*HuffmanTree;          // Used to store all nodes in Hefman Tree
11. typedef char **HuffmanCode;    // Hofman coding used to store each leaf node

According to the steps of the construction of Hefman tree, we can write the code to build a Hofman tree as follows:

[cpp]
view plain
copy
1. /*
2. Based on the Nable value of the given naigo, constructing a Herfman tree, and the nable value is stored in the WET
3. */
4. HuffmanTree create_HuffmanTree(int *wet,int n)
5. {
6.     // A Hofman tree with N leaf nodes has a total of 2N-1 nodes
7.     int total = 2*n-1;
8.     HuffmanTree HT = (HuffmanTree)malloc(total*sizeof(HTNode));
9.     if(!HT)
10.     {
11.         printf(“HuffmanTree malloc faild!”);
12.         exit(-1);
13.     }
14.     int i;
15.     // The following initialization serial numbers are all represented by -1,
16.     // When a serial number of Parent or LChild or Rchild in the encoding function,
17.     // will not be confused with any one in the HT array
18.     /ht [0], htinger and weekend.
19.     for(i=0;i<n;i++)
20.     {
21.         HT[i].parent = -1;
22.         HT[i].lchild = -1;
23.         HT[i].rchild = -1;
24.         HT[i].weight = *wet;
25.         wet++;
26.     }
27.     //ht [n and htinger, htinger, 1]. In the root nodes of each binary tree constructed in the middle
28.     for(;i<total;i++)
29.     {
30.         HT[i].parent = -1;
31.         HT[i].lchild = -1;
32.         HT[i].rchild = -1;
33.         HT[i].weight = 0;
34.     }
35.     int min1,min2; // It is used to save the two weight of each round of the smallest and Parent with 0
36.     // After each round of comparison, choose MIN1 and MIN2 to form a lesson binary tree, and finally form a Hefman tree
37.     for(i=n;i<total;i++)
38.     {
39.         select_minium(HT,i,min1,min2);
40.         HT[min1].parent = i;
41.         HT[min2].parent = i;
42.         // Here the left child and right child can reverse, and it constitutes a Hefman tree, but the codes obtained are different
43.         HT[i].lchild = min1;
44.         HT[i].rchild = min2;
45.         HT[i].weight =HT[min1].weight + HT[min2].weight;
46.     }
47.     return HT;
48. }
The above code is called the Select_minium () function. It indicates that two smallest binary trees are selected from the collection. The code is as follows:

[cpp]
view plain
copy
1. /*
2. Select two Weight of Weight from the former K element of the HT array and two with -1 Parent, and save it in min1 and min2 respectively.
3. */
4. void select_minium(HuffmanTree HT,int k,int &min1,int &min2)
5. {
6.     min1 = min(HT,k);
7.     min2 = min(HT,k);
8. }

The min () function code called here is as follows:

[cpp]
view plain
copy
1. /*
2. Select the minimum and Parent element of -1 from the former K element of the HT array, and return the serial number of the element
3. */
4. int min(HuffmanTree HT,int k)
5. {
6.     int i = 0;
7.     int min;        // The serial number of the element of the minimum and Parent of Weight
8.     int min_weight; // Weight values for storing Weight with the smallest and Parent element of -1
9.     // First give the first Parent element of -1 weight value to min_weight, and stay for comparison in the future.
10.     // Note that you cannot follow the general practice here, first give ht [0] .weight to min_weight,
11.     // Because if the value of HT [0] .weight is relatively small, then it will be selected when the binary tree is constructed for the first time,
12.     // and the comparison of each round of the minimum power constructed binary tree in the subsequent round is still used to judge the value of ht [0] .weight.
13.     // This will be selected again, which will cause logical errors.
14.     while(HT[i].parent != -1)
15.         i++;
16.     min_weight = HT[i].weight;
17.     min = i;
18.     // Select the element of the smallest Weight and Parent to -1, and give it the serial number to the min
19.     for(;i<k;i++)
20.     {
21.         if(HT[i].weight<min_weight && HT[i].parent==-1)
22.         {
23.             min_weight = HT[i].weight;
24.             min = i;
25.         }
26.     }
27.     // After selecting the smallest element of Weight, place it with 1, so that the next comparison will be excluded.

The

28.     HT[min].parent = 1;
29.     return min;
30. }
built a Hefman tree, you can use the Hofman coding. Ask the Hofman encoding, you need to traverse the path from the root node to the leaf nodes. The following two types of traversal of Hefman tree are required to cod. method.
1. Use the Hefman encoding from the leaf node to the root node to the reverse traversal of each character. The code is as follows:

[cpp]
view plain
copy
1. /*
2. From the leaf nodes to the root node, the Hefman encoded in the N -leaf node in HT HT, HT, and stored it in HC
3. */
4. void HuffmanCoding(HuffmanTree HT,HuffmanCode &HC,int n)
5. {
6.     // Poor pointer used to preserve the pointer to each Hefman coding strings
7.     HC = (HuffmanCode)malloc(n*sizeof(char *));
8.     if(!HC)
9.     {
10.         printf(“HuffmanCode malloc faild!”);
11.         exit(-1);
12.     }
13.     // Temporary space, used to save the Hofman coding string every time
14.     // For the Hefman trees with n leaf nodes, the coding length of each leaf node does not exceed N-1
15.     // Add a ‘\ 0’ ending character, so the length of the array of allocated is N
16.     char *code = (char *)malloc(n*sizeof(char));
17.     if(!code)
18.     {
19.         printf(“code malloc faild!”);
20.         exit(-1);
21.     }
22.     code[n-1] = ‘\0’;  // Code ending symbol, which is also the end of the character array
23.     // Seeking the Hofman coding of each character
24.     int i;
25.     for(i=0;i<n;i++)
26.     {
27.         int current = i;           // Define the node of the current access
28.         int father = HT[i].parent; // parent node of the current node
29.         int start = n-1;           // The position of each coding is initially for the position of the coding ending character
30.         // Traversing Hefman tree from the leaf node until the root node
31.         while(father != -1)
32.         {
33.             if(HT[father].lchild == current)   // If it is a left child, the encoding is 0
34.                 code[–start] = ‘0’;
35.             else                              // If it is a right child, the encoding is 1
36.                 code[–start] = ‘1’;
37.             current = father;
38.             father = HT[father].parent;
39.         }
40.         // Code storage space for the coding string of the first character
41.         HC[i] = (char *)malloc((n-start)*sizeof(char));
42.         if(!HC[i])
43.         {
44.             printf(“HC[i] malloc faild!”);
45.             exit(-1);
46.         }
47.         // Copy the encoding string from code to HC
48.         strcpy(HC[i],code+start);
49.     }
50.     free(code); // Release the temporary space of saving coding string
51. }
The five right values of 5, 4, 3, 2, and 1 we give above are as examples. The obtained coding results are as follows:
This is exactly in line with the coded Hofman coding on the left in the two Hefman trees above. Therefore, the Hefman tree constructed by the program is the left side of the picture above.
This method is coded in the order of 5, 4, 3, 2, and 1 in the order of 5, 4, 3, 2, and 1 (that is, the order of the input character), and it is also printed to the terminal in this order.
2. Use root nodes to leaf nodes without stack non -recursively traversing Hefman trees. Seeking the Hefman code of each character, the code is as follows:

[cpp]
view plain
copy
1. /*
2. from the root node to the leaf nodes without stack non -recursively traversing the Hutman tree HT, find the Hofman coding with n leaf nodes, and store it in HC
3. */
4. void HuffmanCoding2(HuffmanTree HT,HuffmanCode &HC,int n)
5. {
6.     // Poor pointer used to preserve the pointer to each Hefman coding strings
7.     HC = (HuffmanCode)malloc(n*sizeof(char *));
8.     if(!HC)
9.     {
10.         printf(“HuffmanCode malloc faild!”);
11.         exit(-1);
12.     }
13.     // Temporary space, used to save the Hofman coding string every time
14.     // For the Hefman trees with n leaf nodes, the coding length of each leaf node does not exceed N-1
15.     // Add a ‘\ 0’ ending character, so the length of the array of allocated is N
16.     char *code = (char *)malloc(n*sizeof(char));
17.     if(!code)
18.     {
19.         printf(“code malloc faild!”);
20.         exit(-1);
21.     }
22.     int cur = 2*n-2;    // The serial number of the node currently traversed, the initial node serial number
23.     int code_len = 0;   // Define the length of the encoding
24.     // After building a good Hofman tree, use Weight to be used as the status logo of each node when traversing the tree
25.     // weight = 0 indicates that the left and right children of the current node have not been traveled yet
26.     // weight = 1 means that the left child of the current node has been traveled, and the right child has not yet been traveled
27.     // weight = 2 means that the left and right children of the current node have been traversed
28.     int i;
29.     for(i=0;i<cur+1;i++)
30.     {
31.         HT[i].weight = 0;
32.     }
33.     // Start from the root node, and finally return to the root node ends
34.     // When the CUR is the parent of the root node, exit the loop
35.     while(cur != -1)
36.     {
37.         // The children are not traveled around, and they traversed to the left first
38.         if(HT[cur].weight == 0)
39.         {
40.             HT[cur].weight = 1;    // indicates that the left child has been traveled through
41.             if(HT[cur].lchild != -1)
42.             {   // If the current node is not a leaf node, write down the encoding and continue to traverse it to the left
43.                 code[code_len++] = ‘0’;
44.                 cur = HT[cur].lchild;
45.             }
46.             else
47.             {   // If the current node is a leaf node, terminate the encoding and save it
48.                 code[code_len] = ‘\0’;
49.                 HC[cur] = (char *)malloc((code_len+1)*sizeof(char));
50.                 if(!HC[cur])
51.                 {
52.                     printf(“HC[cur] malloc faild!”);
53.                     exit(-1);
54.                 }
55.                 strcpy(HC[cur],code);  // Copy the encoding string
56.             }
57.         }
58.         // The left child has been traveled, and the right child began to go to the right
59.         else if(HT[cur].weight == 1)
60.         {
61.             HT[cur].weight = 2;   // indicates that their children have been traveled through
62.             if(HT[cur].rchild != -1)
63.             {   // If the current node is not a leaf node, write down the code and continue to traverse to the right
64.                 code[code_len++] = ‘1’;
65.                 cur = HT[cur].rchild;
66.             }
67.         }
68.         // The children have been traveled around, returned to the parent node, and the coding length is reduced by 1
69.         else
70.         {
71.             HT[cur].weight = 0;
72.             cur = HT[cur].parent;
73.             –code_len;
74.         }
75.     }
76.     free(code);
77. }
This method is different from the method 1. It is based on the structure of Hefman tree to find the encoding of each character. The program structure constructed Hefman tree is shown in the left figure in the figure above. The order of 1, 2, 4, and 5 is encoded by each character in each character, but we print it in the main function in the main function (5, 4, 3, 2, 1) to the terminal.

first edition: only the first encoding method:
Second edition: contains two coding methods, and some code improves some code:

source