由順序方式儲存的完全二叉樹進行重建 (c++)

來源:互聯網
上載者:User
文章目錄
  •  實驗題目(共6題, 第3題)
 實驗題目(共6題, 第3題)
標題: 由順序方式儲存的完全二叉樹進行重建
時 限: 1000 ms
記憶體限制: 3000 K
總時限: 3000 ms
描述: 按順序方式儲存的一棵完全二叉樹的結點記錄,結點個數為n。根據所輸入的順序結構的結點記錄建立二叉樹,輸出樹的先序,中序和後序遍曆結果。 
註:數字“0”表示不存在此結點,沒有孩子結點
輸入: 樹結點個數n 
順序方式儲存的完全二叉樹
輸出: 先序遍曆輸出 
中序遍曆輸出 
後序遍曆輸出
輸入範例: 10 
1 2 0 3 4 0 0 5 6 7 
輸出範例: 1 2 3 5 6 4 75 3 6 2 7 4 15 6 3 7 4 2 1
提示: 數字“0”的孩子結點全部為“0”
來源:
View Code

  1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<malloc.h>
4
5 typedef int ElemType;
6 typedef struct TNode
7 {
8 ElemType data;
9 struct TNode* lChild;
10 struct TNode* rChild;
11 }TNode,*BiTree;
12
13 ElemType* Get(int);
14 BiTree Creat_BiTree(ElemType *,int,int);
15 void PreOrderTraverse(BiTree);
16 void InOrderTraverse(BiTree);
17 void PostOrderTraverse(BiTree);
18 void DestroyTree(BiTree);
19
20 int main(void)
21 {
22 int n;
23 //int i;
24 ElemType *a;
25 BiTree T;
26
27 scanf("%d",&n);
28 a=Get(n);
29 //for(i=0;i<n;i++)
30 // printf("%d\n",a[i]);
31 T=Creat_BiTree(a,0,n);
32 PreOrderTraverse(T);
33 printf("\n");
34 InOrderTraverse(T);
35 printf("\n");
36 PostOrderTraverse(T);
37 printf("\n");
38 DestroyTree(T);
39 free(a);
40
41 return 0;
42 }
43
44 ElemType* Get(int n)
45 {
46 int i;
47 ElemType *a;
48 a=(ElemType*)malloc(sizeof(ElemType)*n);
49 if(!a)
50 {
51 printf("out of space!\n");
52 exit(0);
53 }
54 for(i=0;i<n;i++)
55 scanf("%d",&a[i]);
56
57 return a;
58 }
59
60 BiTree Creat_BiTree(ElemType *a,int i,int n)
61 {
62 //int i;
63 BiTree tmpcell;
64 if(*a==0||i>n-1)
65 return NULL;
66 /*else if(2*i+1>n-1)
67 {
68 tmpcell=(BiTree)malloc(sizeof(TNode));
69 tmpcell->data=*a;
70 tmpcell
71 }*/
72 else
73 {
74 tmpcell=(BiTree)malloc(sizeof(TNode));
75 tmpcell->data=*a;
76 tmpcell->lChild=Creat_BiTree(a+i+1,2*i+1,n);
77 tmpcell->rChild=Creat_BiTree(a+i+2,2*i+2,n);
78 }
79
80 return tmpcell;
81 }
82
83 void PreOrderTraverse(BiTree T)
84 {
85 if(T!=NULL)
86 {
87 printf("%d ",T->data);
88 PreOrderTraverse(T->lChild);
89 PreOrderTraverse(T->rChild);
90 }
91 }
92
93 void InOrderTraverse(BiTree T)
94 {
95 if(T!=NULL)
96 {
97 InOrderTraverse(T->lChild);
98 printf("%d ",T->data);
99 InOrderTraverse(T->rChild);
100 }
101 }
102
103 void PostOrderTraverse(BiTree T)
104 {
105 if(T!=NULL)
106 {
107 PostOrderTraverse(T->lChild);
108 PostOrderTraverse(T->rChild);
109 printf("%d ",T->data);
110 }
111 }
112
113 void DestroyTree(BiTree T)
114 {
115 if(T!=NULL)
116 {
117 DestroyTree(T->lChild);
118 DestroyTree(T->rChild);
119 free(T);
120 }
121 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.