2. 二叉树

2.1. 二叉树的基本概念

链表的每个节点可以有一个后继,而二叉树(Binary Tree)的每个节点可以有两个后继。比如这样定义二叉树的节点:

  1. typedef struct node *link;
  2. struct node {
  3. unsigned char item;
  4. link l, r;
  5. };

这样的节点可以组织成下图所示的各种形态。

图 26.9. 二叉树的定义和举例

二叉树的定义和举例

二叉树可以这样递归地定义:

  1. 就像链表有头指针一样,每个二叉树都有一个根指针(上图中的root指针)指向它。根指针可以是NULL,表示空二叉树,或者

  2. 根指针可以指向一个节点,这个节点除了有数据成员之外还有两个指针域,这两个指针域又分别是另外两个二叉树(左子树和右子树)的根指针。

上图举例示意了几种情况。

  • 单节点的二叉树:左子树和右子树都是空二叉树。

  • 只有左子树的二叉树:右子树是空二叉树。

  • 只有右子树的二叉树:左子树是空二叉树。

  • 一般的二叉树:左右子树都不为空。注意右侧由圈和线段组成的简化图示,以后我们都采用这种简化图示法,在圈中标上该节点数据成员的值。

链表的遍历方法是显而易见的:从前到后遍历即可。二叉树是一种树状结构,如何做到把所有节点都走一遍不重不漏呢?有以下几种方法:

图 26.10. 二叉树的遍历

二叉树的遍历

前序(Pre-order Traversal)、中序(In-order Traversal)、后序遍历(Post-order Traversal)和深度优先搜索的顺序类似,层序遍历(Level-order Traversal)和广度优先搜索的顺序类似。

前序和中序遍历的结果合在一起可以唯一确定二叉树的形态,也就是说根据遍历结果可以构造出二叉树。过程如下图所示:

图 26.11. 根据前序和中序遍历结果构造二叉树

根据前序和中序遍历结果构造二叉树

想一想,根据中序和后序遍历结果能否构造二叉树?根据前序和后序遍历结果能否构造二叉树?

例 26.3. 二叉树

  1. /* binarytree.h */
  2. #ifndef BINARYTREE_H
  3. #define BINARYTREE_H
  4.  
  5. typedef struct node *link;
  6. struct node {
  7. unsigned char item;
  8. link l, r;
  9. };
  10.  
  11. link init(unsigned char VLR[], unsigned char LVR[], int n);
  12. void pre_order(link t, void (*visit)(link));
  13. void in_order(link t, void (*visit)(link));
  14. void post_order(link t, void (*visit)(link));
  15. int count(link t);
  16. int depth(link t);
  17. void destroy(link t);
  18.  
  19. #endif
  1. /* binarytree.c */
  2. #include <stdlib.h>
  3. #include "binarytree.h"
  4.  
  5. static link make_node(unsigned char item)
  6. {
  7. link p = malloc(sizeof *p);
  8. p->item = item;
  9. p->l = p->r = NULL;
  10. return p;
  11. }
  12.  
  13. static void free_node(link p)
  14. {
  15. free(p);
  16. }
  17.  
  18. link init(unsigned char VLR[], unsigned char LVR[], int n)
  19. {
  20. link t;
  21. int k;
  22. if (n <= 0)
  23. return NULL;
  24. for (k = 0; VLR[0] != LVR[k]; k++);
  25. t = make_node(VLR[0]);
  26. t->l = init(VLR+1, LVR, k);
  27. t->r = init(VLR+1+k, LVR+1+k, n-k-1);
  28. return t;
  29. }
  30.  
  31. void pre_order(link t, void (*visit)(link))
  32. {
  33. if (!t)
  34. return;
  35. visit(t);
  36. pre_order(t->l, visit);
  37. pre_order(t->r, visit);
  38. }
  39.  
  40. void in_order(link t, void (*visit)(link))
  41. {
  42. if (!t)
  43. return;
  44. in_order(t->l, visit);
  45. visit(t);
  46. in_order(t->r, visit);
  47. }
  48.  
  49. void post_order(link t, void (*visit)(link))
  50. {
  51. if (!t)
  52. return;
  53. post_order(t->l, visit);
  54. post_order(t->r, visit);
  55. visit(t);
  56. }
  57.  
  58. int count(link t)
  59. {
  60. if (!t)
  61. return 0;
  62. return 1 + count(t->l) + count(t->r);
  63. }
  64.  
  65. int depth(link t)
  66. {
  67. int dl, dr;
  68. if (!t)
  69. return 0;
  70. dl = depth(t->l);
  71. dr = depth(t->r);
  72. return 1 + (dl > dr ? dl : dr);
  73. }
  74.  
  75. void destroy(link t)
  76. {
  77. post_order(t, free_node);
  78. }
  1. /* main.c */
  2. #include <stdio.h>
  3. #include "binarytree.h"
  4.  
  5. void print_item(link p)
  6. {
  7. printf("%d", p->item);
  8. }
  9.  
  10. int main()
  11. {
  12. unsigned char pre_seq[] = { 4, 2, 1, 3, 6, 5, 7 };
  13. unsigned char in_seq[] = { 1, 2, 3, 4, 5, 6, 7 };
  14. link root = init(pre_seq, in_seq, 7);
  15. pre_order(root, print_item);
  16. putchar('\n');
  17. in_order(root, print_item);
  18. putchar('\n');
  19. post_order(root, print_item);
  20. putchar('\n');
  21. printf("count=%d depth=%d\n", count(root), depth(root));
  22. destroy(root);
  23. return 0;
  24. }

习题

1、本节描述了二叉树的递归定义,想一想单链表的递归定义应该怎么表述?请仿照本节的例子用递归实现单链表的各种操作函数:

  1. link init(unsigned char elements[], int n);
  2. void pre_order(link t, void (*visit)(link));
  3. void post_order(link t, void (*visit)(link));
  4. int count(link t);
  5. void destroy(link t);

2.2. 排序二叉树

排序二叉树(BST,Binary Search Tree)具有这样的性质:对于二叉树中的任意节点,如果它有左子树或右子树,则该节点的数据成员大于左子树所有节点的数据成员,且小于右子树所有节点的数据成员。排序二叉树的中序遍历结果是从小到大排列的,其实上一节的图 26.10 “二叉树的遍历”就是排序二叉树。

例 26.4. 排序二叉树

  1. /* bst.h */
  2. #ifndef BST_H
  3. #define BST_H
  4.  
  5. typedef struct node *link;
  6. struct node {
  7. unsigned char item;
  8. link l, r;
  9. };
  10.  
  11. link search(link t, unsigned char key);
  12. link insert(link t, unsigned char key);
  13. link delete(link t, unsigned char key);
  14. void print_tree(link t);
  15.  
  16. #endif
  1. /* bst.c */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "bst.h"
  5.  
  6. static link make_node(unsigned char item)
  7. {
  8. link p = malloc(sizeof *p);
  9. p->item = item;
  10. p->l = p->r = NULL;
  11. return p;
  12. }
  13.  
  14. static void free_node(link p)
  15. {
  16. free(p);
  17. }
  18.  
  19. link search(link t, unsigned char key)
  20. {
  21. if (!t)
  22. return NULL;
  23. if (t->item > key)
  24. return search(t->l, key);
  25. if (t->item < key)
  26. return search(t->r, key);
  27. /* if (t->item == key) */
  28. return t;
  29. }
  30.  
  31. link insert(link t, unsigned char key)
  32. {
  33. if (!t)
  34. return make_node(key);
  35. if (t->item > key) /* insert to left subtree */
  36. t->l = insert(t->l, key);
  37. else /* if (t->item <= key), insert to right subtree */
  38. t->r = insert(t->r, key);
  39. return t;
  40. }
  41.  
  42. link delete(link t, unsigned char key)
  43. {
  44. link p;
  45. if (!t)
  46. return NULL;
  47. if (t->item > key) /* delete from left subtree */
  48. t->l = delete(t->l, key);
  49. else if (t->item < key) /* delete from right subtree */
  50. t->r = delete(t->r, key);
  51. else { /* if (t->item == key) */
  52. if (t->l == NULL && t->r == NULL) { /* if t is leaf node */
  53. free_node(t);
  54. t = NULL;
  55. } else if (t->l) { /* if t has left subtree */
  56. /* replace t with the rightmost node in left subtree */
  57. for (p = t->l; p->r; p = p->r);
  58. t->item = p->item;
  59. t->l = delete(t->l, t->item);
  60. } else { /* if t has right subtree */
  61. /* replace t with the leftmost node in right subtree */
  62. for (p = t->r; p->l; p = p->l);
  63. t->item = p->item;
  64. t->r = delete(t->r, t->item);
  65. }
  66. }
  67. return t;
  68. }
  69.  
  70. void print_tree(link t)
  71. {
  72. if (t) {
  73. printf("(");
  74. printf("%d", t->item);
  75. print_tree(t->l);
  76. print_tree(t->r);
  77. printf(")");
  78. } else
  79. printf("()");
  80. }
  1. /* main.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include "bst.h"
  6.  
  7. #define RANGE 100
  8. #define N 6
  9.  
  10. void print_item(link p)
  11. {
  12. printf("%d", p->item);
  13. }
  14.  
  15. int main()
  16. {
  17. int i, key;
  18. link root = NULL;
  19. srand(time(NULL));
  20. for (i = 0; i < N; i++)
  21. root = insert(root, rand() % RANGE);
  22. printf("\t\\tree");
  23. print_tree(root);
  24. printf("\n\n");
  25. while (root) {
  26. key = rand() % RANGE;
  27. if (search(root, key)) {
  28. printf("delete %d in tree\n", key);
  29. root = delete(root, key);
  30. printf("\t\\tree");
  31. print_tree(root);
  32. printf("\n\n");
  33. }
  34. }
  35. }
  1. $ ./a.out
  2. \tree(83(77(15()(35()()))())(86()(93()())))
  3.  
  4. delete 86 in tree
  5. \tree(83(77(15()(35()()))())(93()()))
  6.  
  7. delete 35 in tree
  8. \tree(83(77(15()())())(93()()))
  9.  
  10. delete 93 in tree
  11. \tree(83(77(15()())())())
  12.  
  13. delete 15 in tree
  14. \tree(83(77()())())
  15.  
  16. delete 83 in tree
  17. \tree(77()())
  18.  
  19. delete 77 in tree
  20. \tree()

程序的运行结果可以用Greg Lee编写的The Tree Preprocessor(http://www.essex.ac.uk/linguistics/clmt/latex4ling/trees/tree/)转换成树形:

  1. $ ./a.out | ./tree/tree
  2. 83
  3. ___|___
  4. | |
  5. 77 86
  6. _|__ _|__
  7. | | | |
  8. 15 93
  9. _|__ _|__
  10. | | | |
  11. 35
  12. _|__
  13. | |
  14.  
  15. delete 86 in tree
  16. 83
  17. ___|___
  18. | |
  19. 77 93
  20. _|__ _|__
  21. | | | |
  22. 15
  23. _|__
  24. | |
  25. 35
  26. _|__
  27. | |
  28.  
  29. delete 35 in tree
  30. 83
  31. ___|___
  32. | |
  33. 77 93
  34. _|__ _|__
  35. | | | |
  36. 15
  37. _|__
  38. | |
  39.  
  40. delete 93 in tree
  41. 83
  42. _|__
  43. | |
  44. 77
  45. _|__
  46. | |
  47. 15
  48. _|__
  49. | |
  50.  
  51. delete 15 in tree
  52. 83
  53. _|__
  54. | |
  55. 77
  56. _|__
  57. | |
  58.  
  59. delete 83 in tree
  60. 77
  61. _|__
  62. | |
  63.  
  64. delete 77 in tree