二叉树的先序非递归遍历(注释版)

/* No recusive to realize the travle of tree */
void NoPreOrder( BiTree root )
{/*{{{*/

    
    Stack S;
    BiTree P;
    P = root;
    S = CreateStack();
    while( P!=NULL || !IsEmpty(S) )//判断条件分别对应着
                                   //1,S is not empty && P = NULL 当前节点为上一节点的右子树,右子树为空,但还有上一节点的上一节点的右子树
                                   //未被遍历
                                   //2.s is empty && p != NULLQ.情况唯一 :初始根节点被退出,但还有右子树需要遍历
                                   //3,s is not empty $$ p != NULL 栈未被退完,而且当前右子树也不为空
    {
        while( P != NULL )
        {
            visit( P );//先序访问根节点
            Push( P , S );//根节点入栈,以备访问右子树
            P = P->Left;//继续访问左子树
        }
        if( !IsEmpty(S) )
        {
             P = Pop( S );//出栈
            P = P->Right;//遍历被访问过的根节点的右子树
        }
    }

}/*}}}*/
原文地址:https://www.cnblogs.com/dilidingzhi/p/4064986.html