• 输入后序和中序,构造二叉树,并输出该二叉树的层序、前序、中序、后序遍历结构;输入后序和中序,构造二叉树,并输出该二叉树的层序、前序、中序、后序遍历结构


    /*
    输入先序和中序,构造二叉树,并输出该二叉树的层序、前序、中序、后序遍历结构
    输入后序和中序,构造二叉树,并输出该二叉树的层序、前序、中序、后序遍历结构
    */
    #include <stdio.h>
    #include<string.h>
    #include<malloc.h>
    typedef struct node{
        char data;
        struct node *lchild;
        struct node *rchild;
    }BTree;
    BTree *createPre(char *pre,char *in,int n){
        if(n<=0) return NULL;
        char *p;
        int k;
        BTree *b;
        b=(BTree *)malloc(sizeof(BTree));
        b->data=*pre;
        for(p=in;p<in+n;p++)
            if(*p==*pre)
                break;
        k=p-in;
        b->lchild=createPre(pre+1,in,k);
        b->rchild=createPre(pre+k+1,p+1,n-k-1);
        return b;
    }
    BTree *createPost(char *post,char *in,int n){
        if(n<=0) return NULL;
        char *p,r;
        int k;
        r=*(post+n-1);
        BTree *b;
        b=(BTree *)malloc(sizeof(BTree));
        b->data=r;
        for(p=in;p<in+n;p++)
            if(*p==r)
                break;
        k=p-in;
        b->lchild=createPost(post,in,k);
        b->rchild=createPost(post+k,p+1,n-k-1);
        return b;
    }
    void presort(BTree *b){
        BTree *p,*st[10];
        int top=-1;
        p=b;
        while(p!=NULL||top>-1){
            while(p!=NULL){
                printf("%c ",p->data);
                top++;
                st[top]=p;
                p=p->lchild;
            }
            if(top>-1){
                p=st[top];
                top--;
                p=p->rchild;
            }
        }
        printf("
    ");
    }
    void insort(BTree *b){
        BTree *p,*st[10];
        int top=-1;
        p=b;
        while(p!=NULL||top>-1){
            while(p!=NULL){
                top++;
                st[top]=p;
                p=p->lchild;
            }
            if(top>-1){
                p=st[top];
                top--;
                printf("%c ",p->data);
                p=p->rchild;
            }
        }
        printf("
    ");
    }
    void postsort(BTree *b){
        BTree *p,*st[10];
        int t1=-1,t2=-1,tag[10],f;
        p=b;
        while(p!=NULL||t1>-1){
            if(p!=NULL){
                t1++;
                st[t1]=p;
                t2++;
                tag[t2]=1;
                p=p->lchild;
            }else{
                p=st[t1];
                t1--;
                f=tag[t2];
                t2--;
                if(f==1){
                    t1++;
                    st[t1]=p;
                    t2++;
                    tag[t2]=2;
                    p=p->rchild;
                }else{
                    printf("%c ",p->data);
                    p=NULL;
                }
            }
        }
        printf("
    ");
    }
    void censort(BTree *b){
        BTree *p,*qu[10];
        int f=-1,r=-1;
        p=b;
        r++;
        qu[r]=p;
        while(f!=r){
            f=(f+1)%10;
            p=qu[f];
            printf("%c ",p->data);
            if(p->lchild!=NULL){
                r=(r+1)%10;
                qu[r]=p->lchild;
            }
            if(p->rchild!=NULL){
                r=(r+1)%10;
                qu[r]=p->rchild;
            }
        }
        printf("
    ");
    }
    int main()
    {
        char pre[100],in[100],post[100];
        int n=7;
        BTree *b;
        gets(pre);
        gets(in);
        b=createPre(pre,in,n);
        censort(b);
        presort(b);
        insort(b);
        postsort(b);
        gets(post);
        gets(in);
        b=createPost(post,in,n);
        censort(b);
        presort(b);
        insort(b);
        postsort(b);
        return 0;
    }
  • 相关阅读:
    优酷网的架构学习笔记
    同网关劫持与不同网关劫持实例
    .flv和.swf格式文件flash播放器代码
    让IE6显示透明PNG背景图片
    新手买车的九大原则
    服务器不支持FLV视频播放的原因
    功能强大易用的Web视频播放器——Flowplayer(使用方法及演示)
    一个不错的媒体网页播放器(国外的)
    网页表格中单元格线条及边框的设置
    20多个漂亮的使用jQuery交互的网站设计欣赏
  • 原文地址:https://www.cnblogs.com/zhaohuan1996/p/11896750.html
Copyright © 2020-2023  润新知