对称序二叉树的意义就是能够很方便的找出前驱和后继
数据结构
struct ThrTreeNode;
typedef struct ThrTreeNode *PThrTreeNode;
struct ThrTreeNode{
DataType info;
PThrTreeNode llink,rlink;
int ltag,rtag;
};
typedef struct ThrTreeNode *ThrTree;
typedef ThrTree *PThrTree;
建立(画草图跟着走理解起来会更方便)
void thread(ThrTree t)
{
PSeqStack st = createEmptyStack(M);//M一般为T的高度
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); //栈顶弹栈 p一直指向当前被访问的节点
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; //(pr指向被访问点的前驱),指向右子树
}while(!isEmptyStack_seq(st) || p!=NULL)
}