3373 数据结构实验之查找一:二叉排序树

数据结构实验之查找一:二叉排序树

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值