我想实现的是 输出中序遍历时的第一个结点,但是我这个代码无论怎么改一直输出的是根节点,实在不知道怎么改了
#include
#include
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
int ltag,rtag;
}BiThrNode,*ThreadTree;
void InThread(ThreadTree p,ThreadTree &pre)
{
if(p!=NULL)
{
InThread(p->lchild,pre);
if(p->lchild==NULL)
{
p->lchild=pre;
p->ltag=1;
}
if(pre!=NULL&&pre->rchild==NULL)
{
pre->rchild=p;
pre->rtag=1;
}
pre=p;
InThread(p->rchild,pre);
}
}
void CreatInThread(ThreadTree &T)
{
ThreadTree pre=NULL;
if(T!=NULL)
{
InThread(T,pre);
if(pre->rchild==NULL)
pre->rtag=1;
}
}
void CreatTree(ThreadTree &T)
{
char ch;
scanf("%c",&ch);
if(ch=='#') T=NULL;
else
{
T=(BiThrNode *)malloc(sizeof(BiThrNode));
CreatTree(T->lchild);
T->data=ch;
CreatTree(T->rchild);
}
}
//求中序线索二叉树中中序序列下的第一个结点
/*void FirstNode(ThreadTree T)
{
if(T!=NULL)
{
FirstNode(T->lchild);
if(T->lchild==NULL)
printf("第一个节点为:%c",T->data);
}
}*/
BiThrNode *FirstNode(BiThrNode *p)
{
while(p->ltag==0)
{
p=p->lchild;
}
return p;
}
BiThrNode *NextNode(BiThrNode *p)
{
if(p->rtag==0)
return FirstNode(p->rchild);
else return(p->rchild);
}
void Inorder(BiThrNode *T)
{
for(BiThrNode *p=FirstNode(T);p!=NULL;p=NextNode(p))
printf("%c",p->data);
}
int main()
{
BiThrNode * T;
CreatTree(T);
CreatInThread(T);
Inorder(T);
return 0;
}
可以提供一些你的输入和输出的数据么?