二叉树的非递归前序,中序,和后序遍历

 前序:

void inorderTraversal(BinTree BT){
    BinTree T BT;
    Stack S = CreatStack(MaxSize);
    while(T||isEmpty(S){
        while(T)   {
            push(S,T);
            printf("%d",T->Data);
            T=T->Left;
        }
        if(!isEmpty(S){
            T = pop(S);
            T=T->Right;
        }
    }
}

 中序:

void inorderTraversal(BinTree BT){
    BinTree T BT;
    Stack S = CreatStack(MaxSize);
    while(T||isEmpty(S){
        while(T)   {
            push(S,T);
            T=T->Left;
        }
        if(!isEmpty(S){
            T = pop(S);
            printf("%d",T->Data);
            T=T->Right;
        }
    }
}

 后序:

void InOrderTraversal( BinTree BT )
{
    BinTree T = BT;
    Stack S = CreatStack( MaxSize); /*创建并初始化堆栈S * ,
    while(T || !Isempty(S)){
        while (T) {
            Push(ST) ;
            T.flag=1;
            T->Left ;
        }
        if( !IsEmpty(S) ){
            T = GetTop(S);
            if(T.flag = 1){ //初始值为0
                T.flag = 2;  
                T=T->Right;
                   
            }else if(T.flag = 2) {
                t = pop(S) 
                printf("%d",t->Data); 
                T=null;
            }
           
        }
}

猜你喜欢

转载自blog.csdn.net/weixin_38902950/article/details/88068808
今日推荐