中根序 线索化 二叉树

来源:互联网 发布:php前景 编辑:程序博客网 时间:2024/06/03 19:58

void thread(ThrTree t){

PSeqStack st=createEmptyStack(M);

ThrTree p,pr;

if(t==NULL)return;

p=t;

pr=NULL;

do{

           while(p!=NULL){

                       push_seq(st,p);

                       p=p->llink;

           }

           p=top_seq(st);

           pop_seq(st);

           if(pr!=NULL){

                      if(pr->rlink==NULL){

                                 pr->rlink=p;pr->rtag=1;

                      }

                      if(p->llink==NULL){

                                p->llink=pr;p->ltag=1;

                      }

          } 

         pr=p;

         p=p->rlink;         

}while(!isEmptyStack_seq(st)||p!=NULL);

}

 

 

 

//中根序周游的非递归算法

void nInOrder(BinTree t){

Stack s=createEmptyStack();

BinTree c=t;

if(c==NULL)return;

do{

            while(c!=NULL){

                           push(s,c);

                           c=leftChild(c);

            }

            c=top(s);

            pop(s);

            visit(root(c));

            c=rightChild(c);

}while(c!=NULL||!isEmptyStack(s));

}