typedef struct ThreadNode{
int data;
struct ThreadNode *lchild,*rchild;
int ltag,rtag;
}ThreadNode,*ThreadTree;
void CreateInThread(ThreadTree T){
ThreadTree pre=NULL;
if(T!=NULL){
InThread(T,pre);
pre->rchild=NULL;
pre->rtag=1;
}
}
void InThread(ThreadTree &p,ThreadTree &pre){
if(p!=NULL){
InThread(p->lchild,pre);
if(p->lchild==NULL){
p->lchild=pre;
p->ltag=1;
}
if(pre!=NULL&&pre->rchild==NULL){
pre->rchild=p;
pre->rtag=1;
}
pre=p;
InThread(p->rchild,pre);
}
}
ThreadTree FirstNode(ThreadTree p){
while(p->ltag==0)p=p->lchild;
return p;
}
ThreadTree NextNode(ThreadTree p){
if(p->rtag==0)return FirstNode(p->rchild);
return p->rchild;
}
void Inorder(ThreadTree T){
for(ThreadTree p=FirstNode(T);p!=NULL;p=NextNode(p)){
visit(p);
}
}
线索二叉树尤其是中序线索化可以直接查找结点前驱和后继,使得遍历过程
可以不通过堆栈来实现,较一般二叉树的遍历速度更快,并且节约存储空间。