数据结构(9) - 二叉树

二叉树(Binary tree)是树形结构的一个重要类型。许多实际问题抽象出来的数据结构往往是二叉树形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显得特别重要。二叉树特点是每个节点最多只能有两棵子树,且有左右之分。

 

 

binary-tree.c

  1 /**
  2  * C data structure binary tree example.
  3  * 
  4  * License - MIT.
  5 */
  6 
  7 #include "binary-tree.h"
  8 
  9 
 10 /**
 11  * bintree_last_traversal - Postorder traversal.
 12 */
 13 int bintree_last_traversal(LPBINTREE lptree)
 14 {
 15     if (NULL != lptree) {
 16         bintree_last_traversal(lptree->lchild);
 17 
 18         bintree_last_traversal(lptree->rchild);
 19 
 20         printf("%c ", lptree->data);
 21     }
 22 
 23     return 0;
 24 }
 25 
 26 
 27 /**
 28  * bintree_mid_traversal - Inorder traversal.
 29 */
 30 int bintree_mid_traversal(LPBINTREE lptree)
 31 {
 32     if (NULL != lptree) {
 33         bintree_mid_traversal(lptree->lchild);
 34 
 35         printf("%c ", lptree->data);
 36 
 37         bintree_mid_traversal(lptree->rchild);
 38     }
 39 
 40     return 0;
 41 }
 42 
 43 
 44 /**
 45  * bintree_pre_traversal - Preorder traversal.
 46 */
 47 int bintree_pre_traversal(LPBINTREE lptree)
 48 {
 49     if (NULL != lptree) {
 50         printf("%c ", lptree->data);
 51 
 52         bintree_pre_traversal(lptree->lchild);
 53 
 54         bintree_pre_traversal(lptree->rchild);
 55     }
 56 
 57     return 0;
 58 }
 59 
 60 
 61 /**
 62  * bintree_init - Create data to binary tree.
 63 */
 64 int bintree_create(LPBINTREE *lptree)
 65 {
 66     char data;
 67 
 68     scanf("%c", &data);
 69 
 70     if ('#' == data) {
 71         *lptree = NULL;
 72     }
 73     else {
 74         *lptree = (LPBINTREE) malloc(sizeof(BINTREE));
 75 
 76         if (NULL == *lptree) {
 77             printf("Error in create.\n");
 78             exit(1);
 79         }
 80 
 81         (*lptree)->data = data;
 82 
 83         bintree_create(&(*lptree)->lchild);
 84 
 85         bintree_create(&(*lptree)->rchild);
 86     }
 87 
 88     return 0;
 89 }
 90 
 91 
 92 /**
 93  * bintree_clear - Delete all data from tree.
 94 */
 95 int bintree_clear(LPBINTREE lptree)
 96 {
 97     if (NULL != lptree) {
 98         bintree_clear(lptree->lchild);
 99 
100         bintree_clear(lptree->rchild);
101 
102         /* 0 == '\0'. */
103         lptree->data   = 0;
104         lptree->lchild = NULL;
105         lptree->rchild = NULL;
106 
107         free(lptree);
108     }
109 
110     return 0;
111 }

binary-tree.h

 1 /**
 2  * C data structure binary tree example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #ifndef __BINARY_TREE_H__
 8 #define __BINARY_TREE_H__
 9 
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdbool.h>
14 
15 
16 typedef struct _BINTREE {
17     char data;
18     struct _BINTREE *lchild, *rchild;
19 } BINTREE, *LPBINTREE;
20 
21 
22 int bintree_last_traversal  (LPBINTREE lptree);
23 int bintree_mid_traversal   (LPBINTREE lptree);
24 int bintree_pre_traversal   (LPBINTREE lptree);
25 int bintree_create          (LPBINTREE *lptree);
26 int bintree_clear           (LPBINTREE lptree);
27 
28 
29 #endif /* __BINARY_TREE_H__ */

main.c

 1 /**
 2  * C data structure binary tree example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #include <stdio.h>
 8 
 9 #include "binary-tree.h"
10 
11 
12 /**
13  * Main function.
14 */
15 int main(void)
16 {
17     LPBINTREE lptree = NULL;
18 
19     /**
20      * Such as: [ABD##E##CF##G##].
21     */
22     printf("Input data:\n");
23     bintree_create(&lptree);
24 
25     printf("Preorder traversal.\n");
26     bintree_pre_traversal(lptree);
27 
28     printf("\nInorder traversal.\n");
29     bintree_mid_traversal(lptree);
30 
31     printf("\nPostorder traversal.\n");
32     bintree_last_traversal(lptree);
33 
34     printf("\n");
35 
36     bintree_clear(lptree);
37 
38     return 0;
39 }

Makefile

 1 # Makefile
 2 CC = gcc
 3 CFLAGS = -Wall -g -O0
 4 
 5 SRC = main.c binary-tree.c
 6 
 7 OBJ = bintree-test
 8 
 9 $(OBJ) : $(SRC)
10     $(CC) $(CFLAGS) -o $@ $^
11 
12 clean:
13     $(RM) $(OBJ) *.o *.*.sw?

 

详细请参考: [Link] [https://github.com/Phoebus-Ma/C-Helper/tree/main/Class-1/Tree.C/tree-binary].

posted @ 2022-06-25 16:05  this毛豆  阅读(34)  评论(0编辑  收藏  举报