前序遍历
void UnRecPreTraversal(BinaryTree *pTree)
{
if(pTree == NULL)return;
//申请辅助栈
Stack *pStack = NULL;
s_Init(&pStack);
while(1)
{
//当前节点非空
while(pTree)
{
//打印 入栈 向左走
printf("%d ",pTree->nValue);
s_Push(pStack,pTree);
pTree = pTree->pLeft;
}
//弹出
pTree = s_Pop(pStack);
//栈空 结束
if(pTree == NULL)break;
//右
pTree = pTree->pRight;
}
}
中序遍历
void UnRecInTraversal(BinaryTree *pTree)
{
if(pTree == NULL)return;
//申请辅助栈
Stack *pStack = NULL;
s_Init(&pStack);
while(1)
{
//当前节点非空
while(pTree)
{
// 入栈 向左走
s_Push(pStack,pTree);
pTree = pTree->pLeft;
}
//弹出
pTree = s_Pop(pStack);
//栈空 结束
if(pTree == NULL)break;
//打印
printf("%d ",pTree->nValue);
//右
pTree = pTree->pRight;
}
}
后续遍历
void UnRecLastTraversal(BinaryTree *pTree)
{
if(pTree == NULL)return;
//辅助栈
Stack *pStack = NULL;
s_Init(&pStack);
BinaryTree *pMark = NULL;
while(1)
{
while(pTree)
{
s_Push(pStack,pTree);
pTree = pTree->pLeft;
}
if(pStack->pTop == NULL)break;
if(pStack->pTop->nValue->pRight == NULL || pStack->pTop->nValue->pRight == pMark)
{
//弹出 标记 打印
pMark = s_Pop(pStack);
printf("%d ",pMark->nValue);
}
else
{
//右 非空 且未被标记
pTree = pStack->pTop->nValue->pRight;
}
}