二叉树的几种递归

第三次更,已对
#include
#include

typedef struct BiTreeNode{
    char data;
    struct BiTreeNode *lchild,*rchild;
}BiTreeNode,*BiTree;

void  Create(BiTree &T)
{
    char ch;
    ch=getchar();
    if(ch=='.'){
        T=NULL;
    }    
    else{
        T=(BiTree)malloc(sizeof(BiTreeNode));
        T->data=ch;
        Create(T->lchild);
        Create(T->rchild);
    }
}
void PreOrder_1(BiTree T)
{
    if(T!=NULL){
        printf("%c",T->data);
        PreOrder_1(T->lchild);
        PreOrder_1(T->rchild);
    }
}
void PreOrder_2(BiTree T)
{
    BiTree p;
    BiTree S[100];
    int top=0;
    S[top]=T;
    while(top!=-1){
        p=S[top];
        top--;
        printf("%c",p->data);
        if(p->rchild) S[++top]=p->rchild;
        if(p->lchild) S[++top]=p->lchild;
    }
}
void InOrder_1(BiTree T)
{
    if(T){
        InOrder_1(T->lchild);
        printf("%c",T->data );
        InOrder_1(T->rchild);
    }
}
void InOrder_2(BiTree T)
{
    BiTree S[100],p;
    int top=0;
    S[top]=T;
    while (top!=-1){
        while(p=S[top]){
            S[++top]=p->lchild;
        }
        top--;
        if(top!=-1){
            p=S[top--];
            printf("%c",p->data);
            S[++top]=p->rchild;
        }
    }
}
void InOrder_3(BiTree T)
{
    BiTree S[100],p=T;
    int top=-1;
    while(top!=-1||p){
        if(p){
            S[++top]=p;
            p=p->lchild;
        }
        else{
            p=S[top--];
            printf("%c",p->data);
            p=p->rchild;
        }
    }
}
void PostOrder_1(BiTree T)
{
    if(T){
        PostOrder_1(T->lchild);
        PostOrder_1(T->rchild);
        printf("%c",T->data);
    }
}
void PostOrder_2(BiTree T)
{
    typedef struct{
        BiTree ptr;
        int mark;
    }Stack;
    Stack S[100];
    int top=0;
    for(int i=0;i<100;i++)
    {
        S[i].ptr=NULL;
        S[i].mark=0;
    }
    Stack p;
    S[top].ptr=T;
    while(top!=-1){
        p=S[top--];
        switch (p.mark){
            case 0: 
            p.mark=1;
            S[++top]=p;
            if(p.ptr->lchild){
                p.ptr=p.ptr->lchild;
                p.mark=0;
                S[++top]=p;
            }
            break;
            case 1:
            p.mark=2;
            S[++top]=p;
            if(p.ptr->rchild){
                p.ptr=p.ptr->rchild;
                p.mark=0;
                S[++top]=p;
            }
            break;
            case 2:
            printf("%c",p.ptr->data);
        }
    }
}
 
void LayerOrder(BiTree T)
{
    BiTree p;
    BiTree Q[100];
    int front,rear=front=0;
    Q[rear++]=T;
    while (front!=rear){
        p=Q[front++];
        printf("%c",p->data);
        if(p->lchild)  Q[rear++]=p->lchild;
        if(p->rchild)  Q[rear++]=p->rchild;
    }
}
void PostFree(BiTree &T)
{
    if(T)
    {
        PostFree(T->lchild);
        PostFree(T->rchild);
        printf("%c",T->data);
        free(T);
    }
}
void InFree(BiTree &T)
{
    if(T){
        InFree(T->lchild);
        BiTree p;
        p=T->rchild;
        printf("%c",T->data);
        free(T);
        InFree(p);
    }
}
void PreFree(BiTree &T)
{
    if(T){
        BiTree l=T->lchild,r=T->rchild;
        printf("%c",T->data);
        free(T);
        PreFree(l);
        PreFree(r);
    }
}
int main()
{
    BiTree T;
    Create(T);
    printf("the PreOrder_1:\n");
    PreOrder_1(T);
    printf("\n"); 
    printf("the PreOrder_2:\n");
    PreOrder_2(T);
    printf("\n"); 
    printf("the InOrder_1:\n");
    InOrder_1(T);
    printf("\n"); 
    printf("the InOrder_2:\n");
    InOrder_2(T);
    printf("\n"); 
    printf("the InOrder_3:\n");
    InOrder_3(T);
    printf("\n"); 
    printf("the PostOrder_1:\n");
    PostOrder_1(T);
    printf("\n"); 
    printf("the PostOrder_2:\n");
    PostOrder_2(T);
    printf("\n"); 
    printf("the LayerOrder:\n");
    LayerOrder(T);
    printf("\n"); 
    PreFree(T);
}

你可能感兴趣的:(二叉树的几种递归)