二叉树的非递归遍历 前序 中序 后序 -----完美运行

#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;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值