1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <strings.h> 4 #define MAX 100 5 6 char * p; 7 typedef struct BTree{ 8 char * str; 9 struct BTree * lchild; 10 struct BTree * rchild; 11 }BTree; 12 13 BTree* creat_tree(){ 14 BTree* temp; 15 while(*p==' ')p++; 16 if(*p=='#'){ 17 p++; 18 return NULL; 19 } 20 if(*p!=' '){ 21 int i=0; 22 temp = (BTree *)malloc(sizeof(BTree)); 23 temp->str[i++]=*p++; 24 temp->lchild=creat_tree(); 25 temp->rchild=creat_tree(); 26 } 27 return temp; 28 } 29 30 void pre_visit(BTree* node){ 31 printf("%c",node->str); 32 if(node->lchild!=NULL)pre_visit(node->lchild); 33 if(node->rchild!=NULL)pre_visit(node->rchild); 34 } 35 int main() 36 { 37 char tree[MAX];p=tree; 38 BTree * head; 39 printf("Please input the tree(use char and #) 要求按照先序遍历的方式输入,加上#进行区分 例如123# #4##5##: "); 40 //scanf("%s",tree); 41 gets(tree); 42 43 if(*p!='