Data_Structure tree

---Restore content begins---

summarize

1 Tree structure mind map

2 Tree structure learning experience

As a nonlinear structure, the tree is a little more complicated than the previous linear structure, and the most important thing is the recursive call. When doing the PTA of the tree,

The problems I have encountered are: I am not very skilled in recursive tree building, and I have to use templates; I still can't use trees to evaluate expression values; In-order and post-order traversal is still a bit vague; I don't understand the clues of trees, let alone code. .

The problems I solved are: determine the unique binary tree and post-order traversal based on pre-order and in-order traversal; build a Huffman tree given a bunch of numbers; find the weighted length of leaf nodes;

PTA lab work

1 Topic 1: 6-1 jmu-ds-binary tree operation set

Design ideas (pseudocode or flowchart)

PTA Submission List Instructions

Reading the code in the book, I didn't encounter too many problems.

 Topic 2: 6-4 jmu-ds-expression tree (required)

I really don't know about this. . . .

Topic 3: 7-7 Repairing the ranch

Design ideas (pseudocode or flowchart)

Code screenshot

 PTA Submission Listing Instructions.

This question has tree-building and recursive traversal code in the book, so I only need to write the code that selects the minimum value, and then try it on DEVC for a while and submit it directly.

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

The total score of this question set: 285
points

1 Screenshot of PTA ranking

My total score: 2

 Read the code (must do)

Title: PTA 7-7 Genealogy Processing

code show as below:

[#include<bits/stdc++.h>
using namespace std;
struct Node{
string name;
int parent;
int num;
}t[105];
int n;
int find(char *a)
{
// printf("a = %s\n",a);
for(int i=0;i<=n;i++)
{
// cout<<"t[i] = "<<t[i].name<<endl;
if(t[i].name==a)
{
// printf("sssssss\n");
// printf("i = %d\n",i);
return i;
}
}
return -1;
}
int Trace(int child)
{
for(int i=child-1;i>=0;i--)
{
if(t[i].num<t[child].num)
return i;
}
return -1;
}
int CheckParent(int c,int p)
{
// printf("t = %d\n",Trace(c));
if(Trace(c)==p)return 1;
else return 0;
}
int CheckAncestor(int c,int an)
{
while(c!=-1)
{
if(CheckParent(c,an))return 1;
else c=Trace(c);
}
return 0;
}
int CheckSibling(int c1,int c2)
{
if(Trace(c1)==Trace(c2))return 1;
else return 0;
}
void pipei()
{
int x,y,res=0;
char a[15],b[15],c[15],d[15];
scanf("%s %s %s %s %s %s",a,d,d,c,d,b);
// printf("%s %s %s\n",a,b,c);
x=find(a);
// printf("x = %d\n",x);
y=find(b);
// printf("y = %d\n",y);
switch(c[0])
{
case 'c':res=CheckParent(x,y);break;
case 'p':res=CheckParent(y,x);break;
case 's':res=CheckSibling(x,y);break;
case 'd':res=CheckAncestor(x,y);break;
case 'a':res=CheckAncestor(y,x);break;
}
if(res)printf("True\n");
else printf("False\n");
}
int main()
{
int m,i,j,len;
string s;
char a[15],b[15],c[15];
scanf("%d%d",&n,&m);
getchar();
t[0].parent=-1;
for(i=1;i<=n;i++)
{
getline(cin,s);
// cout<<s<<endl;
len=s.size();
t[i].num=0;
t[i].parent=i;
j=0;
while(s[j]==' ')
{
t[i].num++;
j++;
}
t[i].name=s.substr(t[i].num,len);
}
// for(i=1;i<=n;i++)
// cout<<"name"<<t[i].name<<endl;

// char x[15];
// scanf("%s",x);
for(i=1;i<=m;i++)
{
pipei();
if(i<m)
getchar();
}
}]

/*Note: 1. Only those with the same parent can be called siblings;
2. The first name with less space than him is his parent;
3. Ancestor must be the parent's parent...parent ,
not just with fewer spaces than him. */

Link address: [https://gitee.com/QueGuangRenChen/data_structure/blob/master/chpt04/%E5%AE%B6%E8%B0%B1%E5%A4%84%E7%90%86.cpp]( https://gitee.com/QueGuangRenChen/data_structure/blob/master/chpt04/%E5%AE%B6%E8%B0%B1%E5%A4%84%E7%90%86.cpp)

Screenshot of Git commit record

 

---End of recovery content---

Guess you like

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