二叉树非递归遍历(以完全二叉树为例)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int MAX=5000;

int main(){
    printf("/**二叉树先序遍历和后序遍历算法*/\n");
    int bt[MAX];                     ///记录二叉树的元素
    int n;                              ///记录二叉树元素个数
    printf("请输入二叉树元素个数:");
    scanf("%d",&n);
    printf("请按从上到下从左到右的顺序依次输入二叉树元素:\n");
    for(int i=1;i<=n;i++){
        scanf("%d",&bt[i]);
    }

    int Stack[MAX];
    int mark[MAX];
    memset(mark, 0,sizeof(mark));
    printf("先序遍历结果为:");
    int top=-1;
    Stack[++top]=1;
    printf("%d ",bt[1]);
    mark[1]=1;
    while(~top){
        int t=Stack[top];
        int l=t*2,r=t*2+1;
        if(l>n){              ///无左右子树
            top--;
            continue ;
        }
        else if(r<=n){        ///有左右子树
            if(!mark[l]){
                printf("%d ",bt[l]);
                Stack[++top]=l;
                mark[l]=1;
            }
            else if(!mark[r]){
                printf("%d ",bt[r]);
                Stack[++top]=r;
                mark[r]=1;
            }
            else {
                top--;
                continue;
            }
        }
        else {               ///只有左子树
            if(!mark[l]){
                printf("%d ",bt[l]);
                Stack[++top]=l;
                mark[l]=1;
            }
            else {
                top--;
                continue;
            }
        }
    }

    memset(mark,0,sizeof(mark));
    top=-1;
    Stack[++top]=1;
    printf("\n\n后序遍历结果为:");
    while(~top){
        int t=Stack[top];
        int l=t*2,r=t*2+1;
        if(l>n){              ///无左右子树
            printf("%d ",bt[t]);
            top--;
            continue ;
        }
        else if(r<=n){        ///有左右子树
            if(!mark[l]){
                Stack[++top]=l;
                mark[l]=1;
            }
            else if(!mark[r]){
                Stack[++top]=r;
                mark[r]=1;
            }
            else {
                printf("%d ",bt[t]);
                top--;
                continue;
            }
        }
        else {               ///只有左子树
            if(!mark[l]){
                Stack[++top]=l;
                mark[l]=1;
            }
            else {
                printf("%d ",bt[t]);
                top--;
                continue;
            }
        }
    }

    memset(mark,0,sizeof(mark));
    top=-1;
    Stack[++top]=1;
    printf("\n\n中序遍历结果为:");
    while(~top){
        int t=Stack[top];
        int l=t*2,r=t*2+1;
        if(l>n){              ///无左右子树
            printf("%d ",bt[t]);
            top--;
            continue ;
        }
        else if(r<=n){        ///有左右子树
            if(mark[l]&&!mark[r])
                printf("%d ",bt[t]);

            if(!mark[l]){
                Stack[++top]=l;
                mark[l]=1;
            }
            else if(!mark[r]){
                Stack[++top]=r;
                mark[r]=1;
            }
            else {
                top--;
                continue;
            }
        }
        else {               ///只有左子树
            if(mark[l])
                printf("%d ",bt[t]);

            if(!mark[l]){
                Stack[++top]=l;
                mark[l]=1;
            }
            else {
                top--;
                continue;
            }
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值