当前位置:   article > 正文

C语言实现二叉树_c语言2叉树数组实现

c语言2叉树数组实现

C语言实现二叉树

今天我们来介绍一下二叉树,上一节说到堆的实现,即为一种二叉树的顺序结构的应用,通过顺序表来维护堆

二叉树也可以通过链式结构来实现,即二叉链,结构如下图所示。

二叉树的链式存储结构是指,用链表来表示一棵二叉树,即用链来指示元素的逻辑关系。 通常的方法是链表 中每个结点由三个域组成,数据域和左右指针域,左右指针分别用来给出该结点左孩子和右孩子所在的链结 点的存储地址 。

例如下图这棵二叉树:

其二叉链存储表示如下:

下面具体通过代码来详细了解二叉链的实现过程:

  • 二叉树结构定义

  1. typedef char BTDataType;
  2. typedef struct BinaryTreeNode//二叉树节点
  3. {
  4. BTDataType _data;
  5. struct BinaryTreeNode* _left;
  6. struct BinaryTreeNode* _right;
  7. }BTNode;
  8. typedef struct BTree
  9. {
  10. BTNode* _root;
  11. }BTree;
  • 二叉树链表实现接口

  1. // 创建二叉树,返回二叉树的根结点指针
  2. BTNode* BinaryTreeCreate(BTDataType* arr, int idx);
  3. // 二叉树销毁
  4. void BinaryTreeDestory(BTNode** root);
  5. // 二叉树节点个数
  6. int BinaryTreeSize(BTNode* root);
  7. // 二叉树叶子节点个数
  8. int BinaryTreeLeafSize(BTNode* root);
  9. // 二叉树第k层节点个数
  10. int BinaryTreeLevelKSize(BTNode* root, int k);
  11. // 二叉树查找值为x的节点
  12. BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
  13. // 二叉树前序遍历
  14. void BinaryTreePrevOrder(BTNode* root);
  15. // 二叉树中序遍历
  16. void BinaryTreeInOrder(BTNode* root);
  17. // 二叉树后序遍历
  18. void BinaryTreePostOrder(BTNode* root);
  19. // 层序遍历
  20. void BinaryTreeLevelOrder(BTNode* root);
  21. // 判断二叉树是否是完全二叉树
  22. int BinaryTreeComplete(BTNode* root);
  23. //二叉树高度,层数
  24. int BinaryTreeHigh(BTNode* root);
  • 创建二叉树

在创建二叉树的过程中发现,注意:索引如果是局部变量。在递归过程中,索引无法更新
//解决方法:要么定义为全局变量,要么给指针存地址

  1. BTNode* BinaryTreeCreate(BTDataType* arr, int *idx)
  2. {
  3. if (arr[*idx] == '#')
  4. {
  5. (*idx)++;//索引后移
  6. return NULL;//为空返回null
  7. }
  8. BTNode* root = (BTNode*)malloc(sizeof(BTNode));//不为空创建节点,并根据当前索引赋数组值
  9. root->_data = arr[*idx];
  10. (*idx)++;//索引后移
  11. root->_left = BinaryTreeCreate(arr,idx);
  12. root->_right = BinaryTreeCreate(arr,idx);
  13. return root;
  14. }

通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树,为#返回空,否则创建根结点赋值,然后递归遍历创建左子树和右子树,在回溯的过程中,注意索引需要随之变化,要么为全局变量,要么为指针变量(存地址)

  • 二叉树的销毁

  1. void BinaryTreeDestory(BTNode** root)
  2. {
  3. if (*root)
  4. {
  5. BinaryTreeDestory(&((*root)->_left));
  6. BinaryTreeDestory(&((*root)->_right));
  7. free(*root);
  8. *root = NULL;
  9. }
  10. }
  1. void BinaryTreeDestory(BTNode* root)
  2. {
  3. if (root)
  4. {
  5. BinaryTreeDestory(root->_left);
  6. BinaryTreeDestory(root->_right);
  7. free(root);
  8. root = NULL;
  9. }
  10. }

比较上述二者的区别,

代码1 参数为二级指针,指向存放root地址的地址,调用过程中,指针置空,无野指针,

代码2 参数为一级指针,指向root地址,调用过程中,指针没有置空,置空的为指针的拷贝,存在野指针

  • 二叉树的遍历

前序/中序/后序的递归结构遍历:是根据访问结点操作发生位置命名

1. NLR:前序遍历(Preorder Traversal 亦称先序遍历)——访问根结点的操作发生在遍历其左右子树之前。

2. LNR:中序遍历(Inorder Traversal)——访问根结点的操作发生在遍历其左右子树之中(间)。
3. LRN:后序遍历(Postorder Traversal)——访问根结点的操作发生在遍历其左右子树之后。

由于被访问的结点必是某子树的根,所以N(Node)、L(Left subtree)和R(Right subtree)又可解释为 根、根的左子树和根的右子树。NLR、LNR和LRN分别又称为先根遍历、中根遍历和后根遍历。

在二叉树遍历过程中都是递归,回溯的过程:

  1. // 二叉树前序遍历
  2. void BinaryTreePrevOrder(BTNode* root)
  3. {
  4. if (root == NULL)
  5. {
  6. return;
  7. }
  8. printf("%c ",root->_data);
  9. BinaryTreePrevOrder(root->_left);
  10. BinaryTreePrevOrder(root->_right);
  11. }
  12. // 二叉树中序遍历
  13. void BinaryTreeInOrder(BTNode* root)
  14. {
  15. if (root == NULL)
  16. {
  17. return;
  18. }
  19. BinaryTreeInOrder(root->_left);
  20. printf("%c ", root->_data);
  21. BinaryTreeInOrder(root->_right);
  22. }
  23. // 二叉树后序遍历
  24. void BinaryTreePostOrder(BTNode* root)
  25. {
  26. if (root == NULL)
  27. {
  28. return;
  29. }
  30. BinaryTreePostOrder(root->_left);
  31. BinaryTreePostOrder(root->_right);
  32. printf("%c ", root->_data);
  33. }
  • 层序遍历

借助队列或链表,先进先出的原则,通过尾插,头删进行遍历,通过在队列的结构实现层序遍历(按序遍历)

  1. // 层序遍历
  2. void BinaryTreeLevelOrder(BTNode* root)
  3. {
  4. //通过队列尾插头删实现层序遍历
  5. //借助队列保存节点
  6. Queue q;
  7. initQueue(&q);
  8. //根节点存入队列
  9. if (root)
  10. {
  11. queuePush(&q,root);
  12. }
  13. //遍历队列中每一个节点
  14. while (!EmptyQueue(&q))
  15. {
  16. //获取队头元素
  17. BTNode* front = queueFront(&q);
  18. //出队
  19. queuePop(&q);
  20. printf("%c ",front->_data);
  21. //保存队头元素的左右节点
  22. if (front->_left)
  23. {
  24. queuePush(&q,front->_left);
  25. }
  26. if (front->_right)
  27. {
  28. queuePush(&q,front->_right);
  29. }
  30. }
  31. printf("\n");
  32. }
  • 节点数计算(递归回溯)

  1. // 二叉树节点个数
  2. int BinaryTreeSize(BTNode* root)
  3. {
  4. if (root == NULL)
  5. return 0;
  6. return BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right) + 1;
  7. }
  8. // 二叉树叶子节点个数
  9. int BinaryTreeLeafSize(BTNode* root)
  10. {
  11. //空树 为0
  12. if (root == NULL)
  13. {
  14. return 0;
  15. }
  16. //叶子节点 为1
  17. if (root->_left == NULL && root->_right == NULL)
  18. return 1;
  19. //非叶子 左子树叶子+右子树叶子
  20. return BinaryTreeLeafSize(root->_left)+BinaryTreeLeafSize(root->_right);
  21. }
  22. // 二叉树第k层节点个数
  23. //第k层节点个数=左右子树第k-1层节点个数和
  24. //假设根为第一层
  25. int BinaryTreeLevelKSize(BTNode* root, int k)
  26. {
  27. if (root == NULL)
  28. return 0;
  29. if (k == 1)
  30. {
  31. return 1;
  32. }
  33. return BinaryTreeLevelKSize(root->_left,k-1) + BinaryTreeLevelKSize(root->_right,k-1);
  34. }
  • 二叉树高度

  1. //二叉树高度,层数
  2. int BinaryTreeHigh(BTNode* root)
  3. {
  4. if (root == NULL)
  5. return 0;
  6. int left = BinaryTreeHigh(root->_left);
  7. int right = BinaryTreeHigh(root->_right);
  8. return left > right ? left + 1 : right + 1;
  9. }
  • 二叉树查找指定节点

  1. // 二叉树查找值为x的节点
  2. BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
  3. {
  4. if (root)
  5. {
  6. if (root->_data == x)
  7. {
  8. return root;
  9. }
  10. if (BinaryTreeFind(root->_left, x))
  11. {
  12. return BinaryTreeFind(root->_left, x);
  13. }
  14. else
  15. return BinaryTreeFind(root->_right, x);
  16. }
  • 判断是否为完全二叉树

  1. // 判断二叉树是否是完全二叉树
  2. int BinaryTreeComplete(BTNode* root)
  3. {
  4. //借助队列保存节点
  5. Queue q;
  6. initQueue(&q);
  7. //根节点存入队列
  8. if (root)
  9. {
  10. queuePush(&q, root);
  11. }
  12. //遍历队列中每一个节点
  13. while (!EmptyQueue(&q))
  14. {
  15. //获取队头元素
  16. BTNode* front = queueFront(&q);
  17. //出队
  18. queuePop(&q);
  19. //此时无需判断左右孩子是否存在,只要当前节点存在,直接入,即使为空节点 作为后续判断依据
  20. if (front)
  21. {
  22. queuePush(&q, front->_left);
  23. queuePush(&q, front->_right);
  24. }
  25. else
  26. break;
  27. }
  28. //剩余元素中,是否存在非空节点
  29. while (!EmptyQueue(&q))
  30. {
  31. BTNode* front = queueFront(&q);
  32. queuePop(&q);
  33. if (front)
  34. {
  35. //如果剩余节点中存在非空节点 则说明该节点不连续
  36. return 0;
  37. }
  38. }
  39. return 1;
  40. }

基于层序遍历的思想,当遍历到空节点时停止,遍历剩余元素,如果剩余元素中存在非空节点,说明节点不连续,为非完全二叉树。

  • 测试代码

  1. #include<stdio.h>
  2. #include"BinaryTree.h"
  3. #include"queue.h"
  4. void test()
  5. {
  6. char arr[] = "ABD##E#H##CF##G##";
  7. int idx = 0;
  8. //创建二叉树
  9. BTNode* root = BinaryTreeCreate(arr,&idx);
  10. //前序遍历
  11. printf("前序遍历: ");
  12. BinaryTreePrevOrder(root);
  13. printf("\n");
  14. //中序遍历
  15. printf("中序遍历: ");
  16. BinaryTreeInOrder(root);
  17. printf("\n");
  18. //后序遍历
  19. printf("后序遍历: ");
  20. BinaryTreePostOrder(root);
  21. printf("\n");
  22. printf("层序遍历:");
  23. BinaryTreeLevelOrder(root);
  24. printf("\n");
  25. printf("树高: %d\n", BinaryTreeHigh(root));
  26. printf("树节点数: %d\n",BinaryTreeSize(root));
  27. printf("叶子节点数: %d\n", BinaryTreeLeafSize(root));
  28. printf("第%d层子节点数: %d\n",3, BinaryTreeLevelKSize(root,3));
  29. int k = BinaryTreeComplete(root);
  30. printf("%d ",k);
  31. if (k)
  32. printf("完全二叉树\n");
  33. else
  34. printf("非完全二叉树\n");
  35. }
  36. int main()
  37. {
  38. test();
  39. return 0;
  40. }
  • 测试结果

 

 

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

闽ICP备14008679号