Blog assignment 04--tree

1. Learning summary (2 points)

1.1 Tree structure mind map

1.2 Tree structure learning experience

The access paths of the recursive operations in the pre-middle and post-order of the tree are as shown in the figure below.

The path of the hierarchical traversal of the tree is as shown in the figure below.

Operation {
the first node in the queue,
while (the queue is not empty)
{
access the node,
if(BT->lchild! =NULL) into the queue.
if(BT->rchild!=NULL) into the queue.
}
}
Non-recursion of three-order traversal (pre-order as an example):
Operation: {
Push into the root node of the stack tree;
while (the stack is not empty) {
Access
if (BT->rchild!=NULL) Push into the stack.
if(BT->lchild !=NULL) is pushed onto the stack.
}
} Preorder
and inorder tree building process:
Front: ABCEFD
Middle: BCEFAD
The first A in the preorder: indicates that it is the root node
Left front tree: BCEF Left middle: BCEF
Right front tree: D Right middle: D
Repeatedly find the left and right trees.
The result is as follows :

Huffman tree,
such as building a tree in 1357:

careful observation will find that the sum of its wpl and non-leaf nodes is equal.
Mathematical proof: set abcde several nodes to build a Huffman tree (assuming that each node is added later than Larger node)
a+b is the first non-root node, then a+b+c is the second and so on
It will be found that the sum of non-leaf nodes is e+2d+3c+4b+4a
and wpl also happens to be this value.
It is not accidental. This can be considered from the structure of the Huffman tree, because each incremental layer of nodes that have been established is added. 1 For example, if the Huffman tree has three layers, then the sum of the bottom layer will be added in the second layer and also in the first layer, that is, it will be added
twice, which is exactly the same as the algorithm of the wpl construction number. Therefore, the greedy algorithm can be used to quickly obtain wpl.
A tree is a non-linear structure.
The tree structure is not only useful in itself, but also reflects the abstract structure of many computing processes;
the nodes of the tree structure form a hierarchical structure;
recursion is its focus, but the operation of recursion is understood It's really difficult to get up, so you have to look at other people's code to learn.

2. PTA lab work (4 points)

2.1 Topic 1: Repairing the ranch

1 Design ideas (pseudo code or flowchart)

定义一个队列可以让进队元素按从大到小排列
for(i=0;i<N;I++){
依次输入每一个数
并且入队
}
while(队不空){
出队两个元素ab
并让total=a+b;
再进队两个元素。
}
输出结果

2. Code screenshot

3. PTA Submission List Instructions

2.1 Topic 2: Circle of Friends

1 Design ideas (pseudo code or flowchart)

//定义三个数组一个是保留每一个每一个数对应的根节点一个保留所有根节点
//最后一个保留每一个根对应的孩子数
初始化树中的每个节点数值为-1
先输入孩子数和朋友圈数n,m
for(int i=0;i<m;i++){
输入每个朋友圈中人的个数
并且保留它们的根节点
并且记录每个下标对应的根节点
当这个根是其它根的孩子则将这个朋友圈的其他人的根都转成这个跟
}
遍历保留每个节点根的数组记录总数在最后一个数组中
遍历取最大值

2. Code screenshot

3. PTA Submission List Instructions

2.1 Topic 3: Expression Trees

1 Design ideas (pseudo code or flowchart)

//观察表达式树会发现数字字符的左孩子右孩子都是空的用于后面的表达式树的运算
//创建两个栈一个是树节点的保存类型一个是字符保存栈
for(int i=0;str[i];i++){
if(字符是数字)创建树节点并且入栈
else
{
if(字符栈栈顶优先级小于str[i]){
则进栈字符栈
}
else if(字符栈栈顶优先级大于str[i]){
出栈并且从节点栈中拿出两个;
构树并且放回节点栈中
}
else{
直接出栈
}
}
计算表达式
{
if(BT->rchild==NULL&&BT->lchild==NULL)
return BT->data-'0'
else{
a=计算遍历右树
b=计算遍历左树
switch()
{
case '+':return a+b;
case  '-':return a-b
case '*':returna*b
case '/':return a/b
}
}

2. Code screenshot

3. PTA Submission List Instructions

3.3 My total score: 230

4. Read the code (required, 1 point)

5-27 Family tree processing (30 points)

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
/* 评测结果 时间  结果  得分  题目  编译器     用时(ms)  内存(MB)  用户
2016-08-30 10:31    全部正确    25  5-27    gcc     1   1   569985011
测试点结果 测试点   结果  得分/满分   用时(ms)  内存(MB)
测试点1    答案正确    18/18   1   1
测试点2    答案正确    2/2     1   1
测试点3    答案正确    5/5     1   1
测试点4    答案正确    5/5     1   1
查看代码*/
typedef struct node *Node;
struct node {
    char Name[11];
    int space;
    int  Parant;
};

Node Tree;
int n;

int Scan(char*);
int Trace(int);
int judgeParent(int,int);//父子
int judgeSibling(int,int);//兄弟
int judgeAncestor(int,int);//祖先
void work();
int Index(char*);

int main() {
    int m;
    scanf("%d%d",&n,&m);
    Tree=(Node)malloc(sizeof(struct node)*n);
    getchar();//清除缓存
    for(int i=0; i<n; i++) {
        Tree[i].space=Scan(Tree[i].Name);
        Tree[i].Parant=i;
    }
    Tree[0].Parant=-1;

    for(int i=0; i<m; i++) {
        work();
        getchar();
    }

    return 0;
}
int judgeParent(int x,int y) {
    if(Tree[x].Parant==x)Tree[x].Parant=Trace(x);
    return Tree[x].Parant==y;
}
int judgeSibling(int x,int y) {
    if(Tree[x].Parant==x)Tree[x].Parant=Trace(x);
    if(Tree[y].Parant==y)Tree[y].Parant=Trace(y);
    return Tree[x].Parant==Tree[y].Parant;
}
int judgeAncestor(int x,int y) {
    while(x!=-1) {
        if(judgeParent(x,y))return 1;
        else x=Tree[x].Parant;
    }
    return 0;
}

void work() {
    char StrX[11],StrY[11],relation[11];
    scanf("%s%*s%*s%s%*s%s",StrX,relation,StrY);
//  printf("%s - %s - %s\n",StrX,relation,StrY);

    int X=Index(StrX);
    int Y=Index(StrY);
//  printf("%d   -    %d",X,Y);
    int result;
    switch(relation[0]) {
        case 'c':
            result=judgeParent(X,Y);
            break;
        case 'p':
            result=judgeParent(Y,X);
            break;
        case 's':
            result=judgeSibling(X,Y);
            break;
        case 'd':
            result=judgeAncestor(X,Y);
            break;
        case 'a':
            result=judgeAncestor(Y,X);
            break;
        default:
            result=-1;
            break;
    }



    if(result==1)printf("True\n");
    else if(!result)printf("False\n");
//  else printf("ERROR:系统不能识别所指定关系!\n");
}


int Index(char*a) {
    for(int i=0; i<n; i++) {
//      printf("*");
        if(strcmp(Tree[i].Name,a)==0)return i;
    }
//  printf("ERROR:所给人名不存在!\n");
    return -1;
}

int Trace(int child) { //往前遍历第一个比他缩进少的就是他的父亲

    for(int i=child-1; i>=0; i--) {
        if(Tree[i].space<Tree[child].space) {
//      printf("%d's parent is %d'",child,i);
            return i;
        }
    }
    return -1;//如果没有,那么他就是亚当夏娃了。
}

int Scan(char*p) {
    char c;
    int space=0;

    while((c=getchar())==' ')space++;//记录字符串前面的空格数量

    do {
        *p++=c;
    } while((c=getchar())!='\n');
    *p='\0';

    return space;
}

My initial idea for this question was to build a tree family tree relationship first, and it was indeed successful, but I would not be able to judge the processing of the following relationships.

This input method can ignore the useless information
and then just start from the array To find the location of these two names, and then convert them to the processing of various small problems.
This processing method is really easy and very clever. Also, it retains the information of each person in the family tree, which is processed with an array.

5. Screenshot of code Git submission record

Guess you like

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