#include<iostream>
using namespace std;
//線索標誌,鏈表標誌
typedef enum Flag
{
Link,
Thread
}TFlag;
//線索二叉樹
typedef struct BiThrNode
{
char data;
struct BiThrNode *LChild,*RChild;
TFlag LFlag,RFlag;
} BiThrTNode,*BiThrTree;
BiThrTree pre;
//中序線索化
void InThreading(BiThrTree p,BiThrTree &pre)
{
if (p)
{
InThreading(p->LChild,pre);
if (!p->LChild)
{
p->LFlag=Thread;
p->LChild=pre;
}
if (!pre->RChild)
{
pre->RFlag=Thread;
pre->RChild=p;
}
pre=p;
InThreading(p->RChild,pre);
}
}
//線索化二叉樹
bool InOrderThreading(BiThrTree &Thrt,BiThrTree T)
{
BiThrTree pre;
if (!(Thrt=(BiThrTree)malloc(sizeof(BiThrTNode))))
return false;
Thrt->LFlag=Link;
Thrt->RFlag=Thread;
Thrt->RChild=Thrt;
if (!T)
Thrt->LChild=Thrt;
else
{
Thrt->LChild=T;
pre=Thrt;
InThreading(T,pre);
pre->RFlag=Thread;
pre->RChild=Thrt;
Thrt->RChild=pre;
}
return true;
}
//中序創建線索二叉樹
bool InOrderCreate(BiThrTree &T)
{
char data;
cin>>data;
if (data=='#')
{
T=NULL;
return false;
}
else
{
if (!(T=(BiThrTree)malloc(sizeof(BiThrTNode))))
{
return false;
}
T->data=data;
if(InOrderCreate(T->LChild))
T->LFlag=Link;
if(InOrderCreate(T->RChild))
T->RFlag=Link;
}
return true;
}
//遍歷線索二叉樹
bool InOrderTraverse_Thr(BiThrTree T)
{
BiThrTree p=T->LChild;
while(p!=T)
{
while(p->LFlag==Link)
p=p->LChild;
printf("%c\n",p->data);
while(p->RFlag==Thread&&p->RChild!=T)
{
p=p->RChild;
printf("%c\n",p->data);
}
p=p->RChild;
}
return true;
}
二叉樹(3)——線索化
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.