#include <stdio.h>#include <stdlib.h>struct tree{ int data; struct tree * lc; struct tree * rc; int ltag; int rtag;};struct tree * creat(){ char ch; struct tree *T; printf("请输入:\n"); scanf(" %c",&ch); if(ch=='#') T=NULL; else { if(!(T=(struct tree*)malloc(sizeof(struct tree)))) exit(1); T->data =ch; T->lc =creat(); T->rc =creat(); } return T;}void inthreading (struct tree *p,struct tree *pre){ if(p) { inthreading(p->lc,pre->lc ); if(p->lc) p->ltag=0; else if(!p->lc ) { p->ltag =1; p->lc =pre; printf("啊\n");
} if(!pre->rc) { pre->rtag=1; pre->rc =p; printf("耶"); } pre=p; printf("运行到这里了卧槽"); inthreading(p->rc,pre ); } return;}
void inorderthreading(struct tree *T){ struct tree *thrt; struct tree *pre; if(!(thrt=(struct tree *)malloc(sizeof(struct tree)))) exit(1); thrt->data =22; thrt->ltag =0; thrt->rtag =1; thrt->rc =thrt; if(!T)thrt->lc =thrt; else { thrt->lc =T; pre=thrt; inthreading(T,pre); pre->rc =thrt; pre->rtag =1; thrt->rc =pre; } return;}
void print(struct tree*T){ struct tree*p=T->lc ; while(p!=T) { while(p->ltag ==0) p=p->lc ; if(p->data) printf("%c\n",p->data ); while(p->rtag ==1&&p->rc !=T) { p=p->rc; printf("%c\n",p->data ); } p=p->rc ; } return;}void main(){ struct tree *T; T=creat(); inorderthreading(T); print(T); free(T);
}
最近也在看二叉树的线索化
void inthreading (struct tree *p,struct tree **pre)测试可以通过,希望能帮助到你