【数据结构】二叉树详解(1)

⭐️ 前言

二叉树的概念性质


⭐️ 二叉树链式结构的实现

结构定义:

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef int BinaryTreeDataType;

typedef struct BinaryTreeNode {
	BinaryTreeDataType value;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BinaryTreeNode;

⭕️ 二叉树的遍历

按照规则,二叉树的遍历分为:前序/中序/后序的递归遍历。

  • 前序遍历(PreOrder Traversal):访问根节点的操作发生在遍历其左右子树之前。
    • 根 -> 左子树 -> 右子树
  • 中序遍历(InOrder Traversal):访问根节点的操作发生在遍历左右子树之间。
    • 左子树 -> 根 -> 右子树
  • 后序遍历(PostOrder Traversal):访问根节点的操作发生在遍历其左右子树之后。
    • 左子树 -> 右子树 -> 根


PreOrder 代码:

c 复制代码
// 前序遍历递归 根 -> 左子树 -> 右子树
void PreOrder(BinaryTreeNode* root) {
	if (root == NULL) {
		printf("# ");
		return;
	}

	printf("%d ", root->value);
	PreOrder(root->left);
	PreOrder(root->right);
}

前序递归流程图:

前序递归遍历顺序为:1 2 3 # # # 4 5 # # 6 # #

InOrder 代码:

c 复制代码
// 中序遍历递归 左子树 -> 根 -> 右子树
void InOrder(BinaryTreeNode* root) {
	if (root == NULL) {
		printf("# ");
		return;
	}

	InOrder(root->left);
	printf("%d " , root->value);
	InOrder(root->right);
}

中序递归流程图:

中序递归遍历顺序为:# 3 # 2 # 1 # 5 # 4 # 6 #

PostOrder 代码:

c 复制代码
// 后序遍历递归 左子树 -> 右子树 -> 根
void PostOrder(BinaryTreeNode* root) {
	if (root == NULL) {
		printf("# ");
		return;
	}

	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d " , root->value);
}

后序递归流程图:

后序递归遍历顺序为:# # 3 # 2 # # 5 # # 6 4 1


注:# 代表空树

⭕️ 二叉树的其他接口

c 复制代码
// 节点的数量
int BinaryTreeSize(BinaryTreeNode* root);
// 叶子节点的数量
int BinaryTreeLeafSize(BinaryTreeNode* root);
// 求k层节点的个数
int BinaryTreeKLevelSize(BinaryTreeNode* root , int k);
// 二叉树中查找某个元素
BinaryTreeNode* BinaryTreeFind(BinaryTreeNode* root , BinaryTreeDataType x);

BinaryTreeSize 代码:

c 复制代码
// 节点的数量
int BinaryTreeSize(BinaryTreeNode* root) {
	if (root == NULL) {
		return 0;
	}

	return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}

BinaryTreeSize 递归流程图:


BinaryTreeLeafSize 代码:

c 复制代码
// 叶子节点的数量
int BinaryTreeLeafSize(BinaryTreeNode* root) {
	if (root == NULL) {
		return 0;
	}

	if (root->left == NULL && root->right == NULL) {
		return 1;
	}

	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}

BinaryTreeLeafSize 递归流程图:


BinaryTreeKLevelSize 代码:

c 复制代码
// 求k层节点的个数
int BinaryTreeKLevelSize(BinaryTreeNode* root , int k) {
	assert(k >= 1);

	if (root == NULL) {
		return 0;
	}

	if (k == 1) {
		return 1;
	}

	return BinaryTreeKLevelSize(root->left , k - 1) + BinaryTreeKLevelSize(root->right , k - 1);
}

BinaryTreeKLevelSize 递归流程图:


BinaryTreeFind 代码:

c 复制代码
// 二叉树中查找某个元素
BinaryTreeNode* BinaryTreeFind(BinaryTreeNode* root , BinaryTreeDataType x) {
	if (root == NULL) {
		return NULL;
	}

	if (root->value == x) {
		return root;
	}

	BinaryTreeNode* left = BinaryTreeFind(root->left , x);
	if (left != NULL) {
		return left;
	}

	BinaryTreeNode* right = BinaryTreeFind(root->right , x);
	if (right != NULL) {
		return right;
	}

	return NULL;
}

BinaryTreeFind 递归流程图:


相关推荐
lingggggaaaa1 分钟前
小迪安全v2023学习笔记(七十八讲)—— 数据库安全&Redis&CouchDB&H2database&未授权&CVE
redis·笔记·学习·算法·安全·网络安全·couchdb
得意霄尽欢7 分钟前
Redis之核心数据结构浅析
数据结构·redis
Jayyih44 分钟前
嵌入式系统学习Day29(tcp)
网络·学习·tcp/ip
DashingGuy1 小时前
算法(keep learning)
java·数据结构·算法
田里的水稻1 小时前
C++_数据类型和数据结构
java·数据结构·c++
兔兔西1 小时前
【数据结构、java学习】数组(Array)
java·数据结构·算法
小徐不徐说1 小时前
数据结构基础之队列:数组/链表
c语言·数据结构·算法·链表·面试
g_i_a_o_giao2 小时前
Android8 binder源码学习分析笔记(一)
android·java·笔记·学习·binder·安卓源码分析
yuxb732 小时前
Docker学习笔记(三):镜像与容器管理进阶操作
笔记·学习·docker
人生游戏牛马NPC1号2 小时前
学习 Android (二十) 学习 OpenCV (五)
android·opencv·学习