主题:[原创]大家帮忙看一下我这个程序!!!!!!!!!!1
大家帮忙看一下啊,我这个程序是二叉树的建立与遍厉的,程序调试通过但最终是个死循环或者得不到正确的结果.我的树的建立的算法是没问题的,我单独的试过.能得到正确的结果.我也不知道怎么了啊
#include"stdio.h"
typedef struct btnode{
char data;
struct btnode *lchild;
struct btnode *rchild;
}sjj; int number;
void preorder(sjj *bt)
{if(bt!=0)
{ if(bt->data!='0') printf("%c",bt->data);
preorder(bt->lchild);
preorder(bt->rchild);}
}
void inorder(sjj *bt)
{if(bt!=0)
{inorder(bt->lchild);
if(bt->data!='0')printf("%c",bt->data);
inorder(bt->rchild);}
}
void postorder(sjj *bt)
{if(bt!=0)
{postorder(bt->lchild);
postorder(bt->rchild);
if(bt->data!='0')printf("%c",bt->data);
}
}
creattree(sjj *l)
{sjj *p,*q,*(a[50]); int i=2;
while(1)
{
p=(sjj*)malloc(sizeof(sjj));
q=(sjj*)malloc(sizeof(sjj));
printf("input left child:");
scanf("%c",&p->data); fflush(stdin);
l->lchild=p;
a[i++]=p;
if(i>number) break;
printf("input right child:");
scanf("%c",&q->data); fflush(stdin);
l->rchild=q;
a[i++]=q;
if(i>number) break;
l= a[i/2];
}
}
main()
{ sjj *l,*p,*a,*b;
l=(sjj*)malloc(sizeof(sjj)); p=l; printf("please input the first data\n");
scanf("%c",&l->data);
printf("the number ?\n");
scanf("%d",&number); fflush(stdin);
creattree(l);
preorder(p);
printf("\n");
inorder(p);
printf("\n");
postorder(p);
}
[em18]
#include"stdio.h"
typedef struct btnode{
char data;
struct btnode *lchild;
struct btnode *rchild;
}sjj; int number;
void preorder(sjj *bt)
{if(bt!=0)
{ if(bt->data!='0') printf("%c",bt->data);
preorder(bt->lchild);
preorder(bt->rchild);}
}
void inorder(sjj *bt)
{if(bt!=0)
{inorder(bt->lchild);
if(bt->data!='0')printf("%c",bt->data);
inorder(bt->rchild);}
}
void postorder(sjj *bt)
{if(bt!=0)
{postorder(bt->lchild);
postorder(bt->rchild);
if(bt->data!='0')printf("%c",bt->data);
}
}
creattree(sjj *l)
{sjj *p,*q,*(a[50]); int i=2;
while(1)
{
p=(sjj*)malloc(sizeof(sjj));
q=(sjj*)malloc(sizeof(sjj));
printf("input left child:");
scanf("%c",&p->data); fflush(stdin);
l->lchild=p;
a[i++]=p;
if(i>number) break;
printf("input right child:");
scanf("%c",&q->data); fflush(stdin);
l->rchild=q;
a[i++]=q;
if(i>number) break;
l= a[i/2];
}
}
main()
{ sjj *l,*p,*a,*b;
l=(sjj*)malloc(sizeof(sjj)); p=l; printf("please input the first data\n");
scanf("%c",&l->data);
printf("the number ?\n");
scanf("%d",&number); fflush(stdin);
creattree(l);
preorder(p);
printf("\n");
inorder(p);
printf("\n");
postorder(p);
}
[em18]