二叉搜索树浙大10年机试题

 

#include<cstdio>
#include<cstring>
struct node{
    node *lchild;
    node *rchild;
    int r;
}tree[22];

int loc;
int pre[11];
int ino[11];
int post[11];
int pre2[11];
int ino2[11];
int post2[11];
int pos;

node *create()
{
    tree[loc].lchild = tree[loc].rchild = NULL;
    return &tree[loc ++];
}

node *build(node *t, int x)
{
    if(t == NULL)
    {
        t = create();
        t -> r = x;
        return t;
    }
    else
    {
        if(x < t -> r)
            t -> lchild = build(t -> lchild , x);
        if(x > t -> r)
            t -> rchild = build(t -> rchild , x);
        return t;
    }
}

void preOrder(node *t)
{
    pre[pos ++] = t -> r;
    if(t -> lchild != NULL)
        preOrder(t -> lchild);
    if(t -> rchild != NULL)
        preOrder(t -> rchild);
}

void inOrder(node *t)
{
    if(t -> lchild != NULL)
        inOrder(t -> lchild);
    ino[pos ++] = t -> r;
    if(t -> rchild != NULL)
        inOrder(t -> rchild);
}

void postOrder(node *t)
{
    if(t -> lchild != NULL)
        postOrder(t -> lchild);
    if(t -> rchild != NULL)
        postOrder(t -> rchild);
    post[pos++] = t -> r;
}

void preOrder2(node *t)
{
    pre2[pos ++] = t -> r;
    if(t -> lchild != NULL)
        preOrder2(t -> lchild);
    if(t -> rchild != NULL)
        preOrder2(t -> rchild);
}

void inOrder2(node *t)
{
    if(t -> lchild != NULL)
        inOrder2(t -> lchild);
    ino2[pos ++] = t -> r;
    if(t -> rchild != NULL)
        inOrder2(t -> rchild);
}

void postOrder2(node *t)
{
    if(t -> lchild != NULL)
        postOrder2(t -> lchild);
    if(t -> rchild != NULL)
        postOrder2(t -> rchild);
    post2[pos ++] = t -> r;
}
int main()
{
    char s[11];
    int n;
    while(scanf("%d",&n) != EOF)
    {
        if(n == 0)
            break;
        loc = 0;
        getchar();
        gets(s);
        node *t = NULL;
        for(int i = 0; i < strlen(s); i ++)
        {
            int x = s[i] - '0';
            t = build(t ,x);
        }
        
        pos = 0;
        preOrder(t);
        pos = 0;
        inOrder(t);
        pos = 0;
        postOrder(t);
        int rec = loc;
        char s1[11];
        for(int i = 0; i < n; i ++)
        {
            loc = rec;
            memset(s1,'\0',sizeof(s1));
            memset(pre2,0,sizeof(pre2));
            memset(ino2,0,sizeof(ino2));
            memset(post2,0,sizeof(post2));        
            gets(s1);
            node *p = NULL;
            for(int i = 0; i < strlen(s1); i ++)
            {
                int x = s1[i] - '0';
                p = build(p ,x);
            }
            pos = 0;
            preOrder2(p);
            pos = 0;
            inOrder2(p);
            pos = 0;
            postOrder2(p);
            bool flag = true;
            for(int i = 0; i < strlen(s); i ++)
            {
                if((pre[i] != pre2[i]) || (ino[i] != ino2[i]) || (post[i] != post2[i]))
                {
                    flag = false;
                    break;
                }
            }
            if(flag == true)
                printf("YES\n");
            else
                printf("NO\n");
        }
        
    }
    return 0;
}

 

posted on 2013-04-06 10:43  lgy111  阅读(133)  评论(0编辑  收藏  举报

导航