君所欲言,均在注释中......嘿嘿~
A - 数据结构实验之二叉树二:遍历二叉树
Description
已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并按中序和后序的方式遍历该二叉树。
Input
连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。
Output
每组输入数据对应输出2行:
第1行输出中序遍历序列;
第2行输出后序遍历序列。
Sample
Input
abc,,de,g,,f,,,
Output
cbegdfa
cgefdba
Hint
#include <iostream>
using namespace std;
struct node
{
char data;
node *l,*r;
};
int t;
char s[99];
struct node * buildtree()
{
node *root;
if(s[t]==',')
{
root=NULL;
t++;
}
else
{
root=new node;
root->data=s[t++];
root->l=buildtree();
root->r=buildtree();
}
return root;
}
void midorder(node *root)//中序遍历 “左 根 右”
{
if(root)
{
midorder(root->l);
cout<<root->data;//根植直接输出
midorder(root->r);
}
}
void postorder(node *root)//后序遍历 “左 右 根”
{
if(root)
{
postorder(root->l);
postorder(root->r);
cout<<root->data;
}
}
int main()
{
struct node *root;
while(cin>>s)
{
t=0;//勿忘!
root=buildtree();
midorder(root);
cout<<endl;
postorder(root);
cout<<endl;
}
return 0;
}
B - 数据结构上机测试4.1:二叉树的遍历与应用1
Description
输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。
Input
第一行输入二叉树的先序遍历序列;
第二行输入二叉树的中序遍历序列。
Output
输出该二叉树的后序遍历序列。
Sample
Input
ABDCEF BDAECF
Output
DBEFCA
#include <bits/stdc++.h>
#include <string>
using namespace std;
char s[55];
int t=0;
struct node
{
char data;
node *l,*r;
};
char pre[55],mid[55];
node * buildtree(int len,char *pre,char *mid)
{
node *p;
if(!len)
return NULL;
p=new node;
p->data=pre[0];
int i;
for(i=0; i<len; i++)
{
if(mid[i]==pre[0])
{
break;
}
}
p->l=buildtree(i,pre+1,mid);//左子树的长度 左子树在先序遍历、中序遍历中开始的位置
p->r=buildtree(len-i-1,pre+i+1,mid+i+1);//右子树建立同上 注:pre、mid代表着最初位置
return p;
}
void postorder(struct node *root)
{
if(root)
{
postorder(root->l);
postorder(root->r);
cout<<root->data;
}
}
int main()
{
cin>>pre>>mid;
struct node *root;
int len=strlen(pre);
root=buildtree(len,pre,mid);
postorder(root);
cout<<endl;
return 0;
}
C - 求二叉树的先序遍历
Description
已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
Input
输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的中序遍历序列,第二个字符串表示二叉树的后序遍历序列。
Output
输出二叉树的先序遍历序列
Sample
Input
2 dbgeafc dgebfca lnixu linux
Output
abdegcf xnliu
Hint
#include <bits/stdc++.h>
#include <string>
using namespace std;
char s[55];
int t=0;
struct node
{
char data;
node *l,*r;
};
char post[55],mid[55];
node * buildtree(int len,char *mid,char *post)
{
node *p;
if(!len)
return NULL;
p=new node;
p->data=post[len-1];
int i;
for(i=0; i<len; i++)
{
if(mid[i]==post[len-1])
{
break;
}
}
p->l=buildtree(i,mid,post);//左子树的长度 左子树在中序遍历、后序遍历中开始建立节点的位置
p->r=buildtree(len-i-1,mid+i+1,post+i);//右子树建立同上 注:post、mid代表着最初位置
return p;
}
void preorder(struct node *root)
{
if(root)
{
cout<<root->data;
preorder(root->l);
preorder(root->r);
}
}
int main()
{
int n;
cin>>n;
while(n--)
{
cin>>mid>>post;
struct node *root;
int len=strlen(mid);
root=buildtree(len,mid,post);
preorder(root);
cout<<endl;
}
return 0;
}
D - 数据结构实验之二叉树五:层序遍历
Description
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立二叉树并求二叉树的层次遍历序列。
Input
Output
Sample
Input
2
abd,,eg,,,cf,,,
xnl,,i,,u,,
Output
abcdefg
xnuli
Hint
#include <iostream>
using namespace std;
char s[55];
int t=0;
struct node
{
char data;
node *l,*r;
};
node *buildtree()
{
node *root;
if(s[t]==',')
root=NULL,t++;
else
{
root=new node;
root->data=s[t++];
root->l=buildtree();
root->r=buildtree();
}
return root;
}
void cengxu(node *root)
{
int in=0,out=0;
node *q[55];//用数组来模拟队列
q[in++]=root;
while(in>out)
{
if(q[out])
{
cout<<q[out]->data;
q[in++]=q[out]->l;
q[in++]=q[out]->r;
}
out++;
}
}
int main()
{
int n;
cin>>n;
while(n--)
{
cin>>s;
t=0;
node*root;
root=buildtree();
cengxu(root);
cout<<endl;
}
return 0;
}
E - 求二叉树的层次遍历
Description
已知一颗二叉树的前序遍历和中序遍历,求二叉树的层次遍历。
Input
输入数据有多组,输入T,代表有T组测试数据。每组数据有两个长度小于50的字符串,第一个字符串为前序遍历,第二个为中序遍历。
Output
每组输出这颗二叉树的层次遍历。
Sample
Input
2 abc bac abdec dbeac
Output
abc abcde
#include <bits/stdc++.h>
#include <string>
using namespace std;
char s[55];
int t=0;
struct node
{
char data;
node *l,*r;
};
char pre[55],mid[55];
node * buildtree(int len,char *pre,char *mid)
{
node *p;
if(!len)
return NULL;
p=new node;
p->data=pre[0];
int i;
for(i=0; i<len; i++)
{
if(mid[i]==pre[0])
{
break;
}
}
p->l=buildtree(i,pre+1,mid);//左子树的长度 左子树在先序遍历、中序遍历中开始的位置
p->r=buildtree(len-i-1,pre+i+1,mid+i+1);//右子树建立同上 注:pre、mid代表着最初位置
return p;
}
void cengxu(node *root)
{
int in=0,out=0;
node * q[55];
q[in++]=root;
while(in>out)
{
if(q[out])
{
cout<<q[out]->data;
q[in++]=q[out]->l;
q[in++]=q[out]->r;
}
out++;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
char s1[55],s2[55];
cin>>s1>>s2;
int len=strlen(s1);
struct node *root;
root=buildtree(len,s1,s2);
cengxu(root);
cout<<endl;
}
return 0;
}
F - 数据结构实验之求二叉树后序遍历和层次遍历
Description
已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历和层序遍历。
Input
输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列。
Output
每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列。
Sample
Input
2 abdegcf dbgeafc xnliu lnixu
Output
dgebfca abcdefg linux xnuli
Hint
#include <iostream>
#include <string.h>
#define N 1010
using namespace std;
struct node
{
char data;
struct node *l,*r;
};
char pre[55],mid[55];
node *buildtree(int len,char *pre,char *mid)
{
node *p;
if(!len)
return NULL;
p=new node;
p->data=pre[0];
int i;
for(i=0; i<len; i++)
{
if(mid[i]==pre[0])
break;
}
p->l=buildtree(i,pre+1,mid);
p->r=buildtree(len-i-1,pre+i+1,mid+i+1);
return p;
}
void cengxu(struct node*root)
{
int in=0,out=0;
node *q[55];
q[in++]=root;
while(in>out)
{
if(q[out])
{
cout<<q[out]->data;
q[in++]=q[out]->l;
q[in++]=q[out]->r;
}
out++;
}
}
void postorder(struct node *root)
{
if(root)
{
postorder(root->l);
postorder(root->r);
cout<<root->data;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>pre>>mid;
struct node *root;
int len=strlen(pre);
root=buildtree(len,pre,mid);
postorder(root);
cout<<endl;
cengxu(root);
cout<<endl;
}
return 0;
}
G - 数据结构实验之二叉树三:统计叶子数
Description
已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并求二叉树的叶子结点个数。
Input
连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。
Output
输出二叉树的叶子结点个数。
Sample
Input
abc,,de,g,,f,,,
Output
3
Hint
#include <iostream>
#include <string.h>
#define N 1010
using namespace std;
struct node
{
char data;
struct node *l,*r;
};
char s[55];
int t=0;
node *buildtree()
{
node *root;
if(s[t]==',')
{
root=NULL;
t++;
}
else
{
root=new node;
root->data=s[t++];
root->l=buildtree();
root->r=buildtree();
}
return root;
}
int leaf(node *root)
{
if(root==NULL)//只有根节点
return 0;
else if(root->l==NULL&&root->r==NULL)//是叶子满足的条件
return 1;
else return leaf(root->l)+leaf(root->r);//总叶子数=右子树叶子+左子树叶子
}
int main()
{
while(cin>>s)
{
t=0;//循环的每次都要赋值为0,勿忘~不然会"RTE"
struct node *root;
root=buildtree();
cout<<leaf(root)<<endl;
}
return 0;
}
H - 数据结构实验之二叉树四:(先序中序)还原二叉树
Description
给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。
Input
输入数据有多组,每组数据第一行输入1个正整数N(1 <= N <= 50)为树中结点总数,随后2行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区分大小写)的字符串。
Output
输出一个整数,即该二叉树的高度。
Sample
Input
9 ABDFGHIEC FDHGIBEAC
Output
5
#include <bits/stdc++.h>
using namespace std;
struct node
{
char data;
struct node *l,*r;
};
char pre[55],mid[55];
node *buildtree(int len,char pre[],char mid[])
{
struct node *root;
if(!len)
{
return NULL;
}
root=new node;
root->data=pre[0];
int i;
for(i=0; i<len; i++)
{
if(mid[i]==pre[0])
{
break;
}
}
root->l=buildtree(i,pre+1,mid);
root->r=buildtree(len-i-1,pre+i+1,mid+i+1);
return root;
}
int high(struct node *root)
{
int m,n;
if(root==NULL)
return 0;
else
{
n=high(root->l);
m=high(root->r);
if(m>n)
return m+1;
else return n+1;
}
}
int main()
{
int n;
node*root;
while(cin>>n)
{
cin>>pre>>mid;
int len=strlen(pre);
root=buildtree(len,pre,mid);
cout<<high(root)<<endl;
}
return 0;
}
I - 数据结构实验之二叉树七:叶子问题
Description
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立该二叉树并按从上到下从左到右的顺序输出该二叉树的所有叶子结点。
Input
Output
Sample
Input
abd,,eg,,,cf,,,
xnl,,i,,u,,
Output
dfg
uli
Hint
#include <bits/stdc++.h>
using namespace std;
struct node
{
char data;
node *l,*r;
};
char s[55];
int t=0;
node *buildtree()
{
node *root;
if(s[t]==',')
{
root=NULL;
t++;
}
else
{
root=new node;
root->data=s[t++];
root->l=buildtree();
root->r=buildtree();
}
return root;
}
void cengxu(node *root)//题目要求从上到下从左到右,所以用到层序遍历
{
int in=0,out=0;
node *q[55];//用数组来模拟队列
q[in++]=root;
while(in>out)
{
if(q[out])
{
if(q[out]->l==NULL&&q[out]->r==NULL)//叶子满足的条件
cout<<q[out]->data;//将叶子节点输出
q[in++]=q[out]->l;
q[in++]=q[out]->r;
}
out++;
}
}
int main()
{
while(cin>>s)
{
t=0;
node *root;
root=buildtree();
cengxu(root);
cout<<endl;
}
return 0;
}
J - 数据结构实验之二叉树八:(中序后序)求二叉树的深度
Description
已知一颗二叉树的中序遍历序列和后序遍历序列,求二叉树的深度。
Input
输入数据有多组,输入T,代表有T组数据。每组数据包括两个长度小于50的字符串,第一个字符串表示二叉树的中序遍历,第二个表示二叉树的后序遍历。
Output
输出二叉树的深度。
Sample
Input
2 dbgeafc dgebfca lnixu linux
Output
4 3
#include <bits/stdc++.h>
using namespace std;
struct node
{
char data;
node *l,*r;
};
char mid[55],post[55];
node * buildtree(int len,char *mid,char *post)
{
node *p;
if(!len)
return NULL;
p=new node;
p->data=post[len-1];
int i;
for(i=0; i<len; i++)
{
if(mid[i]==post[len-1])
{
break;
}
}
p->l=buildtree(i,mid,post);//左子树的长度 左子树在中序遍历、后序遍历中开始建立节点的位置
p->r=buildtree(len-i-1,mid+i+1,post+i);//右子树建立同上 注:post、mid代表着最初位置
return p;
}
int high(struct node *root)
{
int m,n;
if(root==NULL)
return 0;
else
{
n=high(root->l);
m=high(root->r);
if(m>n)
return m+1;
else return n+1;
}
}
int main()
{
int n;
cin>>n;
while(n--)
{
cin>>mid>>post;
int len=strlen(mid);
node *root;
root=buildtree(len,mid,post);
int h;
h=high(root);//记得要用一个变量来承接返回值;
cout<<h<<endl;
}
return 0;
}
K - 数据结构实验之二叉树的建立与遍历
Description
Input
Output
第1行输出中序遍历序列;
第2行输出后序遍历序列;
第3行输出叶子节点个数;
第4行输出二叉树深度。
Sample
Input
abc,,de,g,,f,,,
Output
cbegdfa
cgefdba
3
5
Hint
#include <bits/stdc++.h>
using namespace std;
struct node
{
char data;
node *l,*r;
};
char s[55];
int t=0;
node *buildtree()
{
node *root;
if(s[t]==',')
{
root=NULL;
t++;
}
else
{
root=new node;
root->data=s[t++];
root->l=buildtree();
root->r=buildtree();
}
return root;
}
int yezishu(node *root)
{
int in=0,out=0;
int m=0;
node *q[55];//用数组来模拟队列
q[in++]=root;
while(in>out)
{
if(q[out])
{
if(q[out]->l==NULL&&q[out]->r==NULL)//叶子满足的条件
m++;
q[in++]=q[out]->l;
q[in++]=q[out]->r;
}
out++;
}
return m;
}
void midorder(struct node *root)
{
if(root)
{
midorder(root->l);
cout<<root->data;
midorder(root->r);
}
}
void postorder(struct node *root)
{
if(root)
{
postorder(root->l);
postorder(root->r);
cout<<root->data;
}
}
int high(struct node *root)
{
int m,n;
if(root==NULL)
return 0;
else
{
n=high(root->l);
m=high(root->r);
if(m>n)
return m+1;
else return n+1;
}
}
int main()
{
cin>>s;
struct node *root;
root=buildtree();
midorder(root);
cout<<endl;
postorder(root);
cout<<endl;
int m=yezishu(root);
cout<<m<<endl;
int h=high(root);
cout<<h<<endl;
return 0;
}
* * L - 数据结构实验之二叉树一:树的同构
Description
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。
图1
图2
现给定两棵树,请你判断它们是否是同构的。
Input
注意:题目保证每个结点中存储的字母是不同的。
Output
Sample
Input
8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -
Output
Yes
Hint
#include<iostream>
#include<malloc.h>
#define null -1
using namespace std;
struct TreeNode
{
char data; // 存值
int left; // 左子树的下标
int right; // 右子树的下标
} T1[20],T2[20];
// 返回根结点
int create(int n,struct TreeNode T[])
{
int root = 0;
char left,right;
if(!n)
return null;
for(int i=0; i<n; i++)
{
cin>>T[i].data>>left>>right;
if(left=='-')
T[i].left = null;
else
{
T[i].left = left-'0';
root -= T[i].left;
}
if(right=='-')
T[i].right = null;
else
{
T[i].right = right-'0';
root -= T[i].right;
}
// 0 累加到 n-1
root +=i;
}
return root;
}
// 判断是否同构
bool judge(int R1,int R2)
{
if(R1 == null && R2 == null) // 都为空
return true;
if((R1 == null && R2 != null) ||( R1 != null && R2 == null) ) // 一个为空,一个不为空
return false;
if(T1[R1].data != T2[R2].data) // 值不同
return false;
if((T1[R1].left != null && T2[R2].left != null )&&(T1[T1[R1].left].data == T2[T2[R2].left].data)) // 左儿子不为空且值相等
return judge(T1[R1].left,T2[R2].left) && judge(T1[R1].right,T2[R2].right);
else // 左儿子不为空且值不等 或者 某一个左儿子为空
return judge(T1[R1].right,T2[R2].left) && judge(T1[R1].left,T2[R2].right);
}
int main()
{
int R1,R2;
int n,m;//两棵树的节点个数可能不同
while(cin>>n)//题目要求多组输入
{
R1 = create(n,T1);//第一棵树的建立
cin>>m;
R2 = create(m,T2);//第二棵树的建立
if(judge(R1,R2))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
树的同构,简言之就是,“左右节点扭了扭位置”,最后“该连着谁还是谁”。