二叉树的非递归遍历
- 中序遍历非递归遍历算法
非递归算法实现的基本思路:使用堆栈
中序遍历非递归遍历算法
- 遇到一个结点,就是它压栈,并去遍历它的左子树
- 当左子树遍历结束后,从栈顶弹出这个结点并访问它
- 然后按其右指针再去中序遍历该结点的右子树
void InOrderTraversal(BinTree BT)
{ BinTree T=BT;
Stack S = CreateStack(MaxSize);
while (T || !IsEmpty(S)) {
while (T)
{
Push(S,T);
T=T->Left;
}
if (!isEmpty(S))
{
T=pop(S);
printf("%5d",T->Data);
T=T->Right;
}
}
}