线索二叉树就是在遍历二叉链表的过程中添加各种判断,修改空指针,使之成为一个线索链表。
而线索二叉树的数据结构本身就和二叉树有着本质的区别
typedef emun flag {child,thread}
typedef struct BinThrNode{
TElemtype data;
BinThrNode *lchild,*rchild;
flag LTag,RTag;
}BinThrNode,*BinThrTree;
在线索过程中,需要在二叉树的线索链表上添加一个头结点,它的data域为空;令其lchild域的指针指向二叉树的根节点;rchild域的指针指向中序遍历时访问的最后一个节点。
指针p指向当前访问的节点,且p在递归的过程中不断变化
利用pre指针存储刚刚访问过的节点
thrt其实只在开头和结尾有点用。。。
Status InOrderThreading(BinThrTree &Thrt,BinThrTree T){
if(!(Thrt=(BinThrTree)malloc(sizeof(BinThrNode)))) exit(OVERFLOW);
Thrt->LTag=Child;
Thrt->RTag=Thread;
Thrt->rchild=Thrd; //右指针回指
if(!T) Thrt->lchild=Thrt; //若二叉树空,则左指针回指
else{
Thrt->lchild=T; pre=Thrt;
InThreading(T);
pre->rchild=Thrt; pre->RTag=Thread; //最后一个结点线索化
Thrt->rchild=pre;
}
}
void InThreading(BinThrTree){
if(p){
InThreading(p->lchild); //左子树线索化
if(!p->lchild) { p->LTag=Thread; p->lchild=pre; } // 前趋线索
if(!pre->rchild) {pre->RTag=Thread; pre->rchild=p; } // 后继线索
pre=p; // 保持pre指向p的前趋
InThreading(p->rchild); // 右子树线索化
}
}