当前位置:   article > 正文

【数据结构】——链式二叉树

【数据结构】——链式二叉树

目录

一、链式二叉树的定义结构

二、链式二叉树的遍历

2.1前序遍历

2.2中序遍历

2.3后序遍历

2.4层序遍历

三、链式二叉树的基本功能函数

3.1结点个数

3.2叶子结点个数

3.3二叉树第k层结点个数

3.4查找值为x的结点

3.5二叉树的销毁

四、基础OJ

4.1二叉树遍历

4.2左叶子的和

4.3翻转二叉树


一、链式二叉树的定义结构

<1>二叉树的结构体 包含了数据和指向左右子树的指针

  1. typedef int BTDataType;
  2. typedef struct BinaryTreeNode
  3. {
  4. BTDataType data;
  5. struct BinaryTreeNode* left;
  6. struct BinaryTreeNode* right;
  7. }BTNode;

<2>二叉树结点的创建

  1. BTNode* BuyNode(BTDataType x)
  2. {
  3. BTNode* root = (BTNode*)malloc(sizeof(BTNode));
  4. if (root == NULL)
  5. {
  6. perror("malloc fail");
  7. return NULL;
  8. }
  9. root->data = x;
  10. root->left = NULL;
  11. root->right = NULL;
  12. return root;
  13. }

<3>二叉树的创建

  1. BTNode* CreatBinaryTree()
  2. {
  3. BTNode* node1 = BuyNode(1);
  4. BTNode* node2 = BuyNode(2);
  5. BTNode* node3 = BuyNode(3);
  6. BTNode* node4 = BuyNode(4);
  7. BTNode* node5 = BuyNode(5);
  8. BTNode* node6 = BuyNode(6);
  9. node1->left = node2;
  10. node1->right = node4;
  11. node2->left = node3;
  12. node4->left = node5;
  13. node4->right = node6;
  14. return node1;
  15. }

Tip:二叉树是递归定义的,都可以单独看作 根、左子树、右子树

二、链式二叉树的遍历

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

遍历是二叉树上最重要的运算之一,也是二叉树上进行其它运算的基础。

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

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

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

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

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

2.1前序遍历

前序遍历就是按照 根、左子树、右子树的顺序实现遍历

然后左、右子树又按照新的树继续递归下去

值得注意的是不能忘记 3 这个叶子二叉树 它的左子树右子树均为空 但是也是需要访问的

代码实现:

<1>树为空 return结束

<2>树不为空 访问结点,递归左子树、右子树

  1. void PrevOrder(BTNode*root)
  2. {
  3. if (root == NULL)
  4. {
  5. printf("N ");
  6. return;
  7. }
  8. printf("%d ", root->data);//根
  9. PrevOrder(root->left);//左子树
  10. PrevOrder(root->right);//右子树
  11. }

逻辑分析过程:

逻辑上将一颗树的前序遍历分为 根的访问和左子树的遍历和右子树的遍历。
左右子树的遍历又看成新的一棵树整体的前序遍历。所以我们递归左右子树即可。

物理过程:

函数的调用会创建函数栈帧空间

2.2中序遍历

对比前序遍历 都是整体式的递归访问 但是中序遍历顺序为左子树、根、右子树

代码实现:

  1. void InOrder(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. printf("N ");
  6. return;
  7. }
  8. InOrder(root->left);//左子树
  9. printf("%d ", root->data);//根
  10. InOrder(root->right);//右子树
  11. }

可以看到这里的访问就是打印数据 前序遍历根的访问在前 (因为是 根 左子树 右子树)而中序遍历 先递归到最小的整体单元 (左子树 根 右子树)

2.3后序遍历

与前中序遍历同理,顺序为 左子树 右子树 根

代码实现:

  1. void PostOrder(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. printf("N ");
  6. return;
  7. }
  8. PostOrder(root->left);//左子树
  9. PostOrder(root->right);//右子树
  10. printf("%d ", root->data);///根
  11. }

验证一下数据

2.4层序遍历

层序遍历:除了前序遍历、中序遍历、后序遍历外,还可以对二叉树进行层序遍历。设二叉树的根结点所在层数为1,层序遍历就是从所在二叉树的根结点出发,首先访问第一层的树根结点,然后从左到右访问第2层 上的结点,接着是第三层的结点,以此类推,自上而下,自左至右逐层访问树的结点的过程就是层序遍历。


分析:

代码实现:

  1. void TreeLvelOrder(BTNode* root)
  2. {
  3. Queue q;
  4. QueueInit(&q);
  5. if (root != NULL)
  6. {
  7. QueuePush(&q, root);
  8. }
  9. while (!QueueEmpty(&q))
  10. {
  11. //获取队头数据 先进先出 层序遍历
  12. BTNode* front = QueueFront(&q);
  13. QueuePop(&q);
  14. printf("%d ", front->data);
  15. if(front->left)
  16. QueuePush(&q, front->left);
  17. if(front->right)
  18. QueuePush(&q, front->right);
  19. }
  20. QueueDestory(&q);
  21. }

BFS:广度优先遍历(层序遍历)

DFS:深度优先遍历(前中后序遍历)

三、链式二叉树的基本功能函数

3.1结点个数

<1>遍历二叉树 size++

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

定义全局静态的size,遍历不为空++,缺点调用必须置零size 过于繁琐

<2>分治递归思想 拆成若干个单元整体

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

3.2叶子结点个数

叶子结点的特征 左右子树为空 return 1

  1. int BinaryTreeLeafSize(BTNode* root)
  2. {
  3. //如果没有这个 会导致空指针的引用报错
  4. if (root == NULL)
  5. return 0;
  6. if (root->left == NULL && root->right== NULL)
  7. return 1;
  8. return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
  9. }

3.3二叉树第k层结点个数

分析:如何找到返回条件和子问题呢?

  1. int BinaryTreeLevelKSize(BTNode* root, int k)
  2. {
  3. //返回条件
  4. if (root == NULL||k==0)
  5. return 0;
  6. if (k == 1)
  7. return 1;
  8. //子问题
  9. return BinaryTreeLevelKSize(root->left, k - 1) +BinaryTreeLevelKSize(root->right, k - 1);
  10. }

3.4查找值为x的结点

找到了值为x的结点,需要层层返回给上一层

  1. BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
  2. {
  3. if (root == NULL)
  4. return NULL;
  5. if (root->data == x)
  6. return root;
  7. //记录找到的值防止重复调用
  8. BTNode* ret1 = BinaryTreeFind(root->left, x);
  9. if (ret1)
  10. return ret1;
  11. BTNode* ret2 = BinaryTreeFind(root->right, x);
  12. if (ret2)
  13. return ret2;
  14. return NULL;
  15. }

递归展开图:

3.5二叉树的销毁

树的销毁也是递归销毁的 仍然是按照 若干个整体单元递归销毁,但是防止找不到左、右子树所以采用后序遍历的思想(左子树 右子树 根)

  1. void BinaryTreeDestory(BTNode* root)
  2. {
  3. if (root == NULL)
  4. return;
  5. BinaryTreeDestory(root->left);//左子树的销毁
  6. BinaryTreeDestory(root->right);//右子树的销毁
  7. free(root);//根的销毁
  8. }

四、基础OJ

4.1二叉树遍历

二叉树遍历_牛客题霸_牛客网

思路分析:

题目要求我们前序遍历字符串构建一个二叉树,之后中序遍历打印数据

我们按照前序思路遍历 ( 根,左子树,右子树)

<1>遇到 # return NULL

<2>非空 malloc结点记录当前字符 

<3>递归遍历实现

  1. #include <stdio.h>
  2. typedef char BTDataType;
  3. typedef struct BinaryTreeNode
  4. {
  5. BTDataType data;
  6. struct BinaryTreeNode* left;
  7. struct BinaryTreeNode* right;
  8. }BTNode;
  9. BTNode*CreateTree(char*a,int*pi)
  10. {
  11. if(a[*pi]=='#')
  12. {
  13. (*pi)++;
  14. return NULL;
  15. }
  16. BTNode*root=(BTNode*)malloc(sizeof(BTNode));
  17. root->data=a[(*pi)++];//访问根
  18. root->left=CreateTree(a,pi);//构建左子树
  19. root->right=CreateTree(a, pi);//构建右子树
  20. return root;
  21. }
  22. void InOrder(BTNode* root)
  23. {
  24. if (root == NULL)
  25. {
  26. return;
  27. }
  28. InOrder(root->left);
  29. printf("%c ", root->data);
  30. InOrder(root->right);
  31. }
  32. int main() {
  33. char a[100];
  34. int i=0;
  35. scanf("%s",a);
  36. BTNode*root= CreateTree(a, &i);
  37. InOrder(root);
  38. return 0;
  39. }

注意:需要同时遍历数组和递归遍历构建二叉树,遍历数组时,如果字符为 # 变量 i 也是需要++的

4.2左叶子的和

. - 力扣(LeetCode)

思路分析:

求的是左子叶的和

<1>保证是左叶子结点

<2>记录左叶子的数值

<3>递归方法返回子树左叶子结点和

  1. int dfs(struct TreeNode* root)
  2. {
  3. int sum=0;//记录求和数值
  4. if(root==NULL)
  5. return 0;
  6. if(root->left!=NULL)//保证左叶子
  7. {
  8. if(root->left->left==NULL&&root->left->right==NULL)//保证叶子结点
  9. {
  10. sum+=root->left->val;//加左叶子数值
  11. }
  12. }
  13. //dfs遍历
  14. sum+=dfs(root->left);//遍历左子树
  15. sum+=dfs(root->right);//遍历右子树
  16. return sum;
  17. }
  18. int sumOfLeftLeaves(struct TreeNode* root){
  19. return dfs(root);
  20. }

4.3翻转二叉树

. - 力扣(LeetCode)

思路分析:

dfs遍历最小的整体单元(根,左子树,右子树)

先叶子结点开始翻转交换,然后左子树,右子树交换

  1. struct TreeNode* invertTree(struct TreeNode* root) {
  2. if (root == NULL) {
  3. return NULL;
  4. }
  5. struct TreeNode* left = invertTree(root->left);
  6. struct TreeNode* right = invertTree(root->right);
  7. root->left = right;
  8. root->right = left;
  9. return root;
  10. }

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

闽ICP备14008679号