1 #include"stdio.h" 2 #include"string.h" 3 #include"malloc.h" 4 #include"stdlib.h" 5 typedef char DataType; 6 typedef struct Tree{ 7 DataType key;//存储的数据 8 struct Tree *left;//左孩子 9 struct Tree *right;//右孩子 10 }Tree; 11 int count = 0; 12 is void createTree (Tree T *, int * COUNT) 13 is { 14 the DataType CH; 15 IF ((CH = getchar ()) == ' ; ' ) // Type ';' represents the end of the tree is created 16 { . 17 getchar ( ); 18 is return ; . 19 } 20 is getchar (); 21 is IF ! (CH = ' # ' ) // Type '#' is representative of an empty node 22 is { 23 is T = (Tree *) the malloc ( the sizeof(Tree)); // Create a node 24- T-> Key = CH; 25 (* COUNT) ++ ; 26 printf ( " Please enter the left node data, such as to enter the air node \ '# \' \ the n- " ) ; 27 createTree (T-> left, COUNT); // create a left subtree 28 the printf ( " Please enter the right-node data, such as to enter the air node \ '# \' \ n- " ); 29 createTree (T-> right, COUNT); // Create a right subtree 30 } 31 is the else 32 { 33 is T = NULL; 34 is } 35 } 36 void firstPrint (Tree * T) // preorder output 37 [ { 38 is IF (T =! NULL) 39 { 40 the printf ( " % C \ T " , T-> Key); 41 is firstPrint (T-> left) ; 42 is firstPrint (T-> right); 43 is } 44 is } 45 main () 46 is { 47 Tree T; 48 the printf ( " Please enter the root node data, such as to enter the air node \ '# \' \ n- " ); 49 createTree (& T, & COUNT); 50 system("cls"); 51 firstPrint(&t); 52 }