27

问答题 27 /115

实现一个先序遍历非递归算法。

参考答案

void PreOrderUnrec(Bitree *t)
{
    Stack s;
    StackInit(s);
    Bitree *p = t;
    while (p != NULL || !StackEmpty(s))

        while (p != NULL) //遍历左子树
        {
            visite(p->data);
            push(s, p);
            p = p->lchild;
        }
    if (!StackEmpty(s)) //通过下一次循环中的内嵌while 实现右子树遍历
    {
        p = pop(s);
        p = p->rchild;
    }//endif
}//endwhile
}