二叉树的遍历 c++标准栈实现

来源:互联网 发布:乐之网络 编辑:程序博客网 时间:2024/06/05 00:31
//前序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;        }    }}

1 0
原创粉丝点击