当前位置: 首页 > news >正文

数据结构之二叉树--前序,中序,后序详解(含源码)

二叉树

二叉树不能轻易用断言,因为树一定有空

二叉树链式结构的实现

在学习二叉树的基本操作前,需先要创建一棵二叉树,然后才能学习其相关的基本操作。
typedef int BTDataType;
typedef struct BinaryTreeNode
{BTDataType _data;struct BinaryTreeNode* _left;struct BinaryTreeNode* _right;
}BTNode;
BTNode* CreatBinaryTree()
{BTNode* node1 = BuyNode(1);BTNode* node2 = BuyNode(2);BTNode* node3 = BuyNode(3);BTNode* node4 = BuyNode(4);BTNode* node5 = BuyNode(5);BTNode* node6 = BuyNode(6);node1->_left = node2;node1->_right = node4;node2->_left = node3;node4->_left = node5;node4->_right = node6;return node1;
}

二叉树的遍历

前序、中序以及后序遍历

1. 前序遍历 (Preorder Traversal 亦称先序遍历 )— 访问根结点的操作发生在遍历其左右子树之前。
2. 中序遍历 (Inorder Traversal)—— 访问根结点的操作发生在遍历其左右子树之中(间)。
3. 后序遍历 (Postorder Traversal)—— 访问根结点的操作发生在遍历其左右子树之后。
由于被访问的结点必是某子树的根, 所以 N(Node )、 L(Left subtree )和 R(Right subtree )又可解释为 根,根的左子树和根的右子树 NLR LNR LRN 分别又称为先根遍历、中根遍历和后根遍历。
// 二叉树前序遍历
void PreOrder(BTNode* root);
// 二叉树中序遍历
void InOrder(BTNode* root);
// 二叉树后序遍历
void PostOrder(BTNode* root);

前序

递归图

中序

递归图

后序同理

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>typedef int BTDataType;
typedef struct BinaryTreeNode
{BTDataType data;struct BinaryTreeNode* left;struct BinaryTreeNode* right;
}BTNode;BTNode* BuyNode(BTDataType x)
{BTNode* node = (BTNode*)malloc(sizeof(BTNode));if (node == NULL){perror("malloc fail");return NULL;}node->data = x;node->left = NULL;node->right = NULL;return node;
}BTNode* CreatBinaryTree()
{BTNode* node1 = BuyNode(1);BTNode* node2 = BuyNode(2);BTNode* node3 = BuyNode(3);BTNode* node4 = BuyNode(4);BTNode* node5 = BuyNode(5);BTNode* node6 = BuyNode(6);BTNode* node7 = BuyNode(7);node1->left = node2;node1->right = node4;node2->left = node3;node4->left = node5;node4->right = node6;node5->left = node7;return node1;
}void PrevOrder(BTNode* root)
{if (root == NULL){printf("N ");return;}printf("%d ", root->data);PrevOrder(root->left);PrevOrder(root->right);
}void InOrder(BTNode* root)
{if (root == NULL){printf("N ");return;}InOrder(root->left);printf("%d ", root->data);InOrder(root->right);
}void PostOrder(BTNode* root)
{if (root == NULL){printf("N ");return;}PostOrder(root->left);PostOrder(root->right);printf("%d ", root->data);
}

层序遍历

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

计算二叉树高度

分析

int BTreeHeight(BTNode* root)
{if (root == NULL)return 0;int leftHeight = BTreeHeight(root->left);int rightHeight = BTreeHeight(root->right);return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}

计算结点个数

1

2递归求结点个数

//int size = 0;
//void BTreeSize(BTNode* root)
//{
//	if (root == NULL)
//		return;
//
//	++size;
//
//	BTreeSize(root->left);
//	BTreeSize(root->right);
//}int BTreeSize(BTNode* root)
{/*if (root == NULL)return 0;return BTreeSize(root->left)+ BTreeSize(root->right)+ 1;*/return root == NULL ? 0 : BTreeSize(root->left)+ BTreeSize(root->right) + 1;
}

求叶子结点个数

// 求叶子节点的个数
int BTreeLeafSize(BTNode* root)
{if (root == NULL){return 0;}if (root->left == NULL&& root->right == NULL){return 1;}return BTreeLeafSize(root->left)+ BTreeLeafSize(root->right);
}

计算第k层结点个数

// 二叉树第k层结点个数
int BTreeLevelKSize(BTNode* root, int k)
{assert(k > 0);if (root == NULL)return 0;if (k == 1)return 1;return BTreeLevelKSize(root->left, k - 1)+ BTreeLevelKSize(root->right, k - 1);
}

http://www.lryc.cn/news/480440.html

相关文章:

  • 红黑树及MySQL 基础架构
  • 大数据-212 数据挖掘 机器学习理论 - 无监督学习算法 KMeans 基本原理 簇内误差平方和
  • QJson-趟过的各种坑(先坑后用法)
  • 基于STM32的hx711称重模块使用
  • Nginx独立项目相关配置说明
  • Nuxt3之使用lighthouse性能测试及性能优化实操
  • ‌webdriver.Chrome()参数简介
  • Ubuntu如何更换环境中的Python版本
  • python-字符串中大写字母转小写,小写字母转大写
  • 前端学习之ES6+
  • yolov10的几种权重文件
  • FPGA视频GTH 8b/10b编解码转PCIE3.0传输,基于XDMA中断架构,提供工程源码和技术支持
  • C++类和对象 (下)
  • 网络层5——IPV6
  • 【wpf】ResourceDictionary 字典资源的用法
  • Foliate:沉浸式阅读!!!
  • 【excel基本操作-sumif绝对引用和相对引用
  • word及Excel常见功能使用
  • 网页中的某个元素高度突然无法设置
  • springboot给不同用户动态定制请求结果思路
  • 【网络】HTTP(超文本传输协议)详解
  • poll()方法和remove()方法的区别是什么?
  • 小程序跳转另一个小程序
  • 介绍一下数组(c基础)(详细版)
  • 工业主板在汽车制造中的应用
  • 博弈论(所有情况最优解)——课堂笔记
  • 深入提升Python编程能力的全方位指南
  • 如何使用 Python 语言的正则表达式进行网页数据的爬取?
  • 剑指offer第七、八天
  • 有哪些常见的方法可以评估中断处理能力?