#include
#include
#define max 100
typedef struct node
{
char date;
struct node lchild,*rchild;
}tree,*TREE;
tree *DLR=NULL;
void creatDLR()
{
//先序递归方式创建树
char date;
date=getchar();
if(date=='#')DLR=NULL;
else
{
if(!(DLR=(tree)malloc(sizeof(struct node))))
{printf("先序递归创建树失败");exit(1);}
DLR->date=date;
creatDLR(DLR->lchild);
creatDLR(DLR->rchild);
}
}
void showDLR()
{
//先序递归遍历二叉树
if(DLR)
{
printf("%c",DLR->date);
showDLR(DLR->lchild);
showDLR(DLR->rchild);
}
}
int main()
{
creatDLR();
showDLR();
}
这是我的源码,我的想法是这样的。先利用creatDLR()函数把创建树后的根节点存储在全局变量,再利用showDLR()通过全局变量遍历树。可是为什么不行呢?
creatDLR 的定义没有参数,为什么调用的时候却有一个参数呢?这样编译应该无法通过的。
好多问题,if(!(DLR=(tree)malloc(sizeof(struct node))))这句不对,==》if(!(DLR=(tree *)malloc(sizeof(struct node))))