zoukankan      html  css  js  c++  java
  • 二叉搜索树浙大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;
    }
  • 相关阅读:
    Android中GC_EXTERNAL_ALLOC的含义
    Phonegap开发的前后台数据交互
    代码管理工具TortoiseSVN
    14款响应式前端开发框架
    简化工作流程,10款必备的HTML5开发工具
    [C#.net]处理UTF-8文件乱码
    [Oracle]ORA-14400:插入的分区关键字未映射到任何分区
    [网络]10M、100M、1000M网线的水晶头接法
    [Office]Execl取消保护密码
    SLI的相关学习
  • 原文地址:https://www.cnblogs.com/lgy6534588/p/3002030.html
Copyright © 2011-2022 走看看