#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
typedef struct node
{
int data;
struct node *lchild,*rchild;
}tree;
int len,len1;
tree *creat(tree *root,int x)//建二叉排序树树
{
if(root==NULL)
{
root=new tree();//等同于root=(struct node *)malloc(sizeof(struct node))
root->data=x;
root->lchild=root->rchild=NULL;
}
else
{
if(x<root->data)
root->lchild=creat(root->lchild,x);
else root->rchild=creat(root->rchild,x);
}
return root;
}
int judge(tree *root,tree *root1 )
{
if(root==NULL&&root1==NULL)//判断树的的根节点是否存在。
return 1;
else if(root!=NULL&&root1!=NULL)
{
if(root->data!=root1->data)
return 0;
else if(judge(root->lchild,root1->lchild)&&judge(root->rchild,root1->rchild))//递归调用这个函数就可以判断各个位置上的元素数否相等
return 1;
else
return 0;
}
else return 0;
}
int main()
{
int n,m,i;
int frist[20],second[20];
while(~scanf("%d%d",&n,&m)&&n!=0)
{
for(i=1;i<=n;i++)
cin>>frist[i];
tree *root =new tree();//二叉排序树建树先建一个头结点
root->data=frist[1];
root->lchild=root->rchild =NULL;
for(i=2;i<=n;i++)
{
root=creat(root,frist[i]);
}
while(m--)
{
for(i=1;i<=n;i++)
cin>>second[i];
tree *root1 =new tree();
root1->data=second[1];
root1->lchild=root1->rchild =NULL;
for(i=2;i<=n;i++)
{
root1=creat(root1,second[i]);
}
int key = judge(root,root1);
if(key==1)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
return 0;
}
3373 数据结构实验之查找一:二叉排序树
最新推荐文章于 2018-12-18 20:39:01 发布