程序代码:
#include "stdio.h"
#include "stdlib.h"
typedef enum piontertag{link,thread}Piontertag;
typedef struct bithrnode
{char data;
Piontertag ltag,rtag;
struct bithrnode *lchild,*rchild;
}BithrNODE;
BithrNODE *BithrCreat();
BithrNODE *InOrderThreading(BithrNODE *);
void InThreading(BithrNODE *);
void InOrderTraverse(BithrNODE *);
BithrNODE *pre;
int main(void)
{
BithrNODE *a=BithrCreat();
a=InOrderThreading(a);
InOrderTraverse(a);
return 0;
}
BithrNODE *BithrCreat()
{
char x;
scanf("%c%*c",&x);
if(x=='#')
return NULL;
BithrNODE *p=(BithrNODE *)malloc(sizeof(BithrNODE ));
p->data=x;
p->rtag=p->ltag=link;
p->lchild=BithrCreat();
p->rchild=BithrCreat();
return p;
}
BithrNODE*InOrderThreading(BithrNODE *a)
{
BithrNODE *h=(BithrNODE *)malloc(sizeof(BithrNODE ));
h->ltag=link;
h->rtag=thread;
h->rchild=h;
if(!a)
h->lchild=h;
else
{
h->lchild=a;
pre=h;
InThreading(a);
pre->rchild=h;
pre->rtag=thread;
h->rchild=pre;
}
return h;
}
void InThreading(BithrNODE *a)
{
if(a)
{
if(!a->lchild)
{
a->ltag=thread;
a->lchild=pre;
}
if(!pre->rchild)
{
pre->rtag=thread;
pre->rchild=a;
}
pre=a;
if(a->ltag==link)
InThreading(a->lchild);
InThreading(a->rchild);
}
}
void InOrderTraverse(BithrNODE *a)
{
BithrNODE *p=a->lchild;
while(p!=a)
{
while(p->ltag==link&&p->rchild!=a)
{
printf("%c ",p->data);
p=p->lchild;
}
printf("%c ",p->data);
p=p->rchild;
}
先序线索化二叉树 然后先序输出
输入两组数据 一组是ABC##D##E#F###
第一组是 ABC##D##E#F##
为什么第一组不输出
第二组却能正确输出呢