加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 04:45:21 所属栏目:大数据 来源:网络整理
导读: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 MaxS
 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,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 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读