//前序
void PreOrderINversal(struct BinNode* Bintree)
{
struct BinNode* Temp;
stack s=CreateStack();
Temp=Bintree;
while(Temp||!IsEmpty(stack)){
while(Temp){
printf("%d ",Temp->Element);
Push(s,temp);
temp=temp->next;
}
Temp=Pop(s);
Temp=Temp->Right;
}
}
//中序
void InOrderINversal(struct BinNode* Bintree)
{
struct BinNode* Temp;
stack s=CreateStack();
Temp=Bintree;
while(Temp||!IsEmpty(stack)){
while(Temp){
Push(s,temp);
temp=temp->next;
}
Temp=Pop(s);
printf("%d ",Temp->Element);
Temp=Temp->Right;
}
}
//后序
void PostOrderINversal(struct BinNode* Bintree)
{
struct BinNode* Temp;
stack s=CreateStack();
Temp=Bintree;
while(Temp||!IsEmpty(stack)){
while(Temp){
Temp->Time++;
Push(s,temp);
Temp=temp->Left;
}
Temp=Pop(s);
if(Temp->Time==2){
printf("%d ",Temp->Element);
Pop(s);
}
else{
Temp->Time++;//二次入栈
Push(s,Temp);
Temp=Temp->Right;
}
}
}
二叉树的遍历 c++标准栈实现
最新推荐文章于 2024-08-15 17:11:26 发布