• 二叉树的链式结构及遍历


    目录

    1. 定义

    2. 二叉树的遍历 

    2.1 前序、中序以及后序遍历

    2.1.1 前序遍历

    2.1.2 中序遍历

    2.1.3 后序遍历 

    2.2 二叉树的层序遍历

    3. 二叉树节点个数及高度等

    3.1 求节点个数

    3.2 求叶子节点个数

    3.3 求第K层节点个数

    3.4 求二叉树深度

    3.5 二叉树查找值为X的节点


    1. 定义

    1. typedef int BTDataType;
    2. typedef struct BinaryTreeNode
    3. {
    4. BTDataType _data;
    5. struct BinaryTreeNode* _left;
    6. struct BinaryTreeNode* _right;
    7. }BTNode;

    回顾下二叉树的概念,二叉树是:
    1. 空树
    2. 非空:根节点,根节点的左子树、根节点的右子树组成的

    从概念中可以看出,二叉树定义是递归式的,因此后序基本操作中基本都是按照该概念实现的

    2. 二叉树的遍历 

    2.1 前序、中序以及后序遍历

           学习二叉树结构,最简单的方式就是遍历。所谓二叉树遍历(Traversal)是按照某种特定的规则,依次对二叉树中的节点进行相应的操作,并且每个节点只操作一次。

    按照规则,二叉树的遍历有:前序/中序/后序的递归结构遍历

    前序遍历: 根        左子树        右子树

    中序遍历: 左子树        根        右子树

    后序遍历: 左子树        右子树        根

    注:二叉树的前中后遍历是根据 根节点 的遍历位置判断的

    举例:

    2.1.1 前序遍历

    1. void PreOrder(BTNode* root)
    2. {
    3. if (root == NULL)//便于测试观察
    4. {
    5. printf("# ");
    6. return;
    7. }
    8. printf("%d ", root->data);
    9. PreOrder(root->left);
    10. PreOrder(root->right);
    11. }

    2.1.2 中序遍历

    1. void InOrder(BTNode* root){
    2. if (root == NULL)//便于测试观察
    3. {
    4. printf("# ");
    5. return;
    6. }
    7. InOrder(root->left);
    8. printf("%d ", root->data);
    9. InOrder(root->right);
    10. }

    2.1.3 后序遍历 

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

    2.2 二叉树的层序遍历

            思路:运用队列,将根节点入队列,当队列不为空的时候,将根节点出队列,同时将左右子树根节点入队。

    1. void BinaryTreeLevelOrder(BTNode* root)
    2. {
    3. Queue q;
    4. QueueInit(&q);
    5. if (root)
    6. {
    7. QueuePush(&q, root);
    8. }
    9. while (!QueueEmpty(&q))
    10. {
    11. BTNode* front = QueueFront(&q);
    12. printf("%d ", front->data);
    13. QueuePop(&q);
    14. if (front->left)
    15. {
    16. QueuePush(&q, front->left);
    17. }
    18. if (front->right)
    19. {
    20. QueuePush(&q, front->right);
    21. }
    22. }
    23. printf("\n");
    24. QueueDestroy(&q);
    25. }


    3. 二叉树节点个数及高度等

    3.1 求节点个数

    1. int TreeSize(BTNode* root)
    2. {
    3. return root == NULL ? 0 :
    4. TreeSize(root->left) + TreeSize(root->right) + 1;
    5. }

    3.2 求叶子节点个数

    1. int TreeLeafSize(BTNode* root)
    2. {
    3. if (root == NULL)//空节点
    4. return 0;
    5. if (root->left== NULL && root->right == NULL)//叶子节点
    6. return 1;
    7. //非空非叶子返回 左子树叶子节点+右子树叶子节点
    8. return TreeLeafSize(root->left) + TreeLeafSize(root->right);
    9. }

    3.3 求第K层节点个数

    1. int TreeKLevel(BTNode* root, int k)
    2. {
    3. assert(k >= 1);
    4. if (root == NULL)//空节点
    5. return 0;
    6. if (k == 1)//递归到了第K层,遇到节点+1
    7. return 1;
    8. //非空非K,返回左子树的K-1层节点数+右子树的K-1层节点数
    9. return TreeKLevel(root->left, k - 1)
    10. + TreeKLevel(root->right, k - 1);
    11. }

    3.4 求二叉树深度

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

    3.5 二叉树查找值为X的节点

    1. BTNode* TreeFind(BTNode* root, BTDataType x)
    2. {
    3. if (root == NULL)
    4. return NULL;
    5. if (root->data == x)
    6. return root;
    7. BTNode* ret1 = TreeFind(root->left, x);
    8. if (ret1)
    9. return ret1;
    10. BTNode* ret2 = TreeFind(root->right, x);
    11. if (ret2)
    12. return ret2;
    13. return NULL;
    14. }

  • 相关阅读:
    Autojs微信研究:微信自动发送信息机器人最终成品(有效果演示)
    电脑开机蓝屏错误代码c000021a怎么办 电脑蓝屏报错c000021a的解决办法
    基于.Net开发的数据库导入导出的开源项目
    华为浏览器,Chrome的平替,插件无缝连接
    弃用http改用https的缘故,与密钥的使用,证书意义
    一文了解AIGC与ChatGPT
    python变量名解析总结
    类和对象(上)
    Cat1模组蓄“光”发展,广和通全场景助力光伏行业零碳发电
    c语言:于龙加
  • 原文地址:https://blog.csdn.net/blue_jun_/article/details/126879241