1.2 Tree structure learning experience
Difficult, not me and other ordinary people can learn.
Difficulty: The understanding of recursion is not thorough enough, and it has been unable to turn around.
Solution: Look at the code more.
2.PTA experiment work
Topic 1: 6-4 jmu-ds-expression tree (25 points)
topic:
- Enter a line of infix expressions, transform a binary expression tree, and solve.
- The expression only contains
+
,-
,*
,/
,(
,)
operators, and the operand has only one digit and is an integer (interested students can consider negative decimals and two-digit practice). The binary tree is constructed according to the rules of parentheses, multiplication and division, and addition and subtraction. - As shown in the figure, the "1+(2+3)*2-4/5" algebraic expression corresponds to a binary tree, and the corresponding binary tree is used to calculate the value of the expression. Convert the binary tree as follows:
Ideas:
Build an expression binary tree
Define stack s to store numbers used in operations
Define character stack op to store numeric operation symbols
First put '#' as stack op
while str is not empty
if is a number
Just create a tree node, assign it to the value of str{i}, make the left and right children NULL, and push the stack
else mobilize Precede
Operate on the operators it appears one by one,
with switch
while op's top is not #
Create new tree node
The value assigned to op.top
Give the last two values in the stack to the left and right children respectively
/* Evaluate the expression tree */
Convert all characters to numbers using recursion
while if there are tree nodes
just calculate
4. PTA Submission List Instructions
Topic 2: 6-1 jmu-ds-binary tree operation set (20 points)
This question requires the use of the hierarchical method to create a binary tree. The input sequence of the hierarchical method is formed in the order of the tree from top to bottom and left to right. The empty nodes of each layer are represented by the character #
2. Design ideas:
Create a binary tree from a hierarchical sequence of characters
Define a queue q;
a tree temp
if str is #
just return
else create a tree node
Left and right children are NULL;
assign str[i] to it
enqueue the node
while ! queue
dequeue
Determine whether the last two strings are empty
Not empty: create a new node, assign a value
6-3 Preorder Output Leaf Nodes (15 points)
This problem requires outputting the leaf nodes of a given binary tree in the order of preorder traversal.
2. Design ideas:
if ! BT ends
When left and right children have more NULLs
output node
total score
185
That's 2 points
4. Read the code
- //huffmanCoding.c
- #include <stdio.h>
- #include <limits.h>
- #include <string.h>
- #include <stdlib.h>
- #define N 6
- typedef struct huffNode
- {
- unsigned int weight; //权重
- unsigned int lchild,rchild,parent; //Left and right child nodes and parent nodes
- }HTNode,*HuffTree;
- typedef char **HuffCode;
- //Find out the subscripts of the two nodes with no parent node and the smallest weight in the array, and save them with s1 and s2 respectively
- void select(const HuffTree &HT,int n,int &s1,int &s2);
- //HT: Huffman tree, HC: Huffman encoding, w: weight of constructing Huffman tree nodes, n: number of constructing Huffman tree nodes
- void HuffmanCode(HuffTree &HT,HuffCode &HC,int *w,int n);
- intmain()
- {
- int i;
- char key[N] = { '0', 'A', 'B', 'C', 'D', 'E'}; //The 0th element is reserved
- int w[N] = {0,1,2,4,5,6}; //The 0th element is reserved
- HuffTree HT;
- HuffCode HC;
- HuffmanCode(HT,HC,w,N - 1);
- for ( i = 1; i < N; i++ )
- printf("%c:%s\n",key[i],HC[i]);
- printf("\n");
- return 0;
- }
- //Find out the two subscripts of the two nodes with the smallest weight in the array, and save them with s1 and s2 respectively
- void select(const HuffTree &HT,int n,int &s1,int &s2)
- {
- int i;
- s1 = s2 = 0;
- int min1 = INT_MAX; //The minimum value, INT_MAX is defined in <limits.h>
- int min2 = INT_MAX; // next smallest value
- for ( i = 1; i <= n; ++i )
- {
- if ( HT[i].parent == 0 )
- { //Filter the smallest and next smallest weight subscripts without parent nodes
- if ( HT[i].weight < min1 )
- { //if less than min
- min2 = min1;
- s2 = s1;
- min1 = HT[i].weight;
- s1 = i;
- }
- else if ( (HT[i].weight >= min1) && (HT[i].weight < min2) )
- { //If greater than or equal to the minimum value, and less than the next smallest value
- min2 = HT[i].weight;
- s2 = i;
- }
- else
- { // if greater than the next smallest value, do nothing
- ;
- }
- }
- }
- }
- //HT: Huffman tree, HC: Huffman encoding, w: weight of constructing Huffman tree nodes, n: number of constructing Huffman tree nodes
- void HuffmanCode(HuffTree &HT,HuffCode &HC,int *w,int n)
- {
- int s1;
- int s2;
- int m = 2 * n - 1; //It is easy to know that the Huffman tree constructed with n nodes is 2n-1 nodes
- int i,c,f,j;
- char *code; //temporarily encoded
- HT = (HuffTree)malloc((m+1)* sizeof(HTNode)); //Unit 0 is not used
- for ( i = 1; i <= n; i++ )
- HT[i] = {w[i],0,0,0}; //Initialize the first n nodes (the original nodes of the Huffman tree)
- for ( i = n + 1; i <= m; i++ )
- HT[i] = {0,0,0,0}; //n-1 nodes after initialization
- //build Huffman tree
- for ( i = n + 1; i <= m; i++)
- {
- select(HT,i-1,s1,s2); //Find the subscript of the node with the smallest weight among the first i-1 nodes
- HT[s1].parent = i;
- HT[s2].parent = i;
- HT[i].lchild = s1;
- HT[i].rchild = s2;
- HT[i].weight = HT[s1].weight + HT[s2].weight;
- }
- // Huffman encoding
- HC = (char **)malloc((n)*sizeof(char *));
- //temporary code
- code = ( char *)malloc(n* sizeof( char)); //The 0th unit is used
- for ( i = 1; i <= n; i++ )
- {
- for ( c = i, f = HT[c].parent, j = 0; f != 0; c = HT[c].parent, f = HT[c].parent, j++ )
- { //Scan from leaf to root
- if ( HT[f].lchild == c )
- {
- code[j] = '0';
- }
- else if(HT[f].rchild == c)
- {
- code[j] = '1';
- }
- else
- { //otherwise do nothing
- ;
- }
- }
- code[j] = '\0';
- HC[i] = (char *)malloc(strlen(code)*sizeof(char));
- strcpy(HC[i],code);
- }
- }