如何用非递规来做二叉树的后序遍历

来源:互联网 发布:粒子群算法参数 编辑:程序博客网 时间:2024/05/01 10:02
Q :如何用非递规来做二叉树的后序遍历主要解答者:zgy231552提交人:langhaixin感谢:zgy231552审核者:starfish社区对应贴子:查看     A :
二叉树的结构  
struct  bt  
{  
         char  data;  
         struct  bt  *ltree;  
         struct  bt  *rtree;  
}  
---------------------------------------------------------------  
 
上次写错了struct  bt  
//要设置一个标志位tag  
{  
         char  data;  
         int  tag;  
         struct  bt  *ltree;  
         struct  bt  *rtree;  
}  
typedef    struct  bt  BT;  
typedef  BT  *link;  
link  root=NULL;  
link  create_tree(link  root)  
{..................};  
void  postorder(link  root)  
{  
   link  p=root;  
   while(p!=NULL&&!isempty())  
   {  
       while(p!=NULL)  
       {    //push  it  into  the  stack  
               p->tag=0;//left  tree  
               top++;  
               push(stack,p);  
               p=p->left;    
       }  
     int  continue=1;  
   while(p!=NULL&&continue)  
 {  
               p=gettop(stack);  
               pop();  
     switch(p->tag){  
   case  0:  continue=0;  
                   p->tag=1;  
                   push(stack,p);  
                   p=p->right;  
                   break;  
 case  1:  printf("%c",p->data);  
                 break;  
     }  
}  
}  
}