/*
  先序线索化
*/
#include<stdio.h> #include<stdlib.h> //建立线索二叉树存储结构 typedef struct ThreadNode{ int data; //数据元素 struct ThreadNode *lchild,*rchild; //左右子树 int ltag,rtag; //左右线索标志 }ThreadNode,*ThreadTree; //先序线索化二叉树 void PreThread(ThreadTree &p,ThreadTree &pre){ if(p!=NULL){ /*visit()*/ if(p->lchild==NULL){ //左子树为空,建立前驱线索 p->lchild=pre; p->ltag=1; } if(pre!=NULL&&pre->rchild==NULL){ pre->rchild=p; //右子树为空,建立后继线索 pre->rtag=1; } pre=p; if(p->ltag==0){ PreThread(p->lchild,pre); //递归线索化左子树 } PreThread(p->rchild,pre); //递归线索化右子树 } } //建立线索化二叉树 void CreatePreThread(ThreadTree T){ ThreadTree pre=NULL; if(T!=NULL){ //非空二叉树——线索化 PreThread(T,pre); //线索化二叉树 pre->rchild=NULL; //处理遍历的最后一个结点 pre->rtag=1; } } int main(){ }

 

/*
    中序线索化
*/

#include<stdio.h>
#include<stdlib.h>

//建立线索二叉树存储结构
typedef struct ThreadNode{
    int data;                            //数据元素
    struct ThreadNode *lchild,*rchild;    //左右子树
    int ltag,rtag;                        //左右线索标志
}ThreadNode,*ThreadTree;

//线索化二叉树
void InitThread(ThreadTree &p,ThreadTree &pre){
    if(p!=NULL){
        InitThread(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;
        InitThread(p->rchild,pre);
    }
}
//建立线索化二叉树
void CreateInitThread(ThreadTree T){
    ThreadTree pre=NULL;
    if(T!=NULL){
        InitThread(T,pre);
        pre->rchild=NULL;
        pre->rtag=1;
    }
}

int main(){
    
}

 

/*
    后序线索化
*/

#include<stdio.h>
#include<stdlib.h>

//建立线索二叉树存储结构
typedef struct ThreadNode{
    int data;                            //数据元素
    struct ThreadNode *lchild,*rchild;    //左右子树
    int ltag,rtag;                        //左右线索标志
}ThreadNode,*ThreadTree;

//后序线索化二叉树
void PostThread(ThreadTree &p,ThreadTree &pre){
    if(p!=NULL){
        PostThread(p->lchild,pre);                //递归线索化左子树
        PostThread(p->rchild,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;
    }
}
//建立线索化二叉树
void CreatePostThread(ThreadTree T){
    ThreadTree pre=NULL;
    if(T!=NULL){                                //非空二叉树——线索化
        PostThread(T,pre);                        //线索化二叉树
        pre->rchild=NULL;                        //处理遍历的最后一个结点
        pre->rtag=1;
    }
}

int main(){
    
}