C语言 中序递归线索化 为什么下面这个执行到一半就停止运行

#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)
{
 if(p)
 {
  inthreading(p->lc,pre);
  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;
}

//inorderthreading 函数中对应代码改下 
struct tree * 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 thrt;
}

//main 函数改一下
void main()
{
 struct tree *T,*head;
 T=creat();
 head = inorderthreading(T);
 print(head);
 free(T);
 free(head);
 }

测试可以通过,希望能帮助到你

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答