1 #include"stdio.h"
2 #define MaxSize 30
3 typedef char DataType;
4 typedef struct TreeNode{
5 DataType data[MaxSize];
6 int count;
7 }BinTree;
8 int k = 0;
9 //初始化树
10 void initBinTree(BinTree *t)
11 {
12 for( int i= 0; i< MaxSize; i++)
13 {
14 t->data[i] = '#';
15 }
16 t->count = 0;
17 }
18 //创建二叉树
19 void creatBinTree(BinTree *t, DataType array[], int index)
20 {
21 DataType ch = array[k++];
22 if( ch == ';')
23 {
24 return ;
25 }
26 if( ch != '#' )
27 {
28 t->data[index] = ch;
29 t->count++;
30 creatBinTree(t, array, index*2+1);
31 creatBinTree(t, array, index*2+2);
32 }
33 }
34 //按广义表输出二叉树
35 void printBinTree(BinTree *t, int index)
36 {
37 if(t != NULL)
38 {
39 printf("%c ",t->data[index]);
40 if( t->data[index*2+1] != '#' || t->data[index*2+2] != '#' )
41 {
42 printf("(");
43 printBinTree(t, index*2+1);
44 printf(",");
45 printBinTree(t, index*2+2);
46 printf(")");
47 }
48 }
49 }
50 main()
51 {
52 BinTree t;
53 int n;
54 int index = 0;
55 DataType ch[] = {'A','B','C','#','#','E','#','#','F','G','#','#','H','#','#',';'};
56 initBinTree(&t);
57 creatBinTree(&t, ch, index);
58 printBinTree(&t, index);
59 }