#include <stdio.h>
#include <stdlib.h>
typedef enum {Link, Thread} PointerTag;
typedef struct BiThrNode
{
char data;
struct BiThrNode *lchild, *rchild;
PointerTag ltag;
PointerTag rtag;
} BiThrNode, *BiThrTree;
BiThrTree pre;
//前序遍历 输入
CreateBiThrTree (BiThrTree *T) // 双重指针 (*T)指向二叉树节点 T 指向*T的位置;
{
char c;
scanf ("%c", &c);
if (' ' == c)
{
*T = NULL;
}
else
{
*T = (BiThrNode *)malloc(sizeof(BiThrNode));
(*T)->data = c;
(*T)->ltag = Link;
(*T)->rtag = Link;
CreateBiThrTree (&(*T)->lchild); //双重指针
CreateBiThrTree (&(*T)->rchild);
}
}
//中序遍历线索
InThreading (BiThrTree T)
{
if (T)
{
InThreading (T->lchild); //递归左孩子线索化
if (!T->lchild) //如果该节点没有左孩子, 设置ltag 为thread,并把lchild指向前驱
{
T->ltag = Thread;
T->lchild = pre;
}
if (!pre->rchild)
{
pre->rtag = Thread;
pre->rchild = T;
}
pre = T;
InThreading (T->rchild); // 递归右孩子线索化
}
}
InOrderThreading (BiThrTree *p, BiThrTree T)
{
*p =(BiThrTree) malloc(sizeof(BiThrNode));
(*p)->ltag = Link;
(*p)->rtag = Thread;
(*p)->rchild = *p;
if (!T)
{
(*p)->lchild = *p;
}
else
{
(*p)->lchild = T;
pre = *p;
InThreading(T);
pre->rchild = *p;
pre->rtag = Thread;
(*p)->rchild = pre;
}
}
int main()
{
BiThrTree P, T = NULL; //指针T指向NULL
CreateBiThrTree (&T); //指针T的地址 传入。。
InOrderThreading (&P, T);
}