二叉树的创建和遍历,又是内存操作错误,求指教。。。
程序代码:
#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; 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); } }
编译通过,输入序列为1 2 3 0 0 4 5 0 6 0 0 7 0 0 0 输入的0意味空子树,调用遍历函数的时候提示内存错误。。。求指点啊