二叉排序樹

#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
    int data;
    struct node *l,*r;
}*tree;
int flag;
void creat1(int n,tree &root)   // 第一種建樹 要加& 建樹需要
{
    if(!root)
    {
        root=new node;
        root->data=n;
        root->l=NULL;
        root->r=NULL;
    }
    else
    {
        if(root->data > n)
        {
           creat1(n,root->l);
        }
        else
        {
           creat1(n,root->r);
        }
    }
}
void creat(tree &root,int a[],int n) //第二種建樹 要加& 建樹需要
{
    root=new node;
    root->l=NULL;
    root->r=NULL;
    root->data=a[0];
    for( int i=1;i<n;i++)
    {
        tree q=root;
        while(1)
        {
            if(a[i]>q->data)
            {
                if(q->r)
                {
                    q=q->r;
                }
                else
                {
                    tree p;
                    p=new node;
                    p->data=a[i];
                    p->l=NULL;
                    p->r=NULL;
                    q->r=p;
                    break;
                }
            }
            else
            {
                if(q->l)
                {
                    q=q->l;
                }
                else
                {
                    tree p;
                    p=new node;
                    p->l=NULL;
                    p->r=NULL;
                    p->data=a[i];
                    q->l=p;
                    break;
                }
            }
        }
    }
}
void xianxu(tree &root)
{
    if(root)
    {
        printf("%d",root->data);
        xianxu(root->l);
        xianxu(root->r);
    }
}
int judge(tree &root1,tree &root2)
{
    if(root1&&root2)
    {
        if(root1->data!=root2->data)
        {
            flag=1;
            return flag;
        }
        judge(root1->l,root2->l);
        judge(root1->r,root2->r);
    }
    return flag;
}
void change(char a[],int aa[]) // 轉換數據類型
{
    int i;
    for( i=0;i<strlen(a);i++)
    {
        if(a[i]=='9')
        {
            aa[i]=9;
        }
        if(a[i]=='8')
        {
            aa[i]=8;
        }
        if(a[i]=='7')
        {
            aa[i]=7;
        }
        if(a[i]=='6')
        {
            aa[i]=6;
        }
        if(a[i]=='5')
        {
            aa[i]=5;
        }
        if(a[i]=='4')
        {
            aa[i]=4;
        }
        if(a[i]=='3')
        {
            aa[i]=3;
        }
        if(a[i]=='2')
        {
            aa[i]=2;
        }
        if(a[i]=='1')
        {
            aa[i]=1;
        }
        if(a[i]=='0')
        {
            aa[i]=0;
        }
    }
}
int main()
{
    int i,t;
    char a[20];
    char b[20];
    int aa[20];
    int bb[20];
    while(~scanf("%d",&t)&&t!=0)
    {
        scanf("%s",a);
        change(a,aa);
        tree root;
        creat(root,aa,strlen(a));
        while(t--)
        {
            scanf("%s",b);
            change(b,bb);
            tree root1;
            root1=NULL;
            creat(root1,bb,strlen(a));
            flag=0;
            judge(root,root1);
            if(flag==1)
            {
                printf("NO\n");
            }
            else
            {
                printf("YES\n");
            }
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章