首页 新闻 搜索 专区 学院

数据结构----生成二叉树

1
[已解决问题] 解决于 2012-05-26 11:16

采用递归生成二叉树,头结点无法获取

 1 #include "stdio.h"
 2 #include "stdlib.h" 
 3 
 4 typedef struct BTree 
 5 {
 6     int data;
 7     struct BTree * lchild;
 8     struct BTree * rchild;
 9 }BTreeNode, *PBTree;
10 
11 
12 
13 void Create_Tree ( PBTree T )
14 {
15     int num;
16     scanf ( "%d", &num );
17     if ( num == 0 )
18         T = NULL;
19     else
20     {
21         T = ( PBTree ) malloc ( sizeof (BTreeNode) );
22 
23         T->data = num;
24     
25         Create_Tree ( T->lchild );
26         Create_Tree ( T->rchild );
27     }
28 }
29 
30 
31 void Show_Tree ( PBTree T )
32 {
33     if (T)
34     {
35         printf ( "%d\t", T->data );
36         Show_Tree ( T->lchild );
37         Show_Tree ( T->rchild );
38     }
39 }
40 
41 
42 
43 int main ( void )
44 {
45     PBTree T = NULL;
46     Create_Tree ( T );
47 
48     Show_Tree ( T );
49 
50     return 0;
Anger_Coder的主页 Anger_Coder | 初学一级 | 园豆:186
提问于:2012-05-15 15:11
< >
分享
最佳答案
1

#include "stdio.h"
#include "stdlib.h"

typedef struct BTree
{
int data;
struct BTree * lchild;
struct BTree * rchild;
}BTreeNode, *PBTree;
void Create_Tree ( PBTree *T )
{
int num;
scanf ( "%d", &num );
if (num == 0 )
(*T)=NULL;
else
{
*T = ( PBTree) malloc ( sizeof (BTreeNode) );
(*T)->data = num;
Create_Tree (&(*T)->lchild );
Create_Tree (&(*T)->rchild );
}
}

void Show_Tree ( PBTree T )
{
if (T)
{
printf ( "%d\t", T->data );
Show_Tree ( T->lchild );
Show_Tree ( T->rchild );
}
}

 

int main ( void )
{
PBTree T= NULL;
Create_Tree(&T);
Show_Tree (T);
printf("\n");
return 0;
}

函数中传参时生成一个临时指针PBTree tmp=T,函数运行的过程中操作的是tmp,T没有更改

奖励园豆:5
星空雾雨 | 菜鸟二级 |园豆:311 | 2012-05-15 16:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册