线索二叉树的一些基本操作
一 利用中序递归遍历发来建立线索二叉树
算法描述:
typedef struct Bnode
{
short int Ltag,Rtag;
datatype data;
struct Bnode* LChild,*Child;
}BTnode,*BTptr
BTptr pre=NULL
void Initthreadbt(BTptr BT)
{
if(BT)
{
Initthreadbt(Bt->LChild);
if(Bt->LChild==NULL)
{
Bt->Ltag=1;
BT->LChild=pre;
}
if(Bt->RChild==NULL)
{
Bt->Rtag=1;
}
if(pre&&pre->Rtag==1)
{
pre->RChild=BT;
}
pre=BT;
Initthreadbt(BT->RChild);
}
}
二 用中序二叉树找出节点的前驱和后缀
BTptr Inpre(BTptr p) //求前驱的算法
{
BTptr pre;
if(P==NULL)
{
return NULL;
}
else
{
pre=p->LChild;
if(p->Ltag==0) //当有左子树时
{
while(pre->Rtag==0)
{
pre->RChild; //去该树最右边的节点
}
}
}
return pre;
}
BTptr Insucc(BTptr p) //求后缀的算法
{
BTptr s;
if(p==NULL)
{
return NULL;
}
s=p->RChild
if(p->Rtag==0) //右节点存在时
{
while(p->Ltag==0)
{
s=p->LChild;
}
}
return s;
}
线索二叉树的遍历
void Tinorder(BTptr BT)
{
BTptr p;
p=BT;
while(p->Ltag==0)
{
p=p->LChild; //找到中序中第一个节点
}
while(p)
{
Visit(p);
p=Insucc(p); //依次访问第一个节点的后缀
}
}
浙公网安备 33010602011771号