当前位置:   article > 正文

【数据结构 - 二叉树】

【数据结构 - 二叉树】

1. 什么是二叉树?

在计算机科学中,二叉树是一种重要的数据结构,它由节点(node)组成,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的设计灵活性使其在各种应用中都有广泛的用途。

2. 基本概念

二叉树有几个关键的概念需要理解:

  • 根节点(Root):树的顶端节点,没有父节点。
  • 子节点(Child):某节点下面的节点。
  • 父节点(Parent):某节点上面的节点。
  • 叶子节点(Leaf):没有子节点的节点。
  • 深度(Depth):从根节点到某节点的唯一路径的长度。
  • 高度(Height):从某节点到一个叶节点最长路径的长度。

3. 树的表示


树结构相对线性表就比较复杂了,要存储表示起来就比较麻烦了,既然保存值域,也要保存结点和结点之间的关系,实际中树有很多种表示方式如:双亲表示法,孩子表示法、孩子双亲表示法以及孩子兄弟表示法等。我们这里就简单的了解其中最常用的孩子兄弟表示法。
 

  1. typedef int DataType;
  2. struct Node
  3. {
  4.  struct Node* firstChild1; // 第一个孩子结点
  5.  struct Node* pNextBrother; // 指向其下一个兄弟结点
  6.  DataType data; // 结点中的数据域
  7. };

4. 二叉树的分类

二叉树有多种类型,每种类型都有其独特的特点和应用场景:

  • 满二叉树:所有叶子节点都在同一层,每个非叶子节点都有两个子节点。
  • 完全二叉树:除了最后一层,其它层的节点都是满的,最后一层的节点集中在左边。
  • 平衡二叉树(AVL树):任意节点的左右子树高度差不超过1,保证了查找、插入和删除的平均时间复杂度为O(log n)。
  • 二叉搜索树(BST):左子树上所有节点的值均小于它的根节点的值,右子树上所有节点的值均大于它的根节点的值,支持高效的查找、插入和删除操作。

5. 二叉树的基本操作

二叉树的基本操作包括:

  • 插入节点:根据规则将新节点插入到合适的位置。
  • 删除节点:删除指定节点,并根据需要调整树的结构。
  • 查找节点:根据指定的值查找节点。

遍历:按照特定顺序访问树的所有节点,包括层次遍历、前序、中序和后序遍历

  1. 1
  2. / \
  3. 2 3
  4. / \ / \
  5. 4 5 6 7
  1. 前序遍历(先根遍历)
    • 访问顺序:根节点 -> 左子树 -> 右子树。
    • 前序遍历的结果为:1 2 4 5 3 6 7
  2. 中序遍历(中根遍历)

    • 访问顺序:左子树 -> 根节点 -> 右子树。
    • 中序遍历的结果为:4 2 5 1 6 3 7
  3. 后序遍历(后根遍历)

    • 访问顺序:左子树 -> 右子树 -> 根节点。
    • 该二叉树的后序遍历结果为:4 5 2 6 7 3 1

代码实现

下面是一个简单的C语言示例,展示如何实现二叉树的前序、中序和后序遍历。我们首先定义二叉树的节点结构 TreeNode 和一些基本操作函数,然后分别实现每种遍历方式的函数。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 二叉树节点结构定义
  4. struct TreeNode {
  5. int data;
  6. struct TreeNode* left;
  7. struct TreeNode* right;
  8. };
  9. // 创建新节点
  10. struct TreeNode* newNode(int data) {
  11. struct TreeNode* node = (struct TreeNode*) malloc(sizeof(struct TreeNode));
  12. node->data = data;
  13. node->left = NULL;
  14. node->right = NULL;
  15. return node;
  16. }
  17. // 前序遍历函数
  18. void preorder(struct TreeNode* root) {
  19. if (root != NULL) {
  20. printf("%d ", root->data); // 访问根节点
  21. preorder(root->left); // 前序遍历左子树
  22. preorder(root->right); // 前序遍历右子树
  23. }
  24. }
  25. // 中序遍历函数
  26. void inorder(struct TreeNode* root) {
  27. if (root != NULL) {
  28. inorder(root->left); // 中序遍历左子树
  29. printf("%d ", root->data); // 访问根节点
  30. inorder(root->right); // 中序遍历右子树
  31. }
  32. }
  33. // 后序遍历函数
  34. void postorder(struct TreeNode* root) {
  35. if (root != NULL) {
  36. postorder(root->left); // 后序遍历左子树
  37. postorder(root->right); // 后序遍历右子树
  38. printf("%d ", root->data); // 访问根节点
  39. }
  40. }
  41. // 主函数
  42. int main() {
  43. // 创建一个示例二叉树
  44. struct TreeNode* root = newNode(1);
  45. root->left = newNode(2);
  46. root->right = newNode(3);
  47. root->left->left = newNode(4);
  48. root->left->right = newNode(5);
  49. printf("前序遍历结果:");
  50. preorder(root);
  51. printf("\n");
  52. printf("中序遍历结果:");
  53. inorder(root);
  54. printf("\n");
  55. printf("后序遍历结果:");
  56. postorder(root);
  57. printf("\n");
  58. return 0;
  59. }
结果输出

运行以上代码,将输出如下结果:

前序遍历结果:1 2 4 5 3

中序遍历结果:4 2 5 1 3

后序遍历结果:4 5 2 3 1

层次遍历

层次遍历需要借助队列来实现,以确保每一层的节点按顺序被访问。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 定义二叉树节点结构
  4. struct TreeNode {
  5. int data;
  6. struct TreeNode* left;
  7. struct TreeNode* right;
  8. };
  9. // 辅助队列节点结构
  10. struct QueueNode {
  11. struct TreeNode* treeNode;
  12. struct QueueNode* next;
  13. };
  14. // 定义队列结构
  15. struct Queue {
  16. struct QueueNode *front, *rear;
  17. };
  18. // 创建新的二叉树节点
  19. struct TreeNode* newNode(int data) {
  20. struct TreeNode* node = (struct TreeNode*) malloc(sizeof(struct TreeNode));
  21. node->data = data;
  22. node->left = NULL;
  23. node->right = NULL;
  24. return node;
  25. }
  26. // 创建空队列
  27. struct Queue* createQueue() {
  28. struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));
  29. queue->front = queue->rear = NULL;
  30. return queue;
  31. }
  32. // 入队操作
  33. void enqueue(struct Queue* queue, struct TreeNode* treeNode) {
  34. struct QueueNode* newNode = (struct QueueNode*) malloc(sizeof(struct QueueNode));
  35. newNode->treeNode = treeNode;
  36. newNode->next = NULL;
  37. if (queue->rear == NULL) {
  38. queue->front = queue->rear = newNode;
  39. return;
  40. }
  41. queue->rear->next = newNode;
  42. queue->rear = newNode;
  43. }
  44. // 出队操作
  45. struct TreeNode* dequeue(struct Queue* queue) {
  46. if (queue->front == NULL)
  47. return NULL;
  48. struct TreeNode* treeNode = queue->front->treeNode;
  49. struct QueueNode* temp = queue->front;
  50. queue->front = queue->front->next;
  51. if (queue->front == NULL)
  52. queue->rear = NULL;
  53. free(temp);
  54. return treeNode;
  55. }
  56. // 层次遍历函数
  57. void levelOrder(struct TreeNode* root) {
  58. if (root == NULL) return;
  59. struct Queue* queue = createQueue();
  60. enqueue(queue, root);
  61. while (queue->front != NULL) {
  62. struct TreeNode* currentNode = dequeue(queue);
  63. printf("%d ", currentNode->data);
  64. if (currentNode->left != NULL)
  65. enqueue(queue, currentNode->left);
  66. if (currentNode->right != NULL)
  67. enqueue(queue, currentNode->right);
  68. }
  69. }
  70. // 主函数
  71. int main() {
  72. // 创建一个示例二叉树
  73. struct TreeNode* root = newNode(1);
  74. root->left = newNode(2);
  75. root->right = newNode(3);
  76. root->left->left = newNode(4);
  77. root->left->right = newNode(5);
  78. printf("层次遍历结果:");
  79. levelOrder(root);
  80. printf("\n");
  81. return 0;
  82. }

层次遍历结果:1 2 3 4 5

6. 二叉树的性质

  1. 5
  2. / \
  3. 3 7
  4. / \ / \
  5. 1 4 6 8

1. 在二叉树的第 i 层上至多有 2^(i - 1) 个节点(i >= 1)。                                                           

  • 例如,在上述二叉树的第 3 层,最 多有 2^(3 - 1) = 4 个节点。

2. 深度为 k 的二叉树至多有 2^k - 1 个节点(k >= 1)

  • 对于这个示例二叉树,深度为 3,节点总数最多为 2^3 - 1 = 7 个。

3. 对任何一棵二叉树,如果其终端节点数为 n0,度为 2 的节点数为 n2,则 n0 = n2 + 1。

7. 实际应用

二叉树在现实世界中有多种应用,例如:

  • 文件系统的目录结构可以使用二叉树来组织。
  • 数据库系统中索引的实现通常采用二叉树或其衍生结构。
  • 编译器中的语法分析阶段可以利用二叉树来表示语法树。

结语

通过本文,我们全面介绍了二叉树的基本概念、分类、基本操作和实际应用。二叉树作为一种核心数据结构,不仅在理论计算机科学中占有重要地位,而且在实际应用中有广泛的运用。深入理解二叉树将有助于提升编程能力和解决实际问题的能力。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/779742
推荐阅读
相关标签
  

闽ICP备14008679号