0


数据结构(二叉树-2)

文章目录

一、 实现链式结构二叉树

  1.1 Tree.h

  1.2 Tree.c 前中后序遍历

    前序遍历

    中序遍历

    后续遍历

  1.2 Tree.c 结点个数

  1.3Tree.c 叶子节点个数

  1.4 Tree.c 二叉树的高度

  1.5 Tree.c 层序遍历

  1.6 判断是否为完全二叉树

  1.7 销毁二叉树

  test.c


一、 实现链式结构二叉树

⽤链表来表⽰⼀棵⼆叉树,即⽤链来指⽰元素的逻辑关系。 通常的⽅法是链表中每个结点由三个域组成,数据域和左右指针域,左右指针分别⽤来给出该结点左孩⼦和右孩⼦所在的链结点的存储地址 ,其结构如下:

1.1 Tree.h

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<assert.h>
  4. #include<stdbool.h>
  5. //定义二叉树的链式结构
  6. //二叉树结点的结构
  7. typedef int BTDataType;
  8. typedef struct BinaryTreeNode
  9. {
  10. int data;
  11. struct BinaryTreeNode* left;
  12. struct BinaryTreeNode* right;
  13. }BTNode;
  14. //前序遍历
  15. void PreOrder(BTNode* root);
  16. //中序遍历
  17. void InOrder(BTNode* root);
  18. //后序遍历
  19. void PostOrder(BTNode* root);
  20. // ⼆叉树结点个数
  21. int BinaryTreeSize(BTNode* root);
  22. // ⼆叉树叶⼦结点个数
  23. int BinaryTreeLeafSize(BTNode* root);
  24. // ⼆叉树第k层结点个数
  25. int BinaryTreeLevelKSize(BTNode* root, int k);
  26. //⼆叉树的深度/⾼度
  27. int BinaryTreeDepth(BTNode* root);
  28. // ⼆叉树查找值为x的结点
  29. BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
  30. // ⼆叉树销毁
  31. void BinaryTreeDestory(BTNode** root);
  32. //层序遍历
  33. void LevelOrder(BTNode* root);
  34. //判断二叉树是否为完全二叉树
  35. bool BinaryTreeComplete(BTNode* root);

下面我们可以在下方 test.c 文件中手动创建一棵链式二叉树,方便对链式结构二叉树遍历方式以及各种方法的实现的研究:

1.2 Tree.c 前中后序遍历

  1. 前序遍历(Preorder Traversal 亦称先序遍历):访问根结点的操作发⽣在遍历其左右⼦树之前 访问顺序为:根结点、左⼦树、右⼦树
  2. 中序遍历(Inorder Traversal):访问根结点的操作发⽣在遍历其左右⼦树之中(间) 访问顺序为:左⼦树、根结点、右⼦树
  3. 后序遍历(Postorder Traversal):访问根结点的操作发⽣在遍历其左右⼦树之后 访问顺序为:左⼦树、右⼦树、根结点
前序遍历
  1. void PreOrder(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. return;
  6. }
  7. printf("%d", root->data);
  8. PreOrder(root->left);
  9. PreOrder(root->right);
  10. }

输出结果为: 1 2 4 3

其他两种遍历方式的递归过程也是一样的做法,下面就只展示代码部分了。

中序遍历
  1. void InOrder(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. return;
  6. }
  7. InOrder(root->left);
  8. printf("%d", root->data);
  9. InOrder(root->right);
  10. }

输出结果为: 4 2 1 3

后续遍历
  1. void PosOrder(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. return;
  6. }
  7. PostOrder(root->left);
  8. PostOrder(root->right);
  9. printf("%d", root->data);
  10. }

输出结果为: 4 2 3 1

1.2 Tree.c 结点个数

  1. int BinaryTreeSize(BTNode* root)
  2. {
  3. if (root == NULL);
  4. {
  5. return 0;
  6. }
  7. return 1 + BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
  8. }

1.3Tree.c 叶子节点个数

  1. int BinaryTreeLeafSize(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. return 0;
  6. }
  7. if (root->left == NULL && root->right == NULL)
  8. {
  9. return 1;
  10. }
  11. return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
  12. }
  • 当一棵二叉树中的节点的左右孩都为空时,证明他就是最后的结点,也叫做 “叶子节点” ,上述代码就运用了这一特点进行函数递归 ,if (root->left == NULL && root->right == NULL) 成立则返回 1 ,不成立就继续对其左右孩创建函数栈帧进行递归。

1.4 Tree.c 二叉树的高度

  1. int BinaryTreeDepth(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. return 0;
  6. }
  7. int lefttree = BinaryTreeDepth(root->left);
  8. int righttree = BinaryTreeDepth(root->right);
  9. return lefttree > righttree ? lefttree + 1 : righttree + 1;
  10. }
  • 此处的递归方式与查找节点个数的递归方式相同,但是这里是求树的高度,所以当我们返回根节点的左右孩子的节点个数时(也就是求出左孩和右孩分别的高度),要进行比较,选出较高的树的高度,此高度就是整棵树的高度。

1.5 Tree.c 层序遍历

在上述构建好的二叉树中,如果要对其进行层序遍历的话,期待输出的结果为 1 2 3 4 ,

此时我们可以借助队列这一数据结构实现,所以在编写代码时先要包括 “Queue.h” 的头文件,具体队列的实现可以参考 数据结构(队列)-CSDN博客。

  1. void LevelOrder(BTNode* root)
  2. {
  3. Queue q;
  4. QueueInit(&q);
  5. QueuePush(&q, root);
  6. while (!QueueEmpty(&q))
  7. {
  8. //取队头,打印
  9. BTNode* front = QueueFront(&q);
  10. printf("%d ", front->data);
  11. QueuePop(&q);
  12. //队头节点的左右孩子入队列
  13. if (front->left)
  14. QueuePush(&q, front->left);
  15. if (front->right)
  16. QueuePush(&q, front->right);
  17. }
  18. //队列为空
  19. QueueDestroy(&q);
  20. }

1.6 判断是否为完全二叉树

  1. bool BinaryTreeComplete(BTNode* root)
  2. {
  3. Queue q;
  4. QueueInit(&q);
  5. QueuePush(&q, root);
  6. while (!QueueEmpty(&q))
  7. {
  8. BTNode* front = QueueFront(&q);
  9. QueuePop(&q);
  10. if (front == NULL)
  11. {
  12. break;
  13. }
  14. QueuePush(&q, front->left);
  15. QueuePush(&q, front->right);
  16. }
  17. while (!QueueEmpty(&q))
  18. {
  19. if (front != NULL)
  20. {
  21. QueueDestory(&q);
  22. return false;
  23. }
  24. }
  25. QueueDestory(&q);
  26. return true;
  27. }

  • 如果是完全二叉树,跳出一个循环之后队列中剩下的全是NULL节点;
  • 如果不是完全二叉树,跳出一个循环之后队列还有非空节点。

1.7 销毁二叉树

  1. void BinaryTreeDestory(BTNode** root)
  2. {
  3. if (*root == NULL)
  4. {
  5. return;
  6. }
  7. BinaryTreeDestory(&((*root)->left));
  8. BinaryTreeDestory(&((*root)->right));
  9. free(*root);
  10. *root = NULL;
  11. }
  • 销毁左子树 + 销毁右子树 + 销毁根节点

test.c

  1. #include"Tree.h"
  2. BTNode* buyNode(BTDataType x)
  3. {
  4. //创建结点
  5. BTNode* newnode = (BTNode*)malloc(sizeof(BTDataType));
  6. if (newnode == NULL)
  7. {
  8. perror("malloc fail!");
  9. exit(1);
  10. }
  11. newnode->data = x;
  12. newnode->left = newnode->right = NULL;
  13. return newnode;
  14. }
  15. void treetest()
  16. {
  17. BTNode* node1 = buyNode(1);
  18. BTNode* node2 = buyNode(2);
  19. BTNode* node3 = buyNode(3);
  20. BTNode* node4 = buyNode(4);
  21. node1->left = node2;
  22. node1->right = node3;
  23. node2->light = node4;
  24. }
  25. int main()
  26. {
  27. treetest();
  28. return 0;
  29. }

未完待续~~


本文转载自: https://blog.csdn.net/2401_83968713/article/details/140596081
版权归原作者 退1️⃣码头 所有, 如有侵权,请联系我们删除。

“数据结构(二叉树-2)”的评论:

还没有评论