SOS!二叉树的递归实现,求大家看看,指针错在哪里
#include <stdio.h>#include <stdlib.h>
#define FORMAT "%c"
/****************************************************************/
typedef char TElemType;
typedef struct BiNode{
TElemType data;
struct BiNode *lchild,*rchild;
}BiTree;
/*************************************************************/
/***************************************************************/
int CreatBiTree(BiTree **Tree);
int PreOrderTraverse(BiTree **Tree,int (* Visit)(TElemType e));
int PrintElement(TElemType e);
/**************************************************************/
int main(void)
{
BiTree *Tree,*Late;
printf("递归构造二叉树\n");
CreatBiTree(&Tree);
printf("递归遍历二叉树\n");
PreOrderTraverse(&Tree,PrintElement);
printf("递归结束\n");
return 0;
}
/**************************************************************/
int CreatBiTree(BiTree **T)
{
BiTree *Tree = *T;
TElemType ch;
// fflush(stdin);
scanf(FORMAT,&ch);
if(ch == '#' ) Tree = NULL;
else{
Tree = (BiTree *) malloc(sizeof(BiTree));
if(!Tree){
perror("存储分配失败\n");
exit(1);
}
Tree->data = ch;
CreatBiTree(&Tree->lchild);
CreatBiTree(&Tree->rchild);
}
return 0;
}
/************************************************************/
int PreOrderTraverse(BiTree **T,int (* Visit)(TElemType e))
{
BiTree *Tree = *T;
if(Tree){
// if(!Visit(Tree->data)){ //发生段错误
if(!PreOrderTraverse(&Tree->lchild,PrintElement))
if( !PreOrderTraverse(&Tree->rchild,PrintElement))
return 0;
else
exit(1);
}
}
return 0;
}
/**************************************************************/
int PrintElement(TElemType e){
printf(FORMAT,e);
return 0;
}
/******************************************************************/