首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

简单的小疑点,求指点

2012-12-30 
简单的小问题,求指点。#include stdio.h#include stdlib.h#include malloc.htypedef struct BiTNode{

简单的小问题,求指点。


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct BiTNode
{
char data;
struct BiTNode *lchild , *rchild;
}BiTNode, *BiTree;

BiTree CreatBiTree(BiTree T)
{
char ch=0;
ch = getchar();
if(ch == '#')
T = NULL;
else
{
T = (BiTNode*)malloc(sizeof(BiTNode));
if(!T)
{
printf("申请空间失败!\n");
}
T->data = ch;
T->lchild = CreatBiTree(T->lchild);
T->rchild = CreatBiTree(T->rchild);
}
return T;
}
void PreTravel(BiTree T)
{
if(T)
{
printf("%c  ",T->data);
PreTravel(T->lchild);
PreTravel(T->rchild);
}
}
void InTravel(BiTree T)
{
if(T)
{
InTravel(T->lchild);
printf("%c  ",T->data);
InTravel(T->rchild);
}
}
void EndTravel(BiTree T)
{
if(T)
{
EndTravel(T->lchild);
EndTravel(T->rchild);
printf("%c  ",T->data);
}
}

 void main()
{
BiTree tree = NULL;
tree = CreatBiTree(tree);
printf("二叉树建立完成!\n");
printf("递归方法\n前序遍历:") ; PreTravel(tree) ; printf("\n");
printf("中序遍历:") ; InTravel(tree) ; printf("\n");
printf("后序遍历:") ; EndTravel(tree) ; printf("\n");
system("pause");
}

一个二叉树的递归遍历已经实现了,但是我有一个疑惑就是主函数中BiTree tree = NULL;为什么要给他初始设置为NULL,如果不设置为NULL就执行不了。。请问是什么问题。。。想不明白。
[解决办法]
 void main() {    
 BiTree tree = CreatBiTree(); 

热点排行