user-0 提问: 关于二叉查找树的问题

回复:
user: user-1
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-2
content: free(T);这样肯定有问题啊。你本来是要构造，那你把节点所指空间都释放了还怎么玩呢

user: user-3
content: #include <stdio.h> #include <stddef.h> #include <stdlib.h>  #ifndef _Tree_H  struct TreeNode; typedef struct TreeNode *Position; typedef struct TreeNode *SearchTree;  void MakeEmpty (SearchTree *T); /*  * Position Find( int X, SearchTree T );  * Position FindMin( SearchTree T );  * Position FindMax( SearchTree T );  * SearchTree Insert( int X, SearchTree T );  * SearchTree Delete( int X, SearchTree T );  * */ #endif /*_Tree_H*/  struct TreeNode {     int Element;     SearchTree Left;     SearchTree Right; }; SearchTree MakeEmpty2(void) {     SearchTree T;     int data;      printf("Please input data: \n");     scanf("%d", &data);      if (data == -1) {         T =  NULL;     } else {         T = (SearchTree)malloc(sizeof(struct TreeNode));         T->Element = data;         T->Left = MakeEmpty2();         T->Right = MakeEmpty2();     }      return T; }  void MakeEmpty (SearchTree *T) {     int data;     printf("Please input data: \n");     scanf("%d", &data);      if (data == -1) {         *T =  NULL;     } else {         *T = (SearchTree)malloc(sizeof(struct TreeNode));         (*T)->Element = data;         MakeEmpty(&((*T)->Left));         MakeEmpty(&((*T)->Right));     } }  int visit(SearchTree T) {     return T->Element; }  // 先序遍历 void preOrderTraverse(SearchTree T) {     if(T)     {         printf("data = %d\n", visit(T));         preOrderTraverse(T->Left);         preOrderTraverse(T->Right);     } }  // 中序遍历 void inOrderTraverse(SearchTree T) {     if(T)     {         inOrderTraverse(T->Left);         printf("data = %d\n", visit(T));         inOrderTraverse(T->Right);     } }  // 后序遍历 void postOrderTraverse(SearchTree T) {     if(T)     {         postOrderTraverse(T->Left);         postOrderTraverse(T->Right);         printf("data = %d\n", visit(T));     } }     int main( void ) {     SearchTree T;     /*     //T = (SearchTree)malloc(sizeof(SearchTree));     T = (SearchTree)malloc(sizeof(struct TreeNode));     T->Left = NULL;     T->Right = NULL;     */     MakeEmpty(&T);     preOrderTraverse(T);     return 0; }  demo作为一个参考吧

user: user-0
content: 引用 5 楼 pengzhixi 的回复:1. 分配的大小应该是 sizeof( struct TreeNode )，而不是sizeof( SearchTree ) 你是要分配一个结构体大小，而不是一个指针大小的内存。 2. 分配之后需要将那段内存清零，否则你那个递归构造T->Left， T->Right可能由于内存不干净而导致非法访问 哦，我还以为使用typedef后两个就等价了。。。 分配后的内存清零是使用 free()函数吗？在什么地方使用啊，不是很懂这个。。。

user: user-2
content: 1. 分配的大小应该是 sizeof( struct TreeNode )，而不是sizeof( SearchTree ) 你是要分配一个结构体大小，而不是一个指针大小的内存。 2. 分配之后需要将那段内存清零，否则你那个递归构造T->Left， T->Right可能由于内存不干净而导致非法访问

user: user-3
content: 引用 3 楼 qq_36230830 的回复:[quote=引用 2 楼 user-4 的回复:] 没仔细看 ....  T = malloc( sizeof( TreeNode ) )SearchTree与TreeNode指的不是一个东西吗？ 我换了以后编译不通过 报错信息： [Error] 'TreeNode' undeclared (first use in this function) [Note] each undeclared identifier is reported only once for each function it appears in [/quote] 改成这样：T = (SearchTree)malloc( sizeof(struct  TreeNode ) ) ;

user: user-0
content: 引用 2 楼 user-4 的回复:没仔细看 ....  T = malloc( sizeof( TreeNode ) )SearchTree与TreeNode指的不是一个东西吗？ 我换了以后编译不通过 报错信息： [Error] 'TreeNode' undeclared (first use in this function) [Note] each undeclared identifier is reported only once for each function it appears in

user: user-4
content: 没仔细看 ....  T = malloc( sizeof( TreeNode ) )

user: user-0
content: #include <stdio.h> #include <stddef.h> #include <stdlib.h>  #ifndef _Tree_H  struct TreeNode; typedef struct TreeNode *Position; typedef struct TreeNode *SearchTree;  SearchTree MakeEmpty (SearchTree T ); /* Position Find( int X, SearchTree T ); Position FindMin( SearchTree T ); Position FindMax( SearchTree T ); SearchTree Insert( int X, SearchTree T ); SearchTree Delete( int X, SearchTree T ); */ #endif /*_Tree_H*/  struct TreeNode { 	int Element;  	SearchTree Left; 	SearchTree Right; };  SearchTree MakeEmpty ( SearchTree T ) { 	if( T != NULL) 	{ 		MakeEmpty( T->Left ); 		MakeEmpty( T->Right ); 		free(T); 	} 	return NULL; } int main( void ) { 	SearchTree T;     T = malloc( sizeof( SearchTree ) ); 	T = MakeEmpty( T ); 	return 0; } 上面的不知怎么少了个分号。。。