二叉樹的非遞歸遍歷 前序 中序 後序 -----完美運行

#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef struct
{
    bitree ptr;
    int flag;
}stacknode;
typedef struct
{
    stacknode elm[MAX];
    int top;
}sqstack;
typedef struct btnode
{
    char data;
    struct btnode *lchild,*rchild;
}btnode,*bitree;
bitree Creatbintree()               //前序遍歷創建二叉樹
{
    char ch;
    bitree bt;
    scanf("%c",&ch);
    if(ch=='#') bt=NULL;
    else
    {
        bt=(btnode*)malloc(sizeof(btnode));
        bt->data=ch;
        bt->lchild=Creatbintree();
        bt->rchild=Creatbintree();
    }
    return bt;
}
typedef struct
{
    bitree data[MAX];
    int top;
}seqstack;
seqstack *Init_seqstack()
{
    seqstack *s;
    s=malloc(sizeof(seqstack));
    if(s) s->top=-1;
    return s;
}
int push(seqstack *s,bitree e)
{
    if(s->top==MAX-1) return 0;
    else
    {
        s->top++;
        s->data[s->top]=e;
        printf("in e=%c\n",e->data);
        return 1;
    }
}
bitree pop(seqstack *s)
{
    if(s->top==-1) return 0;
    else
    {
        bitree e;
        e=s->data[s->top];
        printf("out e2=%c\n",e->data);
        s->top--;
        return e;
    }
}
void Preorder(bitree bt)                //前序遍歷
{
    bitree p;
    seqstack *s;
    s=Init_seqstack();
    p=bt;
    printf("p=%c",p->data);

    while(p!=NULL||s->top!=-1)
    {
        while(p!=NULL)
        {
            push(s,p);
            p=p->lchild;
        }
        if(s->top!=-1)
        {
            p=pop(s);
            printf("%c\n",p->data);
            p-p->rchild;
        }
    }
}
void Inorder(bitree bt)                 //中序遍歷
{
        SqStack s;
    bitree  p;
    s=SqStack *Init();
    p=bt;
    while(p!=NULL||stackempty(s))
    {
        while(p!=NULL)
        {
            push(s,p);
            p=p->lchild;
        }
        if(!stackempty(s))
        {
            p=pop(s);
            printf("%d",p->data);
            p=p->rchild;
        }
    }
}
void Posorder(bitree bt)                //後序遍歷
{
        SqStack s;
    stacknode x;
    s=SqStack *Init();
    p=bt;
    while(p!=NULL||!stackempty(s))
    {
        if(p!NULL)
        {
            x.ptr=p;
            x.flag=1;
            push(s,x);
            p=p->lchild;
        }
        else
        {
            x=pop(s);
            if(x.flag==1)
            {
                x.flag=2;
                p=x.ptr;
                push(s,x);
                p=p->lchild;
            }
            else
            {
                p=x.ptr;
                printf("%d",p->data);
                p=NULL;
            }
        }
    }
}
int main()
{
    bitree bt;
    printf("請按前序遍的次序歷輸入二叉樹(空節點爲#):\n");
    bt=Creatbintree();
    printf("前序遍歷得到的次序\n");
    Preorder(bt);
    printf("\n中序遍歷得到的次序\n");
    Inorder(bt);
    printf("\n後序遍歷得到的次序\n");
    Posorder(bt);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章