Data structure - the sequence tree created by the generalized table, and an output

 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 is          creatBinTree (T, Array, index * 2 + 2 );
 32      }
 33 is  }
 34  // output by binary generalized table 
35  void printBinTree (BinTree T *, int index)
 36  {
 37 [      IF (T! = NULL)
 38 is      {
 39          the 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 }

 

Guess you like

Origin www.cnblogs.com/sucker/p/10966517.html