//非递归中序遍历
Status InOrderTraverse(BiTree T,Status (*Visit)(TElemType))
{
SqStack S;
InitStack(S);
while(T||!StackEmpty(S))
{
if(T)
{
Push(S,T);
T=T->lchild;
}
else
{
Pop(S,T);
if(!Visit(T->data)
return ERROR;
T=T->rchild;
}
}
cout<<endl;
return OK;
}
//层序遍历,借助队列
void LevelOrderTraverse(BiTree T,Status(*Visit)(TElemType))
{
LinkQueue q;
QElemType e;
if(T)
{
InitQueue(q);
EnQueue(q,T);
while(!QueueEmpty(q))
{
DeQueue(q,e);
Visit(e->data);
if(e->lchild!=NULL)
EnQueue(q,e->lchild);
if(e->rchild!=NULL)
EnQueue(q,e->rchild);
}
}
}
Status InOrderTraverse(BiTree T,Status (*Visit)(TElemType))
{
SqStack S;
InitStack(S);
while(T||!StackEmpty(S))
{
if(T)
{
Push(S,T);
T=T->lchild;
}
else
{
Pop(S,T);
if(!Visit(T->data)
return ERROR;
T=T->rchild;
}
}
cout<<endl;
return OK;
}
//层序遍历,借助队列
void LevelOrderTraverse(BiTree T,Status(*Visit)(TElemType))
{
LinkQueue q;
QElemType e;
if(T)
{
InitQueue(q);
EnQueue(q,T);
while(!QueueEmpty(q))
{
DeQueue(q,e);
Visit(e->data);
if(e->lchild!=NULL)
EnQueue(q,e->lchild);
if(e->rchild!=NULL)
EnQueue(q,e->rchild);
}
}
}