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

 

/*
输入先序和中序,构造二叉树,并输出该二叉树的层序、前序、中序、后序遍历结构
输入后序和中序,构造二叉树,并输出该二叉树的层序、前序、中序、后序遍历结构
*/
#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("\n");
}
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("\n");
}
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("\n");
}
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("\n");
}
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;
}

 

posted @ 2019-11-20 11:43  dreamy_java  阅读(222)  评论(0编辑  收藏  举报