非递归遍历二叉树

#include<stdio.h>
#include<malloc.h>

typedef struct binode{
    char data;
    struct binode *lchild;
    struct binode *rchild;
}BiNode,*BiTree;
/****************************
 *输入创建二叉树: abd##ef###c##
 *其实输入按照先序顺序,#表示叶子节点
 *****************************/
void create(BiTree t){
    char ch=(char)getchar();
    if(ch=='#'){
        t->data='#';
        t->lchild=NULL;
        t->rchild=NULL;
    }
    else{
        t->data=ch;
        t->lchild=(BiTree)malloc(sizeof(BiNode));
        create(t->lchild);
        t->rchild=(BiTree)malloc(sizeof(BiNode));
        create(t->rchild);
    }  
}
//先序遍历
void preTraverse(BiTree t){
    BiTree p=t;
    BiTree stack[20]; //使用栈来替代递归方法
    int top=0;
    while(top>=0){
       while(p->data!='#'){
           printf("%c ",p->data);
           stack[top++]=p;
           p=p->lchild;
       }
       if(top>0)
          p=stack[--top]->rchild;
       else 
          top=-1;
       
    }
}
//中序遍历,和先序差不多
void midTraverse(BiTree t){
    BiTree p=t;
    BiTree stack[20];
    int top=0;
    while(top>=0){
        while(p->data!='#'){
           stack[top++]=p;
           p=p->lchild;
        }
        if(top>0){
           p=stack[--top];
           printf("%c ",p->data);
           p=p->rchild;
        }else
           top=-1;
    }
}
//后序遍历,稍微复杂一点
void afeTraverse(BiTree t){
    BiTree p=t,q=t;
    BiTree stack[20];
    int top=0;
    while(top>=0){
        while(p->data!='#'){
           stack[top++]=p;
           p=p->lchild;
        }
        if(q->rchild==p){
           printf("%c ",q->data);
           q->data='#'; //遍历完的节点直接做叶子标记
           --top; 
        }
        if(top>0){
           p=stack[top-1];
           q=p;
           p=p->rchild;
           
        }
    }
}
//测试
int main(){
    BiTree root=(BiTree)malloc(sizeof(BiNode));
    create(root);
    afeTraverse(root);
    printf("\n");
    return 1;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值