|
| 1 | +/*************************************************************************** |
| 2 | + > Filename : 006-btreetraverse.c |
| 3 | + > Author : oneface - one_face@sina.com |
| 4 | + > Company : 一尊还酹江月 |
| 5 | + > Time : 2018-05-02 10:46:21 |
| 6 | + > Description: |
| 7 | +
|
| 8 | + - This program is free software; you can redistribute it and/or modify it |
| 9 | + - under the terms of the GNU General Public License as published by the |
| 10 | + - Free Software Foundation; either version 2 of the License, or (at your |
| 11 | + - option) any later version. |
| 12 | + **************************************************************************/ |
| 13 | +#include <stdio.h> |
| 14 | +#include <stdlib.h> |
| 15 | + |
| 16 | +typedef struct _btree{ |
| 17 | + int da; |
| 18 | + struct _btree *bl, *br; |
| 19 | +}btree; |
| 20 | + |
| 21 | +static void creat_btree(btree **bt) |
| 22 | +{ |
| 23 | + *bt = (btree *)malloc(sizeof(btree)); |
| 24 | + (*bt)->da = 1; |
| 25 | + |
| 26 | + (*bt)->bl = (btree *)malloc(sizeof(btree)); |
| 27 | + (*bt)->br = (btree *)malloc(sizeof(btree)); |
| 28 | + (*bt)->bl->da = 2; |
| 29 | + (*bt)->br->da = 3; |
| 30 | + |
| 31 | + (*bt)->bl->bl = (btree *)malloc(sizeof(btree)); |
| 32 | + (*bt)->bl->br = (btree *)malloc(sizeof(btree)); |
| 33 | + (*bt)->bl->bl->da = 4; |
| 34 | + (*bt)->bl->bl->bl = NULL; |
| 35 | + (*bt)->bl->bl->br = NULL; |
| 36 | + (*bt)->bl->br->da = 5; |
| 37 | + (*bt)->bl->br->bl = NULL; |
| 38 | + (*bt)->bl->br->br = NULL; |
| 39 | + |
| 40 | + (*bt)->br->bl = (btree *)malloc(sizeof(btree)); |
| 41 | + (*bt)->br->br = (btree *)malloc(sizeof(btree)); |
| 42 | + (*bt)->br->bl->da = 6; |
| 43 | + (*bt)->br->bl->bl = NULL; |
| 44 | + (*bt)->br->bl->br = NULL; |
| 45 | + (*bt)->br->br->da = 7; |
| 46 | + (*bt)->br->br->bl = NULL; |
| 47 | + (*bt)->br->br->br = NULL; |
| 48 | +} |
| 49 | + |
| 50 | +static void preorder_traverse(btree *bt) |
| 51 | +{ |
| 52 | + if (bt) { |
| 53 | + printf("%d->", bt->da); |
| 54 | + preorder_traverse(bt->bl); |
| 55 | + preorder_traverse(bt->br); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +static void inorder_traverse(btree *bt) |
| 60 | +{ |
| 61 | + if (bt) { |
| 62 | + inorder_traverse(bt->bl); |
| 63 | + printf("%d->", bt->da); |
| 64 | + inorder_traverse(bt->br); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +static void postorder_traverse(btree *bt) |
| 69 | +{ |
| 70 | + if (bt) { |
| 71 | + postorder_traverse(bt->bl); |
| 72 | + postorder_traverse(bt->br); |
| 73 | + printf("%d->", bt->da); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +int main() |
| 78 | +{ |
| 79 | + btree *bt = NULL; |
| 80 | + |
| 81 | + //http://data.biancheng.net/view/25.html 中图例作为数据来源 |
| 82 | + creat_btree(&bt); |
| 83 | + printf("pre order traverse : "); |
| 84 | + preorder_traverse(bt); |
| 85 | + printf("\nin order traverse : "); |
| 86 | + inorder_traverse(bt); |
| 87 | + printf("\npost order traverse: "); |
| 88 | + postorder_traverse(bt); |
| 89 | + printf("\n"); |
| 90 | + |
| 91 | + free(bt); |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
0 commit comments