以下是对称序线索二叉树的插入代码,用于插入的二叉树如下图所示:
(其中X、Y结点用于插入,插入位置如图)
#include<iostream>
#define MaxSize 13
using namespace std;
//变量定义与声明
typedef char datatype;
typedef struct node
{
datatype data;
struct node *lchild,*rchild;
int ltag,rtag;
}ThrBTnode,*ThrBinTree;
typedef struct
{
ThrBTnode* S[MaxSize];
int top;
}SeqStack;
SeqStack ST;
ThrBinTree root;
ThrBTnode* nodes[13];
//置空栈
void ClearStack(SeqStack &ST)
{
ST.top=-1;
}
//栈的推入
void push(SeqStack &ST,ThrBTnode *p)
{
if(ST.top>=MaxSize-1)
cout<<"overflow!"<<endl;
else
ST.S[++ST.top]=p;
}
//栈的弹出
ThrBTnode* pop(SeqStack &ST,ThrBTnode *p)
{
if(ST.top==-1)
cout<<"underflow!"<<endl;
else
p=ST.S[ST.top--];
return p;
}
//判栈空否
int StackEmpty(SeqStack ST)
{
if(ST.top==-1)
return 1;
else
return 0;
}
//构建一棵二叉树
ThrBinTree Create(ThrBinTree root)
{
ThrBTnode *A=new ThrBTnode;
ThrBTnode *B=new ThrBTnode;
ThrBTnode *C=new ThrBTnode;
ThrBTnode *D=new ThrBTnode;
ThrBTnode *E=new ThrBTnode;
ThrBTnode *F=new ThrBTnode;
ThrBTnode *G=new ThrBTnode;
ThrBTnode *H=new ThrBTnode;
ThrBTnode *I=new ThrBTnode;
ThrBTnode *J=new ThrBTnode;
ThrBTnode *K=new ThrBTnode;
ThrBTnode *X=new ThrBTnode;
ThrBTnode *Y=new ThrBTnode;
A->data='A';
B->data='B';
C->data='C';
D->data='D';
E->data='E';
F->data='F';
G->data='G';
H->data='H';
I->data='I';
J->data='J';
K->data='K';
X->data='X';
Y->data='Y';
A->lchild=B;
A->rchild=C;
A->ltag=A->rtag=0;
B->lchild=D;
B->rchild=E;
B->ltag=B->rtag=0;
C->lchild=F;
C->rchild=G;
C->ltag=C->rtag=0;
D->lchild=D->rchild=NULL;
D->ltag=D->rtag=0;
E->lchild=H;
E->rchild=NULL;
E->ltag=E->rtag=0;
F->lchild=NULL;
F->rchild=I;
F->ltag=F->rtag=0;
G->lchild=G->rchild=NULL;
G->ltag=G->rtag=0;
H->lchild=H->rchild=NULL;
H->ltag=H->rtag=0;
I->lchild=J;
I->rchild=K;
I->ltag=I->rtag=0;
J->lchild=J->rchild=NULL;
J->ltag=J->rtag=0;
K->lchild=K->rchild=NULL;
K->ltag=K->rtag=0;
X->lchild=X->rchild=NULL;
X->ltag=X->rtag=0;
Y->lchild=Y->rchild=NULL;
Y->ltag=Y->rtag=0;
root=A;
nodes[0]=A;
nodes[1]=B;
nodes[2]=C;
nodes[3]=D;
nodes[4]=E;
nodes[5]=F;
nodes[6]=G;
nodes[7]=H;
nodes[8]=I;
nodes[9]=J;
nodes[10]=K;
nodes[11]=X;
nodes[12]=Y;
return root;
}
//线索化二叉树
void inOrderThreadTree(ThrBinTree root)
{
ThrBTnode *pre,*p;
ClearStack(ST);
p=root;
pre=NULL;
while(p!=NULL||!StackEmpty(ST))
{
if(p!=NULL)
{
push(ST,p);
p=p->lchild;
}
else
{
p=pop(ST,p);
if(p->lchild==NULL)
{
p->lchild=pre;
p->ltag=1;
}
if(pre!=NULL&&pre->rchild==NULL)
{
pre->rchild=p;
pre->rtag=1;
}
pre=p;
p=p->rchild;
}
}
if(pre!=NULL)
pre->rtag=1;
}
//对称序遍历对称序线索树
void inOrderTraThrTree(ThrBinTree root)
{
ThrBTnode *p=root;
while(p!=NULL)
{
while(p->ltag==0)
p=p->lchild;
cout<<p->data<<" ";
while(p->rtag==1&&p->rchild!=NULL)
{
p=p->rchild;
cout<<p->data<<" ";
}
p=p->rchild;
}
cout<<endl;
}
//线索二叉树的插入
void InsertRight(ThrBTnode *p,ThrBTnode *r)
{
ThrBTnode *q;
r->rchild=p->rchild;
r->rtag=p->rtag;
r->lchild=p;
r->ltag=1;
p->rchild=r;
p->rtag=0;
if(r->rtag==0)
{
q=r->rchild;
while(q->ltag==0)
q=q->lchild;
q->lchild=r;
}
}
//逐个结点打印线索关系
void Print(ThrBinTree root)
{
for(int i=0;i<13;i++)
{
cout<<"对于结点"<<nodes[i]->data<<"有:"<<endl;
if((nodes[i]->lchild==NULL)||(nodes[i]->rchild==NULL))
{
if(nodes[i]->lchild==NULL&&nodes[i]->rchild==NULL)
{
cout<<"lchild为: NULL"<<endl;
cout<<"rchild为: NULL"<<endl;
}
else if(nodes[i]->rchild==NULL&&nodes[i]->lchild!=NULL)
{
cout<<"lchild为: "<<nodes[i]->lchild->data<<endl;
cout<<"rchild为: NULL"<<endl;
}
else if(nodes[i]->lchild==NULL&&nodes[i]->rchild!=NULL)
{
cout<<"lchild为: NULL"<<endl;
cout<<"rchild为: "<<nodes[i]->rchild->data<<endl;
}
cout<<"ltag为: "<<nodes[i]->ltag<<endl;
cout<<"rtag为: "<<nodes[i]->rtag<<endl;
continue;
}
cout<<"lchild为: "<<nodes[i]->lchild->data<<endl;
cout<<"rchild为: "<<nodes[i]->rchild->data<<endl;
cout<<"ltag为: "<<nodes[i]->ltag<<endl;
cout<<"rtag为: "<<nodes[i]->rtag<<endl;
}
}
//测试函数
int main()
{
root=Create(root);
cout<<"对称序线索化二叉树结果为:"<<endl;
inOrderThreadTree(root);
Print(root);
cout<<"对称序遍历对称序线索树结果为:"<<endl;
inOrderTraThrTree(root);
cout<<"插入X结点后,对称序线索化结果为:"<<endl;
InsertRight(nodes[4],nodes[11]);
Print(root);
cout<<"对称序遍历对称序线索树结果为:"<<endl;
inOrderTraThrTree(root);
cout<<"插入Y结点后,对称序线索化结果为:"<<endl;
InsertRight(nodes[5],nodes[12]);
Print(root);
cout<<"对称序遍历对称序线索树结果为:"<<endl;
inOrderTraThrTree(root);
return 0;
}