1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef char ElemType; 5 6 typedef struct node{ 7 ElemType data; 8 struct node *lson; 9 struct node *rson; 10 }Bynode,*Bytree; 11 12 Bytree CreateBytree()//树的建立 13 { 14 char ch; 15 Bytree t; 16 ch = getchar(); 17 if ( ch == '1') 18 t = NULL; 19 else 20 { 21 t = (Bytree)malloc(sizeof(Bynode)); 22 t->data = ch; 23 t->lson = CreateBytree(); 24 t->rson = CreateBytree(); 25 } 26 return t; 27 } 28 29 void pre(Bytree t)//先序遍历 30 { 31 if(t) 32 { 33 putchar(t->data); 34 pre(t->lson); 35 pre(t->rson); 36 } 37 } 38 39 void in(Bytree t)//中序遍历 40 { 41 if(t) 42 { 43 in(t->lson); 44 putchar(t->data); 45 in(t->rson); 46 } 47 } 48 49 void post(Bytree t)//后序遍历 50 { 51 if(t) 52 { 53 post(t->lson); 54 post(t->rson); 55 putchar(t->data); 56 } 57 } 58 59 void main() 60 { 61 Bytree t; 62 t=CreateBytree(); 63 printf("先序遍历:"); 64 pre(t); 65 putchar('\n'); 66 printf("中序遍历:"); 67 in(t); 68 putchar('\n'); 69 printf("后续遍历:"); 70 post(t); 71 putchar('\n'); 72 }