码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构-顺序树的创建以及按广义表输出

时间:2019-06-03 12:12:59      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:printf   span   define   顺序   array   return   treenode   std   数据结构   

 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 }

 

数据结构-顺序树的创建以及按广义表输出

标签:printf   span   define   顺序   array   return   treenode   std   数据结构   

原文地址:https://www.cnblogs.com/sucker/p/10966517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!