二叉树的先序递归建立,中序线索化,中序遍历
#include
#include
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define NULL 0
#define OVERFLOW -1typedef char TElemType;typedef int Status;typedef enum PointerTag{Link,Thread};
typedef struct BiThrNode {
TElemType data;
struct BiThrNode *lchild, * rchild;
PointerTag LTag,RTag;
}BiThrNode, * BiThrTree;BiThrTree pre;
Status CreatBiThrTree(BiThrTree &T)//递递递的方法先序构造二叉//{
char ch;
scanf("%c",&ch);
if (ch==' ')
T=NULL;
else {
if (!(T=(BiThrNode *)malloc(sizeof(BiThrNode))))
return ERROR;
T->data=ch; // 生成根点递递
CreatBiThrTree(T->lchild); // 构造左子递
CreatBiThrTree(T->rchild); // 构造右子递
}
return OK;
} // CreateBiThrTreevoid InThreading(BiThrTree p)//将点中序索化递递递递递递递//{
if(p)
{
InThreading(p->lchild);
if(!p->lchild)
{
p->LTag=Thread;
p->lchild=pre;
}
else
{
p->LTag=Link;
}
if(!pre->rchild)
{
pre->RTag=Thread;
pre->rchild=p;
}
else
{
pre->RTag=Link;
}
pre=p;
InThreading(p->rchild);
}
}
Status InOrderThreading(BiThrTree &Thrt,BiThrTree T)//中序遍二叉,并将其中序索化递递递递递递递递递递递递递
//
{
if(!(Thrt=(BiThrTree)malloc(sizeof(BiThrNode))))
exit(OVERFLOW);
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 OK;
}
Status visit(TElemType e){
printf("%c\t",e);
return OK;
}
Status InOrderTraverse_Thr(BiThrTree T,Status ( * visit)(TElemType e))//中序遍二叉递递递递
的非算法递递递递//
{
BiThrTree p;
p=T->lchild;
while(p!=T)
{
while(p->LTag==Link)
p=p->lchild;
if(!visit(p->data))
return ERROR;
while(p->RTag==Thread&&p->rchild!=T)
{
p=p->rchild;
visit(p->data);
}
p=p->rchild;
}
return OK;
}
void main()
{
BiThrTree T,Thrt;
Thrt=(BiThrTree)malloc(sizeof(BiThrNode));
printf("按先序次序入二叉中点的,一个字符,,空格
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
示字符递递递递递递递递递递递递递递递递递递递递递递递\n");
CreatBiThrTree(T);
printf("下面将此二叉中序索化递递递递递递\n");
InOrderThreading(Thrt,T);
printf("下面中序遍索二叉递递递递递递递\n");
InOrderTraverse_Thr(Thrt,visit);}