C 二叉树(先根)

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 #define Max 50
 5 
 6 
 7 typedef struct node{
 8     char data[Max];
 9     int n;
10 }sqTree;
11 //先根二叉树 
12 void tree(sqTree &t,char ch[],int n,int &s){
13     char c=ch[s++];
14     if(c==';')  
15         return ;
16     if(c!='#'){
17         t.data[n]=c;
18         tree(t,ch,2*n+1,s);
19         tree(t,ch,2*n+2,s);
20     }
21      
22 }
23 
24 stree(sqTree &t,int i){
25     if(t.data[i]!='#'){
26         printf("%c",t.data[i]);
27         printf("(");
28         stree(t,2*i+1);
29         printf(";");
30         stree(t,2*i+2);
31         printf(")");    
32     }
33 }
34 
35 
36 int main(){
37     sqTree t;
38     char ch[]={'a','b','c','#','#','d','#','#','e','f','#','#','g','#','#',';'};//先根 
39     int a,b=0,c=0;
40     for (a=0;a<Max;a++){
41         t.data[a]='#';
42     }
43     tree(t,ch,b,c);
44     stree(t,b);
45 } 

猜你喜欢

转载自www.cnblogs.com/3w1z3/p/10981372.html