标签:char color void getchar tree sizeof nbsp creat def
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 void createTree(Tree *t, int *count) 13 { 14 DataType ch; 15 if( (ch = getchar()) == ‘;‘)//输入‘;‘代表创建树结束 16 { 17 getchar(); 18 return; 19 } 20 getchar(); 21 if(ch != ‘#‘)//输入‘#‘代表是空节点 22 { 23 t = (Tree *)malloc(sizeof(Tree));//创建节点 24 t->key = ch; 25 (*count)++; 26 printf("请输入左节点数据,如为空节点请输入\‘#\‘\n"); 27 createTree(t->left, count); //创建左子树 28 printf("请输入右节点数据,如为空节点请输入\‘#\‘\n"); 29 createTree(t->right, count);//创建右子树 30 } 31 else 32 { 33 t = NULL; 34 } 35 } 36 void firstPrint(Tree *t)//先序遍历输出 37 { 38 if(t != NULL) 39 { 40 printf("%c\t",t->key); 41 firstPrint(t->left); 42 firstPrint(t->right); 43 } 44 } 45 main() 46 { 47 Tree t; 48 printf("请输入根节点数据,如为空节点请输入\‘#\‘\n"); 49 createTree(&t, &count); 50 system("cls"); 51 firstPrint(&t); 52 }
标签:char color void getchar tree sizeof nbsp creat def
原文地址:https://www.cnblogs.com/sucker/p/10929960.html