判断是否为同一棵搜索树

#include<iostream>
#include<stdlib.h>
using namespace std;   //读入 N 和L  N代表又多少个元素  L代表要比较多少个序列

 typedef struct student
    {
        int data;
        student *left;
        student *right;
        int flag;   //如果访问过了设置为0  没有访问过则设置为1
    }*Btree,tree;

    void init_Btree(Btree &L);//对树的初始化
    Btree insert_Node(Btree &L,int data); //将data 插入到 L中
    Btree creat_tree(int N); //表示创建  4个节点的搜索树
    int  check_tree(Btree L,int e);//在T里面去搜索e  在查找的时候我们来看是否有节点以前没碰到过
    int judge_tree(Btree L,int N); //判断这N个数是否和已知的树一致
    void delete_tree(Btree &L) ;  //释放一棵树
    void Resert_flag(Btree &L);//将这棵树的所有flag重新赋值为0
    int main()
        {
            int N;
            cout<<"您想创建几个节点"<<endl;
            cin>>N;
            Btree NEW_tree;
            int M;
            while(N)
            {
                init_Btree(NEW_tree);
                NEW_tree=creat_tree(N);
                cout<<"你想比较几个序列"<<endl;
                cin>>M;
                for(int i=0;i<M;i++)
                {
                    if(judge_tree(NEW_tree,N)==1)
                    {
                        cout<<"YES"<<endl;

                    }
                    else
                    {
                        cout<<"NO"<<endl;
                    }

                    Resert_flag(NEW_tree);
                }

               delete_tree(NEW_tree);
               NEW_tree=NULL;
               system("pause");
               system("cls");
               cout<<"您想创建几个节点"<<endl;
               cin>>N;

            }

            return 0;
        }

    void init_Btree(Btree &L)   //对树的初始化
        {
            L=new tree;
            L=NULL;

        }

    Btree insert_Node(Btree &L,int data)  //将data 插入到 L中
        {
            if(L==NULL)  //如果这棵树是空树
                {
                    L=new tree;
                    L->left=NULL;
                    L->right=NULL;
                    L->data=data;
                    L->flag=0;
                    return L;
                }
            else if(data>L->data)
            {
               L->right=insert_Node(L->right,data);   //注意 这里已经传了引用  加不加  L->right都一样
            }

            else if(data<L->data)
            {
               L->left=insert_Node(L->left,data);
            }

            return L;
        }


    Btree creat_tree(int N) //表示创建  4个节点的搜索树
        {
            Btree p;
            init_Btree(p);
            cout<<"请输入"<<N<<"个数据来构造树"<<endl;
            int i=0;
            int data;
            for(i=0;i<N;i++)
            {
                cin>>data;    //依次输入
                p=insert_Node(p,data);
            }

            return p;
        }

    int  check_tree(Btree L,int e)//在T里面去搜索e  在查找的时候我们来看是否有节点以前没碰到过
        {
            if(L->flag==1)
                {
                    if(L->data<e)
                        {
                            return check_tree(L->right,e);
                        }
                    else if(L->data>e)
                        {
                            return check_tree(L->left,e);
                        }
                    else
                        {
                            return 0;   //一个树的节点重复出现了两次
                        }
                }

            else
                {
                    if(L->data==e) //如果未查找的这个刚好是e
                        {
                            L->flag=1;
                            return 1;
                        }
                    else
                        {
                            return 0;    //否则不是一致的
                        }
                }
        }


    int judge_tree(Btree L,int N) //判断这N个数是否和已知的树一致
        {
            int e;
            int flag=1;
            cout<<"请输入"<<N<<"个数"<<endl;
            for(int j=0;j<N;j++)
            {
                cin>>e;
                if(flag!=0)
                    {
                       if(check_tree(L,e)==0)
                       {
                           flag=0;
                       }

                    }

            }

            if(flag==0)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }


    void delete_tree(Btree &L)   //释放一棵树
        {
            if(L!=NULL)
            {
                delete_tree(L->left);
                delete_tree(L->right);
                delete L;
            }
        }

    void Resert_flag(Btree &L)//将这棵树的所有flag重新赋值为0
        {
            if(L!=NULL)
            {
                L->flag=0;
                Resert_flag(L->left);
                Resert_flag(L->right);
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值