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

#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;
}
发布了62 篇原创文章 · 获赞 7 · 访问量 1623

猜你喜欢

转载自blog.csdn.net/qq_43331910/article/details/102651433