树的遍历

Source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <stdio.h>

#include <malloc.h>

#include <stdlib.h>

#define MAX_TREE_SIZE 100
typedef struct{

int data;

int parent; //双亲位置域

}PTNode;



/*双亲表示法树结构*/

typedef struct {

PTNode node[MAX_TREE_SIZE];

int count; //根的位置和节点个数

}PTree;





/*树的孩子兄弟表示结点结构定义*/

typedef struct node{

int data;

struct node *firstchild;

struct node *rightsib; }BTNode,*BTree;

void init_ptree(PTree *tree) {

tree->count=-1;

}

//初始化树结点(孩子兄弟表示法)

BTNode GetTreeNode(int x) {

BTNode t;

t.data=x;

t.firstchild=t.rightsib=NULL;

return t;

}



//树的前序遍历(递归)

void preorder(BTNode *T) {

if(T!=NULL)

{

printf("%d ",T->data);

preorder(T->firstchild);

preorder(T->rightsib);

}

}



//树的前序遍历(非递归)

void preorder2(PTree T) {

int i;

for(i=0;i<T.count;i++)

{

printf("%d ",T.node[i]);

}

}





//树后序遍历(递归)

void inoeder(BTNode *T) {

if(T!=NULL)

{

inoeder(T->firstchild);

printf("%d ",T->data);

inoeder(T->rightsib);

}

}



//树后序遍历(非递归)

void inoeder2(PTree T) {

int i;

for(i=T.count-1;i>=0;i--)

{

printf("%d",T.node[i]);

}



}



//层次遍历

void level(PTree T) {

int i;

for(i=0;i<T.count;i++)

{

printf("%d ",T.node[i]);

}



}



//水平输出二叉树

void PrintBTree(BTNode *root,int level)

{

int i;

if(root!=NULL)

{

PrintBTree(root->rightsib,level+1);

for(i=1;i<=8*level;i++)

printf(" ");

printf("-------%d\n",root->data);

PrintBTree(root->firstchild,level+1);

}

}





//输出树

void print_ptree(PTree tree) {

int i;

printf(" 序号 结点 双亲\n");

for(i=0;i<=tree.count;i++)

{

printf("%8d%8d%8d",i,tree.node[i].data,tree.node[i].parent);

printf("\n");

}

}





/*用双亲表示法创建树*/

PTree CreatTree(PTree T) {

int i=1;

int fa,ch;

PTNode p;

for(i=1;ch!=-1;i++)

{

printf("输入第%d结点:\n",i);

scanf("%d,%d",&fa,&ch);

printf("\n");

p.data=ch;
p.parent=fa;

T.count++;

T.node[T.count].data = p.data;

T.node[T.count].parent = p.parent;

}

printf("\n");

printf("创建的树具体情况如下:\n");

print_ptree(T);

return T;

}



/*一般树转换成二叉树*/

BTNode *change(PTree T){
int i,j=0;
BTNode p[MAX_TREE_SIZE];
BTNode *ip,*is,*ir,*Tree;
ip=(BTNode *)malloc(sizeof(BTNode));
is=(BTNode *)malloc(sizeof(BTNode));
ir=(BTNode *)malloc(sizeof(BTNode));
Tree=(BTNode *)malloc(sizeof(BTNode));
for(i=0;i<T.count;i++){
p[i]=GetTreeNode(T.node[i].data);
}
for(i=1;i<T.count;i++){
ip=&p[i];
is=&p[j];
while(T.node[i].parent!=is->data){
j++;
is=&p[j];
}
if(!(is->firstchild)){
is->firstchild=ip;
ir=ip;
}else
{
ir->rightsib=ip;
ir=ip;
}
}
Tree=&p[0];
return Tree;
}



/*主菜单*/

void Menu()

{

printf("=================主菜单=======================\n");

printf("***输入1-------------以双亲法创建一棵一般树***\n");

printf("***输入2-------------树的前序遍历(递归)*******\n");

printf("***输入3-------------树的后序遍历(递归)*******\n");

printf("***输入4-------------树的前序遍历(非递归)*****\n");

printf("***输入5-------------树的后序遍历(非递归)*****\n");

printf("***输入6-------------层次序的非递归遍历*******\n");

printf("***输入0-------------退出程序*****************\n");

printf("==============================================\n");

printf("请输入执行的指令:");

}



/*副菜单*/

void Menu2()

{

printf("*****************副菜单*******************\n");

printf("***9-------------返回主菜单继续操作*******\n");

printf("***0-------------退出程序*****************\n"); }



/*主函数*/

int main(){

int i=0,c1,c2;

PTree T;

BTNode *Tree;

init_ptree(&T);

loop:

Menu();

scanf("%d",&c1);

switch(c1)

{

case 1:

printf("建立一般树,依次输入各个结点情况:\n");
printf("输入结点方式:双亲数据,整型数据(第一个结点双亲数据为-1,最后以-1,-1结束)\n例子:-1,1 1,3\n");

T=CreatTree(T);

Tree=change(T);

printf("一般树转换成二叉树后的情况:\n");

PrintBTree(Tree,i);

getchar();

break;



case 2:

printf("树的前序遍历(递归):\n");

preorder(Tree);

printf("\n");

break;



case 3:

printf("树的后序遍历(递归):\n");

inoeder(Tree);

printf("\n");

break;



case 4:

printf("树的前序遍历(非递归):\n");

preorder2(T);

printf("\n");

break;



case 5:

printf("树的后序遍历(非递归):\n");

inoeder2(T);

printf("\n");

break;



case 6:

printf("树的层次遍历:\n");

level(T);

printf("\n");

break;



case 0:

exit(1);

break;

}

Menu2();
scanf("%d",&c2);

if(c2==9)

goto loop;

else if(c2==0)

exit(1);
return 0;



}