Fourth job tree

 

 

 

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

  1. //huffmanCoding.c  
  2. #include <stdio.h>  
  3. #include <limits.h>  
  4. #include <string.h>  
  5. #include <stdlib.h>  
  6. #define N 6  
  7.   
  8. typedef struct huffNode  
  9. {  
  10.     unsigned int weight;   //权重  
  11.     unsigned  int lchild,rchild,parent;   //Left and right child nodes and parent nodes  
  12. }HTNode,*HuffTree;  
  13. typedef char **HuffCode;  
  14.   
  15. //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  
  16. void select(const HuffTree &HT,int n,int &s1,int &s2);  
  17. //HT: Huffman tree, HC: Huffman encoding, w: weight of constructing Huffman tree nodes, n: number of constructing Huffman tree nodes  
  18. void HuffmanCode(HuffTree &HT,HuffCode &HC,int *w,int n);  
  19.   
  20.   
  21. intmain()  
  22. {  
  23.     int i;  
  24.     char key[N] = { '0', 'A', 'B', 'C', 'D', 'E'}; //The 0th element is reserved  
  25.     int w[N] = {0,1,2,4,5,6};  //The 0th element is reserved  
  26.     HuffTree HT;  
  27.     HuffCode HC;  
  28.     HuffmanCode(HT,HC,w,N - 1);  
  29.     for ( i = 1; i < N; i++ )  
  30.     printf("%c:%s\n",key[i],HC[i]);    
  31.     
  32.     printf("\n");      
  33.     return 0;  
  34. }  
  35.   
  36.   
  37.   
  38.   
  39. //Find out the two subscripts of the two nodes with the smallest weight in the array, and save them with s1 and s2 respectively  
  40. void select(const HuffTree &HT,int n,int &s1,int &s2)  
  41. {  
  42.     int i;  
  43.     s1 = s2 = 0;   
  44.     int min1 = INT_MAX; //The minimum value, INT_MAX is defined in <limits.h>  
  45.     int min2 = INT_MAX; // next smallest value  
  46.   
  47.     for ( i = 1; i <= n; ++i )  
  48.     {  
  49.     if ( HT[i].parent == 0 )  
  50.     { //Filter the smallest and next smallest weight subscripts without parent nodes  
  51.         if ( HT[i].weight < min1 )  
  52.         { //if less than min  
  53.         min2 = min1;  
  54.         s2 = s1;  
  55.         min1 = HT[i].weight;  
  56.         s1 = i;  
  57.         }  
  58.         else if ( (HT[i].weight >= min1) && (HT[i].weight < min2) )  
  59.         { //If greater than or equal to the minimum value, and less than the next smallest value  
  60.         min2 = HT[i].weight;  
  61.         s2 = i;  
  62.         }  
  63.         else  
  64.         { // if greater than the next smallest value, do nothing  
  65.         ;  
  66.         }  
  67.     }  
  68.     }  
  69. }  
  70.   
  71. //HT: Huffman tree, HC: Huffman encoding, w: weight of constructing Huffman tree nodes, n: number of constructing Huffman tree nodes  
  72. void HuffmanCode(HuffTree &HT,HuffCode &HC,int *w,int n)  
  73. {  
  74.     int s1;  
  75.     int s2;  
  76.     int m = 2 * n - 1;        //It is easy to know that the Huffman tree constructed with n nodes is 2n-1 nodes  
  77.     int i,c,f,j;  
  78.     char *code;   //temporarily encoded  
  79.     HT = (HuffTree)malloc((m+1)* sizeof(HTNode));   //Unit 0 is not used  
  80.       
  81.   
  82.     for ( i = 1; i <= n; i++ )  
  83.         HT[i] = {w[i],0,0,0}; //Initialize the first n nodes (the original nodes of the Huffman tree)  
  84.       
  85.     for ( i = n + 1; i <= m; i++ )  
  86.     HT[i] = {0,0,0,0};   //n-1 nodes after initialization  
  87.   
  88.     //build Huffman tree  
  89.     for ( i = n + 1; i <= m; i++)  
  90.     {  
  91.     select(HT,i-1,s1,s2); //Find the subscript of the node with the smallest weight among the first i-1 nodes  
  92.     HT[s1].parent = i;  
  93.     HT[s2].parent = i;  
  94.     HT[i].lchild = s1;  
  95.     HT[i].rchild = s2;  
  96.     HT[i].weight = HT[s1].weight + HT[s2].weight;  
  97.     }  
  98.     // Huffman encoding  
  99.     HC = (char **)malloc((n)*sizeof(char *));  
  100.     //temporary code  
  101.     code = ( char *)malloc(n* sizeof( char)); //The 0th unit is used  
  102.     for ( i = 1; i <= n; i++ )  
  103.     {  
  104.     for ( c = i, f = HT[c].parent, j = 0; f != 0; c = HT[c].parent, f = HT[c].parent,  j++ )  
  105.     { //Scan from leaf to root  
  106.         if ( HT[f].lchild == c )   
  107.         {  
  108.         code[j] =  '0';  
  109.         }  
  110.         else if(HT[f].rchild == c)  
  111.         {  
  112.         code[j] =  '1';  
  113.         }  
  114.         else  
  115.         { //otherwise do nothing  
  116.         ;  
  117.         }  
  118.     }  
  119.     code[j] =  '\0';  
  120.     HC[i] = (char *)malloc(strlen(code)*sizeof(char));  
  121.     strcpy(HC[i],code);  
  122.     }  
  123.       
  124. }  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325298984&siteId=291194637