typedef struct ThreadNode{
ElemType 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&&p->rchild==NULL){
pre->rchild=p;
pre->rtag=1;
}
pre=p;
InThread(p->rchild,pre); //线索化右子树
}
}
void Inorder(ThreadNode *T){ //不含头节点的中序线索二叉树的中序遍历算法
for(ThreadNode *p=Firstnode(T),p!=NULL;p=Nextnode(p)) visit(p);
}
ThreadNode *Firstnode(ThreadNode *p){ //找到遍历的第一个节点,最左下节点
while(p->ltag==0) p=p->lchild;
return p;
}
ThreadNode *Nextnode(ThreadNode *p){ //找到下一个要遍历的节点
if(p->rtag==0) return Firstnode(p->rchild);
else return p->rchild;
}