Fifth(树,二叉树!)

Problem Description

已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并按中序和后序的方式遍历该二叉树。

Input

连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。

Output

每组输入数据对应输出2行:
第1行输出中序遍历序列;
第2行输出后序遍历序列。

 

Example Input
abc,,de,g,,f,,,
Example Output
cbegdfacgefdba
  
  
01#include<bits/stdc++.h>
02using namespace std;
03struct node
04{
05char data;
06struct node *l,*r;
07};
08char st[51];
09int cnt;
10struct node *creat()
11{
12struct node*root;
13if(st[++cnt]==',')
14root=NULL;
15else
16{
17root=new node;
18root->data=st[cnt];
19root->l=creat();
20root->r=creat();
21}
22return root;
23}
24void zhongxu(struct node*root)
25{
26if(root)
27{
28zhongxu(root->l);
29cout<<root->data;
30zhongxu(root->r);
31}
32}
33void houxu(struct node*root)
34{
35if(root)
36{
37houxu(root->l);
38houxu(root->r);
39cout<<root->data;
40}
41}
42int main()
43{
44while(cin>>st)
45{
46cnt=-1;
47struct node *root;
48root=creat();
49zhongxu(root);
50cout<<endl;
51houxu(root);
52cout<<endl;
53}
54return 0;
55}
56 
注意if else的联动性。
Problem Description
输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。
Input
第一行输入二叉树的先序遍历序列; 第二行输入二叉树的中序遍历序列。
Output
输出该二叉树的后序遍历序列。
Example Input
ABDCEF
BDAECF
Example Output
DBEFCA

  
  
01#include<bits/stdc++.h>
02 
03struct node
04{
05    char data;
06    struct node *lch,*rch;
07};
08void pai(char *xian,char *zhong,int len)
09{
10 
11    if(len==0)
12        return ;
13       node *t=new node;
14       t->data=*xian;
15       int i=0;
16       for(;i<len;i++)
17       {
18           if(zhong[i]==*xian)
19            break;
20       }
21       pai(xian+1,zhong,i);
22      pai(xian+i+1,zhong+i+1,len-i-1);
23      printf("%c",t->data);
24      return ;
25}
26int main()
27{
28    char zhong[100],xian[100];
29    int i,len;
30         gets(xian);
31         gets(zhong);
32        len=strlen(zhong);
33       pai(xian,zhong,len);
34       printf("\n");
35    return 0;
36}
37 

数据结构实验之二叉树五:层序遍历

Time Limit: 1000MS  Memory Limit: 65536KB
Problem Description

已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立二叉树并求二叉树的层次遍历序列。

Input
 输入数据有多行,第一行是一个整数 t (t<1000) ,代表有 t 行测试数据。每行是一个长度小于 50 个字符的字符串。
Output
 输出二叉树的层次遍历序列。
Example Input
2
abd,,eg,,,cf,,,
xnl,,i,,u,,
Example Output
abcdefg
xnuli
01#include <bits/stdc++.h>
02 
03using namespace std;
04 
05struct node
06{
07char data;
08struct node *l,*r;
09};
10char st[51];
11 
12int  cnt;
13 
14struct node*creat()
15{
16char c=st[cnt++];
17struct node*root;
18if(c==',')
19root=NULL;
20else
21{
22root=new node;
23root->data=c;
24root->l=creat();
25root->r=creat();
26}
27return root;
28}
29void ceng(struct node *root)
30{
31int out=0;
32int in=0;
33struct node*q[10010];
34q[in++]=root;
35while(out<in)
36{
37if(q[out])
38{
39cout<<q[out]->data;
40q[in++]=q[out]->l;
41q[in++]=q[out]->r;
42}
43out++;
44}
45}
46int main()
47{
48int t;
49cin>>t;
50while(t--)
51{
52cnt=0;
53cin>>st;
54struct node*root;
55root=creat();
56ceng (root);
57cout<<endl;
58}
59    return 0;
60}
 
 
  
  
01#include <iostream>
02#include<cstring>
03using namespace std;
04struct code
05{
06char s;
07struct code *l,*r;
08};
09char st[100];
10int top=-1;
11struct code *creat()
12{
13struct code *p;
14if(st[++top]==',')
15p=NULL;
16else
17{
18p=new code;
19p->s=st[top];
20p->l=creat();
21p->r=creat();
22}
23return p;
24}
25void cx(code *t)
26{
27int in=0,out=0;
28code*temp[100];
29temp[in++]=t;
30while(in>out)
31{
32if(temp[out])
33{
34cout<<temp[out]->s;//只要不是空节点就往外输出,而且一定是一层一层的往外出
35 
36temp[in++]=temp[out]->l;//上一层出去一个我就把他的左右字数往里进来,
37temp[in++]=temp[out]->r;//通过whil循环把所有层遍历一遍,
38//由于队列思想进只会往后面放出从前面出,所以 代码符合层次遍历的从上到下从左到右的要求
39}
40out++;
41}
42}
43int main()
44{
45int n;
46cin>>n;
47while(n--)
48{
49cin>>st;
50top=-1;
51struct code *p;
52p=creat();
53cx(p);
54cout<<endl;
55}
56    return 0;
57}
58 

层序遍历代码。
  
  
  1. #include <iostream>  
  2. #include <stdlib.h>  
  3. using namespace std;  
  4.   
  5. struct node  
  6. {  
  7.     char data;  
  8.     struct node * lchild;  
  9.     struct node * rchild;  
  10. };  
  11.   
  12. char s[100];  
  13. int cnt;  
  14. struct node * creat()                                       //先序建树  
  15. {  
  16.     struct node * root;  
  17.     if(s[++cnt] == ',')  
  18.         root = NULL;  
  19.     else  
  20.     {  
  21.         root = (struct node * )malloc(sizeof(struct node));  
  22.         root->data = s[cnt];  
  23.         root->lchild = creat();  
  24.         root->rchild = creat();  
  25.     }  
  26.     return root;  
  27. };  
  28.   
  29. void cengxu(struct node * root)  
  30. {  
  31.     struct node * temp[100];  //关键中间变量存放每一层的数据  
  32.     int in = 0, out = 0;  
  33.     temp[in++] = root;  //每次把这一层存入,然后输出的时候就把他的左右节点存入  
  34.     while(in > out) //例如一颗完全二叉树abcdefg  输出a的时候把bc放入,输出b的时候把//的孩子放入也就是de,再输出c并且放入孩子fg,依次这样,达到层序的要求
  35.     {
  36.         if(temp[out])  
  37.         {  
  38.             cout << temp[out]->data;  
  39.             temp[in++] = temp[out]->lchild;  
  40.             temp[in++] = temp[out]->rchild;  
  41.         }  
  42.         out++;  
  43.     }  
  44. }  
  45. int main()  
  46. {  
  47.     struct node * root;  
  48.     int t;  
  49.     cin >>  t;  
  50.     while(t--)  
  51.     {  
  52.         cin >> s;  
  53.         cnt = -1;  
  54.         root = creat();  
  55.         cengxu(root);  
  56.         cout << endl;  
  57.     }  
  58.     return 0;  
  59. }  

数据结构实验之二叉树三:统计叶子数

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并求二叉树的叶子结点个数。

Input

连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。

Output

输出二叉树的叶子结点个数。

Example Input
abc,,de,g,,f,,,
Example Output
3

  
  
#include<bits/stdc++.h>
02using namespace std;
03struct node
04{
05char data;
06struct node*l,*r;
07}*root;
08char st[55];
09int cnt,cont;
10struct node*creat()
11{
12struct node *root;
13if(st[cnt++]==',')
14root=NULL;
15else
16{
17root=new node;
18root->data=st[cnt-1];
19root->l=creat();
20root->r=creat();
21}
22return root;
23}
24int jiedian(struct node *root)
25{
26if(root)
27{
28if((root->l==NULL)&&(root->r==NULL))
29cont++;
30jiedian(root->l);
31jiedian(root->r);
32}
33return 0;
34}
35int main()
36{
37while(cin>>st)
38{
39cnt=0;
40cont=0;
41root=creat();
42jiedian(root);
43cout<<cont<<endl;
44}
45return 0;
46}

数据结构实验之二叉树七:叶子问题

Time Limit: 1000MS  Memory Limit: 65536KB
Problem Description

已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立该二叉树并按从上到下从左到右的顺序输出该二叉树的所有叶子结点。

Input
 输入数据有多行,每一行是一个长度小于 50 个字符的字符串。
Output
 按从上到下从左到右的顺序输出二叉树的叶子结点。
Example Input
abd,,eg,,,cf,,,
xnl,,i,,u,,
Example Output
dfg
uli
层序遍历再加个是否是叶子判断输出就可以

  
  
01#include<bits/stdc++.h>
02using namespace std;
03struct node
04{
05char data;
06struct node*l,*r;
07};
08char s[100];
09int cnt;
10struct node*creat()
11{
12struct node *root;
13if(s[++cnt]==',')
14root=NULL;
15else
16{
17root=new node;
18root->data=s[cnt];
19root->l=creat();
20root->r=creat();
21}
22return root;
23};
24void cengxu(struct node*root)
25{
26struct node*temp[100];
27int in=0,out=0;
28temp[in++]=root;
29while(in>out)
30{
31if(temp[out])
32{
33if(temp[out]->l==NULL&&temp[out]->r==NULL)
34cout<<temp[out]->data;
35temp[in++]=temp[out]->l;
36temp[in++]=temp[out]->r;
37}
38out++;
39}
40}
41int main()
42{
43struct node*root;
44while(cin>>s)
45{
46cnt=-1;
47root=creat();
48cengxu(root);
49cout<<endl;
50}
51return 0;
52}

数据结构实验之求二叉树后序遍历和层次遍历

Time Limit: 1000MS  Memory Limit: 65536KB
Problem Description

 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历和层序遍历。

Input

 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列。

Output

每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列。

Example Input
2
abdegcf
dbgeafc
xnliu
lnixu
Example Output
dgebfca
abcdefg
linux
xnuli

  
  
01#include<bits/stdc++.h>
02using namespace std;
03struct node
04{
05char data;
06struct node *l,*r;
07};
08struct node*creat(int n,char *str1,char *str2)
09{
10struct node*root;
11char *p;
12if(n==0)
13return NULL;
14root=new node;
15root->data=str1[0];
16for(p=str2;p!='\0';p++)
17if(*p==str1[0])
18break;
19int t=p-str2;
20root->l=creat(t,str1+1,str2);
21root->r=creat(n-1-t,str1+1+t,p+1);
22return root;
23}
24void houxu(struct node*root)
25{
26if(root)
27{
28houxu(root->l);
29houxu(root->r);
30cout<<root->data;
31}
32}
33void cengxu(struct node*root)
34{
35struct node*temp[100];
36int in=0,out=0;
37temp[in++]=root;
38while(in>out)
39{
40if(temp[out])
41{
42cout<<temp[out]->data;
43temp[in++]=temp[out]->l;
44temp[in++]=temp[out]->r;
45}
46out++;
47}
48}
49int main()
50{
51int n;
52char str1[60],str2[60];
53struct node *root=new node;
54cin>>n;
55while(n--)
56{
57cin>>str1>>str2;
58int n=strlen(str1);
59root=creat(n,str1,str2);
60houxu(root);
61cout<<endl;
62cengxu(root);
63cout<<endl;
64}
65    return 0;
66}
 
 

数据结构实验之二叉树四:(先序中序)还原二叉树

Time Limit: 1000MS  Memory Limit: 65536KB
Problem Description

给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。

Input

输入数据有多组,每组数据第一行输入1个正整数N(1 <= N <= 50)为树中结点总数,随后2行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区分大小写)的字符串。

 

Output

 输出一个整数,即该二叉树的高度。

Example Input
9 
ABDFGHIEC
FDHGIBEAC
Example Output
5

  
  
01#include <bits/stdc++.h>
02using namespace std;
03struct node
04{
05char data;
06struct node *l,*r;
07};
08struct node *creat(int n,char a[],char b[])
09{
10struct node*root;
11char *p;
12if(n==0)
13return NULL;
14root=new node;
15root->data=a[0];
16for(p=b;p!='\0';p++)
17if(*p==a[0])
18break;
19int t;
20t=p-b;
21root->l=creat(t,a+1,b);
22root->r=creat(n-1-t,a+t+1,p+1);
23return root;
24}
25int deep(struct node *root)
26{
27int d=0;
28if(root)
29{
30int l1=deep(root->l);
31int l2=deep(root->r);
32d=max(l1,l2)+1;
33}
34return d;
35}
36int main()
37{
38int n,m;
39char  a[102],b[103];
40struct node*root;
41while(cin>>n)
42{
43cin>>a>>b;
44root=creat(n,a,b);
45m=deep(root);
46cout<<m<<endl;
47}
48    return 0;
49}

数据结构实验之二叉树的建立与遍历

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
       已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(其中逗号表示空节点)。请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度。

Input
  输入一个长度小于50个字符的字符串。
Output
输出共有4行: 第1行输出中序遍历序列; 第2行输出后序遍历序列; 第3行输出叶子节点个数; 第4行输出二叉树深度。
Example Input
abc,,de,g,,f,,,
Example Output
cbegdfacgefdba35

  
  
01#include <bits/stdc++.h>
02using namespace std;
03struct node
04{
05    char data;
06    struct node *l,*r;
07};
08char st[105];
09int cnt=-1,cont=0;
10struct node *creat()
11{
12    struct node*root;
13    if(st[++cnt]==',')
14        root=NULL;
15    else
16    {
17        root=new node;
18        root->data=st[cnt];
19        root->l=creat();
20        root->r=creat();
21    }
22    return root;
23}
24int jiedian(struct node*root)
25{
26    if(root)
27    {
28        if((root->l==NULL)&&(root->r==NULL))
29            cont++;
30        jiedian(root->l);
31        jiedian(root->r);
32    }
33    return 0;
34}
35void zhongxu(struct node *root)
36{
37    if(root)
38    {
39        zhongxu(root->l);
40        cout<<root->data;
41        zhongxu(root->r);
42    }
43}
44void houxu(struct node *root)
45{
46    if(root)
47    {
48        houxu(root->l);
49        houxu(root->r);
50        cout<<root->data;
51    }
52}
53int deep(struct node *root)
54{
55    int d=0;
56    if(root)
57    {
58        int l1=deep(root->l);
59        int l2=deep(root->r);
60        d=max(l1,l2)+1;
61    }
62    return d;
63}
64int main()
65{
66    struct node*root=new node;
67    int m;
68    cin>>st;
69    root=creat();
70    zhongxu(root);
71    cout<<endl;
72    houxu(root);
73    cout<<endl;
74    jiedian(root);
75    m=deep(root);
76    cout<<cont<<endl<<m<<endl;
77    return 0;
78}
79 
#include<bits/stdc++.h>
using namespace std;
char a[100],b[100];
struct  node
{
    char data;
    struct node *lchild,*rchild;
};
struct node *creat(int n,char *a,char *b)
{
    struct node *root;
 if(n==0)
    return NULL;
 else
    root=new node;
    int i;
 root->data=a[0];
 for(i=0;i<n;i++)
 {
     if(b[i]==a[0])break;
 }
    root->lchild=creat(i,a+1,b);
    //第二次长度变为前面的i
    root->rchild=creat(n-i-1,a+i+1,b+i+1);
    return root;
};

void f(struct node *root)
{
    queue<struct node*>q;
    q.push(root);
    struct node *tmp=new node;
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(tmp)
        {
            cout<<tmp->data;
            q.push(tmp->lchild);
            q.push(tmp->rchild);
        }
    }

}
int main()
{
    int t;
    while(cin>>t)
    {
        struct node *root;
        while(t--)
        {
            root=new node;
            cin>>a;
            cin>>b;
            int n=strlen(a);
            root=creat(n,a,b);
            f(root);
            cout<<endl;
            //换行丢了
        }
    }
    return 0;
}
//前中建立二叉树,利用stl的queue
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值