树与二叉树的转换与遍历

树的初始化函数(双亲法和孩子结点法两种),

建树函数,

输出树函数,

树的前序遍历函数(递归和非递归两种),

树的后序遍历函数(递归和非递归两种),

树的层次遍历函数,

一般树和二叉树的转换函数。

主菜单和副菜单。

主函数。

具体代码如下:

  1 #include <stdio.h>
  2 
  3 #include <malloc.h>
  4 
  5 #include <stdlib.h>
  6 
  7 //设置常量:
  8 
  9 #define MAX_TREE_SIZE 100
 10 
 11 //一般树的存储结构有以下几种:双亲结点,孩子结点,孩子兄弟结点。本实验运用到的是双亲结点和孩子兄弟结点。具体存储结构如下:
 12 
 13 /*树的双亲表示结点结构定义*/
 14 
 15 typedef struct    
 16 
 17 {
 18 
 19     int data;
 20 
 21     int parent;        //双亲位置域
 22 
 23 }PTNode;
 24 
 25  
 26 
 27 /*双亲表示法树结构*/
 28 
 29 typedef struct       
 30 
 31 {
 32 
 33     PTNode node[MAX_TREE_SIZE];
 34 
 35     int count;        //根的位置和节点个数
 36 
 37 }PTree;
 38 
 39  
 40 
 41  
 42 
 43 /*树的孩子兄弟表示结点结构定义*/
 44 
 45 typedef struct node{
 46 
 47        int data;
 48 
 49        struct node *firstchild;
 50 
 51        struct node *rightsib;
 52 
 53 }BTNode,*BTree;
 54 
 55  
 56 
 57 //初始化树(双亲表示法)
 58 
 59 void init_ptree(PTree *tree)
 60 
 61 {
 62 
 63     tree->count=-1;
 64 
 65 }
 66 
 67  
 68 
 69 //初始化树结点(孩子兄弟表示法)
 70 
 71 BTNode GetTreeNode(int x)
 72 
 73 {
 74 
 75        BTNode t;
 76 
 77        t.data=x;
 78 
 79        t.firstchild=t.rightsib=NULL;
 80 
 81        return t;
 82 
 83 }
 84 
 85  
 86 
 87 //树的前序遍历(递归)
 88 
 89 void preorder(BTNode *T)
 90 
 91 {
 92 
 93        if(T!=NULL)
 94 
 95        {
 96 
 97               printf("%d ",T->data);
 98 
 99               preorder(T->firstchild);
100 
101               preorder(T->rightsib);
102 
103        }
104 
105 }
106 
107  
108 
109 //树的前序遍历(非递归)
110 
111 void preorder2(PTree T)
112 
113 {
114 
115        int i;
116 
117        for(i=0;i<T.count;i++)
118 
119        {
120 
121               printf("%d ",T.node[i]);
122 
123        }
124 
125 }
126 
127  
128 
129  
130 
131 //树后序遍历(递归)
132 
133 void inoeder(BTNode *T)
134 
135 {
136 
137        if(T!=NULL)
138 
139        {
140 
141               inoeder(T->firstchild);
142 
143               printf("%d ",T->data);
144 
145               inoeder(T->rightsib); 
146 
147        }
148 
149 }
150 
151  
152 
153 //树后序遍历(非递归)
154 
155 void inoeder2(PTree T)
156 
157 {
158 
159        int i;
160 
161        for(i=T.count-1;i>=0;i--)
162 
163        {
164 
165               printf("%d ",T.node[i]);
166 
167        }    
168 
169       
170 
171 }
172 
173  
174 
175 //层次遍历
176 
177 void level(PTree T)
178 
179 {
180 
181        int i;
182 
183        for(i=0;i<T.count;i++)
184 
185        {
186 
187               printf("%d ",T.node[i]);
188 
189        }
190 
191       
192 
193 }
194 
195  
196 
197 //水平输出二叉树
198 
199 void PrintBTree(BTNode *root,int level)
200 
201 {
202 
203   int i;
204 
205   if(root!=NULL)
206 
207   {
208 
209     PrintBTree(root->rightsib,level+1);
210 
211     for(i=1;i<=8*level;i++)
212 
213       printf(" ");
214 
215       printf("-------%d
",root->data);
216 
217     PrintBTree(root->firstchild,level+1);
218 
219   }
220 
221 }
222 
223  
224 
225  
226 
227 //输出树
228 
229 void print_ptree(PTree tree)
230 
231 {
232 
233     int i;
234 
235     printf("    序号    结点    双亲
");
236 
237     for(i=0;i<=tree.count;i++)
238 
239     {
240 
241         printf("%8d%8d%8d",i,tree.node[i].data,tree.node[i].parent);
242 
243         printf("
");
244 
245     }
246 
247 }
248 
249  
250 
251  
252 
253 /*用双亲表示法创建树*/
254 
255 PTree CreatTree(PTree T)
256 
257 {
258 
259        int i=1;
260 
261        int fa,ch;
262 
263        PTNode p;
264 
265        for(i=1;ch!=-1;i++)
266 
267        {
268 
269               printf("输入第%d结点:
",i);
270 
271               scanf("%d,%d",&fa,&ch);
272 
273               printf("
");
274 
275               p.data=ch;
276 
277               p.parent=fa;
278 
279               T.count++;
280 
281        T.node[T.count].data = p.data;
282 
283           T.node[T.count].parent = p.parent;
284 
285        }
286 
287        printf("
");
288 
289        printf("创建的树具体情况如下:
");
290 
291        print_ptree(T);
292 
293        return T;
294 
295 }
296 
297  
298 
299 /*一般树转换成二叉树*/
300 
301 BTNode *change(PTree T)
302 
303 {
304 
305        int i,j=0;
306 
307        BTNode p[MAX_TREE_SIZE];
308 
309        BTNode *ip,*is,*ir,*Tree;
310 
311        ip=(BTNode *)malloc(sizeof(BTNode));
312 
313        is=(BTNode *)malloc(sizeof(BTNode));
314 
315        ir=(BTNode *)malloc(sizeof(BTNode));
316 
317        Tree=(BTNode *)malloc(sizeof(BTNode));
318 
319        for(i=0;i<T.count;i++)
320 
321        {
322 
323               p[i]=GetTreeNode(T.node[i].data);
324 
325        }
326 
327        for(i=1;i<T.count;i++)
328 
329        {
330 
331               ip=&p[i];
332 
333               is=&p[j];
334 
335               while(T.node[i].parent!=is->data)
336 
337               {
338 
339                      j++;
340 
341                      is=&p[j];
342 
343                     
344 
345               }
346 
347               if(!(is->firstchild))
348 
349               {
350 
351                      is->firstchild=ip;
352 
353                      ir=ip;
354 
355               }
356 
357               else
358 
359               {
360 
361                      ir->rightsib=ip;
362 
363                      ir=ip;
364 
365               }
366 
367        }
368 
369        Tree=&p[0];
370 
371        return Tree;
372 
373       
374 
375 }
376 
377  
378 
379 /*主菜单*/
380 
381 void Menu()
382 
383 {
384 
385        printf("=================主菜单=======================
");
386 
387        printf("***输入1-------------以双亲法创建一棵一般树***
");
388 
389    printf("***输入2-------------树的前序遍历(递归)*******
");
390 
391    printf("***输入3-------------树的后序遍历(递归)*******
");
392 
393    printf("***输入4-------------树的前序遍历(非递归)*****
");
394 
395    printf("***输入5-------------树的后序遍历(非递归)*****
");
396 
397    printf("***输入6-------------层次序的非递归遍历*******
");
398 
399    printf("***输入0-------------退出程序*****************
");
400 
401    printf("==============================================
");
402 
403    printf("请输入执行的指令:");
404 
405 }
406 
407  
408 
409 /*副菜单*/
410 
411 void Menu2()
412 
413 {
414 
415        printf("*****************副菜单*******************
");
416 
417        printf("***9-------------返回主菜单继续操作*******
");
418 
419        printf("***0-------------退出程序*****************
");
420 
421 }
422 
423  
424 
425 /*主函数*/
426 
427 void main()
428 
429 {
430 
431     int i=0,c1,c2;
432 
433     PTree T;
434 
435     BTNode *Tree;
436 
437     init_ptree(&T);
438 
439     loop:
440 
441     Menu();
442 
443        scanf("%d",&c1);
444 
445     switch(c1)
446 
447     {
448 
449           case  1:
450 
451                printf("建立一般树,依次输入各个结点情况:
");
452 
453                  printf("输入结点方式:双亲数据,整型数据(第一个结点双亲数据为-1,最后以-1,-1结束)
例子:-1,1  1,3
");
454 
455                      T=CreatTree(T);
456 
457                      Tree=change(T);
458 
459                      printf("一般树转换成二叉树后的情况:
");
460 
461                      PrintBTree(Tree,i);
462 
463                      getchar();
464 
465         break;
466 
467  
468 
469         case  2:
470 
471              printf("树的前序遍历(递归):
");
472 
473             preorder(Tree);
474 
475             printf("
");
476 
477         break;
478 
479  
480 
481         case  3:
482 
483              printf("树的后序遍历(递归):
");
484 
485              inoeder(Tree);
486 
487              printf("
");
488 
489         break;
490 
491  
492 
493         case  4:
494 
495              printf("树的前序遍历(非递归):
");
496 
497              preorder2(T);
498 
499              printf("
");
500 
501         break;
502 
503  
504 
505         case  5:
506 
507              printf("树的后序遍历(非递归):
");
508 
509              inoeder2(T);
510 
511              printf("
");
512 
513         break;
514 
515  
516 
517         case  6: 
518 
519              printf("树的层次遍历:
");
520 
521              level(T);
522 
523              printf("
");
524 
525         break;
526 
527  
528 
529         case  0:
530 
531              exit(1);
532 
533         break;     
534 
535     }
536 
537     Menu2();
538 
539     scanf("%d",&c2);
540 
541     if(c2==9)
542 
543           goto loop;
544 
545     else if(c2==0)
546 
547           exit(1);
548 
549    
550 
551 }
552 
553  
原文地址:https://www.cnblogs.com/buyizhiyou/p/5587665.html