//带头结点的中序线索化
#include<iostream>
using namespace std;
typedef struct BiThrNode{
char data;
struct BiThrNode *lchild,*rchild;
int LTag,RTag;
}BiThrNode,*BiThrTree;
BiThrNode *pre=new BiThrNode;
void CreateBiTree(BiThrTree &T){
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else{
T=newBiThrNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void InThreading(BiThrTree p){
if(p){
InThreading(p->lchild);
if(!p->lchild){
p->LTag=1;
p->lchild=pre;
}else{
p->LTag=0;
}
if(!pre->rchild){
pre->RTag=1;
pre->rchild=p;
}else{
pre->RTag=0;
}
pre=p;
InThreading(p->rchild);
}
}
void InOrderThreading(BiThrTree &Thrt,BiThrTree T){
Thrt=new BiThrNode;
Thrt->LTag=0;
Thrt->RTag=1;
Thrt->rchild=Thrt;
if(!T) Thrt->lchild=Thrt;
else{
Thrt->lchild=T;pre=Thrt;
InThreading(T);
pre->rchild=Thrt;
pre->RTag=1;
Thrt->rchild=pre;
}
}
void main(){
pre->RTag=1;
pre->rchild=NULL;
BiThrTree tree,Thrt;
cout<<"please:\n";
CreateBiTree(tree);
InThreading(Thrt,tree);
cout<<"finish:\n";
}
原文:https://www.cnblogs.com/ygjzs/p/11874608.html