#include<stdio.h>#include<stdlib.h>typedef enum{link,thread}PionerTag;typedef struct BiThrNode{ char data;//数据域 struct BiThrNode*L,*R;//左右孩子指针域 PionerTag Ltag,Rtag;}BiThrNode,*BiThrTree;BiThrTree pre = NULL;//定义一个指向线索树所指位置前一个位子的指针域void PreCreatBiTree(BiThrTree *t);//前序创建二叉树void MidThreading(BiThrTree p);//中序二叉树线索化void MidThrShow(BiThrTree t);//中序遍历线索二叉树int main(){ BiThrTree t; printf("输入任意字符,输入#为空\n"); PreCreatBiTree(&t); printf("\n"); MidThreading(t); printf("中序遍历线索二叉树:\n"); MidThrShow(t); printf("\n");}void PreCreatBiTree(BiThrTree *t){ char ch; scanf("%c",&ch); if(ch!='#') { if(!((*t) = (BiThrTree)malloc(sizeof(BiThrNode)))) { printf("申请空间失败,程序运行失败:\n"); return; } else { (*t)->data = ch; PreCreatBiTree(&(*t)->L); PreCreatBiTree(&(*t)->R); } } else { *t = NULL; }}void MidThreading(BiThrTree p){ if(p)//如果结点p存在 { MidThreading(p->L);//递归遍历左孩子,直到左孩子不存在 if(!p->L) { p ->Ltag = thread;//将当前结点标志域设为thread即1 p ->L = pre;//将当前左孩子指针域指向上逻辑上访问的前一个结点 } else p->Ltag = link; if(pre&&!pre->R)//逻辑上访问的前一个结点存在,并且逻辑上访问前一个结点不存在右孩子 { pre ->Rtag = thread;//逻辑上访问的前一个结点的标志域设为thread即1 pre ->R = p;//逻辑上访问的前一个结点的右孩子指向逻辑上下一个访问的结点 } else p->Rtag = link; pre = p;//逻辑上访问的前一个结点指向逻辑上访问的下一个结点 MidThreading(p ->R);//依次递归遍历当前结点的右孩子 }}void MidThrShow(BiThrTree t){ while(t) { while(t->Ltag==link) { t = t->L; } printf("%c",t->data); while(t->Rtag==thread&&t->R!=NULL ) { t = t->R; printf("%c",t->data); } t = t->R; }}