#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef enum{Link,Thread} PointerTag;
typedef struct BiThrNode{
ElemType data;
PointerTag ltag,rtag;
struct BiThrNode *lchild,*rchild;
}BiThrNode,*BiThrTree;
BiThrTree pre;
void CreateBiThrTree(BiThrTree *T){
char c;
scanf("%c",&c);
if(c==' '){
*T=NULL;
}
else{
*T=(BiThrTree *)malloc(sizeof(BiThrNode));
(*T)->data=c;
(*T)->ltag=Link;
(*T)->rtag=Link;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
//线索化
void InThreading(BiThrTree T){
if(T){
InThreading(T->lchild);
if(T->lchild==NULL){
T->ltag=Thread;
T->lchild=pre;
}
if(!pre->rchild){
pre->rtag=Thread;
pre->rchild=T;
}
pre=T;
InThreading(T->rchild);
}
}
void InOrderThreading(BiThrTree *p,BiThrTree T){
*p=(BiThrTree)malloc(sizeof(BiThrNode));
(*p)->ltag=Link;
(*p)->rtag=Thread;
if(!T){
(*p)->lchild=*p;
}
else{
(*p)->lchild=T;
pre=*p;
InThreading(T);
pre->rchild=p;
pre->rtag=Thread;
(*p)->rchild=pre;
}
}
void InOrderTraverse(BiThrTree T){
BiThrTree p;
p=T->lchild;
while(p!=T){
while(p->ltag==Link){
p=p->lchild;
}
printf("%c",p->data);
while(p->rtag==Thread && p->rchild!=T){
p=p->rchild;
printf("%c",p->data);
}
p=p->rchild;
}
}
int main(){
BiThrTree P,T=NULL;
CreateBiThrTree(&T);
InOrderThreading(&P,T);
InOrderTraverse(P);
return 0;
}
08-23
1227
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-29
455
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-29
584
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)