#include <iostream.h>
#include <malloc.h>
typedef enum PointerTag {Link,Thread};
typedef int TelemType;
typedef struct BiThrNode
{
TelemType data;
struct BiThrNode *lchild,*rchild;
PointerTag LTag,RTag;
}BiThrNode,*BiThrTree;
bool InitBiThrTree(BiThrTree &T)
{
TelemType s;
cout<<"请输入数据:"<<endl;
cin>>s;
if(s==0) T=NULL;
else
{
if(!(T=(BiThrNode *)malloc(sizeof(BiThrNode)))) return 0;
T->data=s;
T->LTag=Link;
T->RTag=Link;
InitBiThrTree(T->lchild);
InitBiThrTree(T->rchild);
}
return 1;
}
bool InOderTrave(BiThrTree T)
{
if(T== NULL) return 0;
else
{
InOderTrave(T->lchild);
cout<<T->data<<" ";
InOderTrave(T->rchild);
}
return 1;
}
void InThreading(BiThrTree p)
{
BiThrTree pre;
if(p)
{
InThreading(p->lchild);
if(!p->lchild)
{
p->LTag=Thread;
p->lchild=pre;
}
if(pre->rchild)
{
pre->RTag=Thread;
pre->rchild=p;
}
pre=p;
InThreading(p->rchild);
}
}
bool InOrderThreading(BiThrTree &Thrt,BiThrTree T)
{
BiThrTree pre;
if(!(Thrt=(BiThrTree)malloc(sizeof(BiThrNode)))) return 0;
Thrt->LTag=Link;//建立头结点
Thrt->RTag=Thread;
Thrt->rchild=Thrt;//右节点回指
if(!T) Thrt->lchild=Thrt;//若二叉树为空,则左指针回指
else
{
Thrt->lchild=T;
pre=Thrt;
InThreading(T);
pre->rchild=Thrt;//对最后一个节点线索化
pre->RTag=Thread;
Thrt->rchild=pre;
}
return 1;
}
bool InOrderTraverse_Thr(BiThrTree T)
{
BiThrTree p;
p=T->lchild;
while(p!=T)
{
while(p->LTag==Link) p=p->lchild;
cout<<p->data<<" ";
while(p->RTag==Thread && p->rchild!=T)
{
p=p->rchild;
cout<<p->data<<" ";
}
p=p->rchild;
}
return 1;
}
int main()
{
BiThrTree T,Thrt;
InitBiThrTree(T);
cout<<"对二叉树进行中序遍历得到:";
InOderTrave(T);
cout<<endl;
cout<<"对二叉树进行中序线索化。"<<endl;
InOrderThreading(Thrt,T);
cout<<"对二叉树进行中序线索化遍历为:"<<endl;
InOrderTraverse_Thr(Thrt);
cout<<endl;
return 0;
#include <malloc.h>
typedef enum PointerTag {Link,Thread};
typedef int TelemType;
typedef struct BiThrNode
{
TelemType data;
struct BiThrNode *lchild,*rchild;
PointerTag LTag,RTag;
}BiThrNode,*BiThrTree;
bool InitBiThrTree(BiThrTree &T)
{
TelemType s;
cout<<"请输入数据:"<<endl;
cin>>s;
if(s==0) T=NULL;
else
{
if(!(T=(BiThrNode *)malloc(sizeof(BiThrNode)))) return 0;
T->data=s;
T->LTag=Link;
T->RTag=Link;
InitBiThrTree(T->lchild);
InitBiThrTree(T->rchild);
}
return 1;
}
bool InOderTrave(BiThrTree T)
{
if(T== NULL) return 0;
else
{
InOderTrave(T->lchild);
cout<<T->data<<" ";
InOderTrave(T->rchild);
}
return 1;
}
void InThreading(BiThrTree p)
{
BiThrTree pre;
if(p)
{
InThreading(p->lchild);
if(!p->lchild)
{
p->LTag=Thread;
p->lchild=pre;
}
if(pre->rchild)
{
pre->RTag=Thread;
pre->rchild=p;
}
pre=p;
InThreading(p->rchild);
}
}
bool InOrderThreading(BiThrTree &Thrt,BiThrTree T)
{
BiThrTree pre;
if(!(Thrt=(BiThrTree)malloc(sizeof(BiThrNode)))) return 0;
Thrt->LTag=Link;//建立头结点
Thrt->RTag=Thread;
Thrt->rchild=Thrt;//右节点回指
if(!T) Thrt->lchild=Thrt;//若二叉树为空,则左指针回指
else
{
Thrt->lchild=T;
pre=Thrt;
InThreading(T);
pre->rchild=Thrt;//对最后一个节点线索化
pre->RTag=Thread;
Thrt->rchild=pre;
}
return 1;
}
bool InOrderTraverse_Thr(BiThrTree T)
{
BiThrTree p;
p=T->lchild;
while(p!=T)
{
while(p->LTag==Link) p=p->lchild;
cout<<p->data<<" ";
while(p->RTag==Thread && p->rchild!=T)
{
p=p->rchild;
cout<<p->data<<" ";
}
p=p->rchild;
}
return 1;
}
int main()
{
BiThrTree T,Thrt;
InitBiThrTree(T);
cout<<"对二叉树进行中序遍历得到:";
InOderTrave(T);
cout<<endl;
cout<<"对二叉树进行中序线索化。"<<endl;
InOrderThreading(Thrt,T);
cout<<"对二叉树进行中序线索化遍历为:"<<endl;
InOrderTraverse_Thr(Thrt);
cout<<endl;
return 0;
}
算是搞完了吧,这个和二叉树没有什么太大的区别,但是应用方面可能比二叉树要强悍,但是目前我还没用到。