#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct Node
{
int Ltag,Rtag;
ElemType data;
Node *Lchild,*Rchild;
} BiTNode,*BiTree;
BiTNode *pre=NULL;
BiTree CreateBiTree();//建立二叉树
void Inthread(BiTree T);//建立中序线索树
int main(void)
{
BiTree root=CreateBiTree();
Inthread(root);//中序
return 0;
}
BiTree CreateBiTree()//建立二叉树
{
ElemType x;
BiTree T;
scanf("%c",&x);
if(x=='#')
T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
T->data=x;
T->Lchild=CreateBiTree();
T->Rchild=CreateBiTree();
}
return T;
}
void Inthread(BiTree T)//建立中序线索树
{
if(T!=NULL)
{
Inthread(T->Lchild);
if(T->Lchild==NULL)
{
T->Ltag=1;
T->Lchild=pre;
}
if(pre!=NULL&&pre->Rchild==NULL)
{
pre->Rchild=T;
pre->Rtag=1;
}
pre=T;
Inthread(T->Rchild);
}
}
建立中序线索树
最新推荐文章于 2023-05-01 17:34:47 发布