Assignment 04 - Width of the tree binary tree

1. Learning summary

1.1 Tree structure mind map

1.2 Tree structure learning experience

1. A tree is a nonlinear structure, and its definition is recursive. In the learning process, you need to pay attention to recursive calls.

2. Difficulty: There is no way of thinking when building and solving problems.

3. Solve Huffman codes and solve equivalence problems.

2.PTA experiment work

2.1 Topic 1:

6-3 Preorder output leaf nodes

2.2 Design ideas (pseudo code or flowchart)

void PreorderPrintLeaves(BinTree BT)
{
if (whether BT is empty)

return;
if (judging whether it is a leaf node)
printf (output leaf node);
recursive above loop
return;
}

2.3 Code screenshots

2.4 PTA Submission List Instructions.

2.1 Topic 2:

6-4 jmu-ds-expression tree

2.2 Design ideas (pseudo code or flowchart)

double EvaluateExTree(BTree T){

a,b, assignment

a=EvaluateExTree(T->lchild);
b=EvaluateExTree(T->rchild);
switch(calculation of judgment tree)
{
case '+':
return a+b;
case '-':
return ab;
case '*' :
return a*b;
case '/':
 return a/b;
}

}

2.3 Code screenshots

 

 

2.4 PTA Submission List Instructions.

2.1 Topic 3:

7-1 Restore Binary Tree

2.2 Design ideas (pseudo code or flowchart)


BinTree CreateTree(char *pre,char *in,int n)
{
BinTree T;
T->data=*pre;
T->Left :

T->Right : //The pre-order and in-order traversal are judged and saved in T
return T;
}

int GetHeight(BinTree T)
{
if(tree is empty) return 0;
l=GetHeight(T->Left);
r=GetHeight(T->Right);
store height max
return max+1;
}

2.3 Code screenshots

2.4 PTA Submission List Instructions.

 

3. Screenshot of the PTA final ranking of this week's topic set

3.1 Screenshot of PTA ranking

3.2 My total score: 1.5

4. Read the code (required)

width of binary tree

1 int treeWidth(Bitree *root){
 2   if(!root){
 3     return 0;
 4   }
 5   int width = 0;
 6   int maxWidth = 0;
 7   queue<Bitree *> Q;
 8   Bitree *p = nullptr;
 9   Q.push(root);
10   while(!Q.empty()){
11     width = Q.size();
12     if(maxWidth < width){
13       maxWidth = width;
14     }
15     for(int i=0; i<width; i++){
16       p = Q.front();
17       Q.pop();
18       if(p->left){
19         Q.push(p->left);
20       }
21       if(p->right){
22         Q.push(p->right);
23       }
24     }
25   }
26   return maxWidth;
27 } 

Code function: Find the height of the binary tree

Advantages: You can clearly know the number of nodes in each layer, and naturally know the width of the tree.

5. Screenshot of code Git commit record

 

Guess you like

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