采用递归生成二叉树,头结点无法获取
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;
#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没有更改