二叉树实现代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 typedef struct CTNode
 5 {
 6     char data;
 7     struct CTNode *Lchild;
 8     struct CTNode *Rchild;
 9 }CTNode;
10 
11 void CreateTree(CTNode *(*T))
12 {
13     char c;
14 
15     scanf("%c",&c);
16     if(' ' == c)
17     {
18         *T = NULL;
19     }
20     else
21     {
22         *T = (CTNode *)malloc(sizeof(CTNode));
23         (*T)->data = c;
24         CreateTree(&(*T)->Lchild);//递归
25         CreateTree(&(*T)->Rchild);
26     }
27 }
28 void ShowTree(CTNode *T)
29 {
30     if( T != NULL)
31     {
32         printf("%c ",T->data);
33         ShowTree((T->Lchild));
34         ShowTree((T->Rchild));
35     }
36 }
37 int main()
38 {
39     CTNode *Tree;
40 
41     CreateTree(&Tree);
42     ShowTree(Tree);
43     printf("\n%c\n%c\n",Tree->data,Tree->Lchild->data,);
44     
45     return 0;
46 }

猜你喜欢

转载自www.cnblogs.com/ghost-98210/p/9486686.html