二叉树的初始化和遍历,内存操作错误,求指教!!!
#include<stdio.h>
#include<stdlib.h>
typedef int Telemtype;
typedef struct bitnode
{
Telemtype data;
struct bitnode *lchild,*rchild,*parent;
}bitnode,*bitree;
bitree creat_tree(bitree tree,bitree parent);
void pre_order(bitree tree);
int main(void)
{
bitree root;
root=(bitree)malloc(sizeof(bitnode));
root->parent=NULL;
root=creat_tree(root,root->parent);
pre_order(root);
getchar();
return 0;
}
bitree creat_tree(bitree tree,bitree parent)
{
Telemtype temp;
printf("input the data: \n");
scanf("%d",&temp);
if(temp==0)
{
tree=NULL;
return NULL;
}
else
{
if(!(tree=(bitree)malloc(sizeof(bitnode))))
exit(-1);
tree->data=temp;
tree->parent=parent;
tree->lchild=creat_tree(tree->lchild,tree);
tree->rchild=creat_tree(tree->rchild,tree);
}
return tree;
}
void pre_order(bitree tree)
{
if(tree)
{
printf("%d parent:%d \n",tree->data,tree->parent->data);
pre_order(tree->lchild);
pre_order(tree->rchild);
}
}