主题:关于二叉树的初级问题,求助!!!
#define null 0
#define error -1
#define ok 1
struct bitnode
{
char data;
struct bitnode *lchild;
struct bitnode *rchild;
};
createtree(struct bitnode *r) /*建立二叉树*/
{
char ch;
scanf("%c",&ch);
if(ch=='#') r=null;
else
{
if(!(r=(struct bitnode *) malloc(sizeof(struct bitnode)))) return error;
{
r->data=ch;
createtree(r->lchild);
createtree(r->rchild);
}
}
}
display(struct bitnode *r)
{
if(r!=null)
{ printf("%c",r->data);
if(r->lchild!=null||r->rchild!=null)
{ printf("(");
printf(r->lchild);
if(r->rchild!=null)printf(",");
printf(r->rchild);
printf(")");
}
}
}
main()
{
struct bitnode *ptree;
ptree=(struct bitnode *) malloc (sizeof(struct bitnode));
printf("Input the char:\n");
createtree(ptree);
display(ptree);
}
以上是我写的用链表实现的创建二叉树和输出二叉树的程序,可是为什么输出总是出错呢?急
#define error -1
#define ok 1
struct bitnode
{
char data;
struct bitnode *lchild;
struct bitnode *rchild;
};
createtree(struct bitnode *r) /*建立二叉树*/
{
char ch;
scanf("%c",&ch);
if(ch=='#') r=null;
else
{
if(!(r=(struct bitnode *) malloc(sizeof(struct bitnode)))) return error;
{
r->data=ch;
createtree(r->lchild);
createtree(r->rchild);
}
}
}
display(struct bitnode *r)
{
if(r!=null)
{ printf("%c",r->data);
if(r->lchild!=null||r->rchild!=null)
{ printf("(");
printf(r->lchild);
if(r->rchild!=null)printf(",");
printf(r->rchild);
printf(")");
}
}
}
main()
{
struct bitnode *ptree;
ptree=(struct bitnode *) malloc (sizeof(struct bitnode));
printf("Input the char:\n");
createtree(ptree);
display(ptree);
}
以上是我写的用链表实现的创建二叉树和输出二叉树的程序,可是为什么输出总是出错呢?急