数据结构:二叉树的链式结构的实现

1.二叉树的前序遍历

2.二叉树的中序遍历

3.二叉树的后序遍历

4.二叉树的层序遍历

5.二叉树的节点个位

6.二叉树的叶子节点个位数

7.二叉树的的k层节点数

8.查找值为x的节点

9.二叉树的销毁

10.判断二叉树是否为完全二叉树

11.求二叉树的高度h

12.通过前序遍历构建二叉树

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


typedef int BTDataType;
typedef struct BinaryTreeNode
{
	BTDataType data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BTNode;


BTNode* BuyNode(BTDataType x);
BTNode* CreatBinaryTree();
void BTreeDestory(BTNode* root);
int BinaryTreeSize(BTNode* root);
int BinaryTreeLeafSize(BTNode* root);
int BinaryTreeLevelKSize(BTNode* root, int k);
//查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
//前、中、后序遍历
void PrevOrder(BTNode* root);
void InOrder(BTNode* root);
void PostOrder(BTNode* root);
//层序遍历
void LevelOrder(BTNode* root);
bool BinaryTreeComplete(BTNode* root);
//通过前序遍历构建二叉树
BTNode* PrevOrderCreate(char* a, int* pi);

1.通过前序遍历构建二叉树

复制代码
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;
}
//(int* pi)是数组的下标用来遍历数组,因为如果不传地址,形参改变不会影响实参
BTNode* PrevOrderCreate(char* a, int* pi)
{
	if (a[*pi] == '#')
	{
		++(*pi);
		return NULL;
	}
	BTNode* root = BuyNode(a[*pi]);
	++(*pi);
	//递归
	root->left = PrevOrderCreate(a, pi);
	root->right = PrevOrderCreate(a, pi);
	return root;
}

2. 二叉树的销毁

复制代码
void BTreeDestory(BTNode* root)
{
	if (root == NULL)
		return;

	BTreeDestory(root->left);
	BTreeDestory(root->right);
	free(root);
}

3.二叉树的遍历

复制代码
//前序遍历
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);
}
//层序遍历
void LevelOrder(BTNode* root)
{
	Queue q;
	QueueInit(&q);

	if (root)
		QueuePush(&q, root);

	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);

		printf("%d ", front->data);

		if (front->left)
			QueuePush(&q, front->left);

		if (front->right)
			QueuePush(&q, front->right);
	}

	printf("\n");

	QueueDestroy(&q);
}

层序遍历需要用到队列: 队列的实现_元清加油的博客-CSDN博客

4. 二叉树的节点个位和二叉树的叶子节点个位数

复制代码
二叉树的节点个位
int BinaryTreeSize(BTNode* root)
{
	return root == NULL ? 0 :
		BinaryTreeSize(root->left) +
		BinaryTreeSize(root->right) + 1;
}
二叉树的叶子节点个位数
int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}

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

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

5. 二叉树的的k层节点数和查找值为x的节点

复制代码
二叉树的的k层节点数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
	assert(k > 0);

	if (root == NULL)
		return 0;

	if (k == 1)
		return 1;

	return BinaryTreeLevelKSize(root->left, k - 1)
		+ BinaryTreeLevelKSize(root->right, k - 1);
}
查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	if (root == NULL)
		return NULL;

	if (root->data == x)
		return root;

	BTNode* ret1 = BinaryTreeFind(root->left, x);
	if (ret1)
		return ret1;

	BTNode* ret2 = BinaryTreeFind(root->right, x);
	if (ret2)
		return ret2;

	return NULL;
}

6. 判断二叉树是否为完全二叉树和求二叉树的高度h

复制代码
// 判断二叉树是否是完全二叉树
bool BinaryTreeComplete(BTNode* root)
{
	Queue q;
	QueueInit(&q);

	if (root)
		QueuePush(&q, root);

	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);

		// 遇到空就跳出
		if (front == NULL)
			break;

		QueuePush(&q, front->left);
		QueuePush(&q, front->right);
	}

	// 检查后面的节点有没有非空
	// 有非空,不是完全二叉树
	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);

		if (front)
		{
			QueueDestroy(&q);
			return false;
		}
	}

	QueueDestroy(&q);
	return true;
}
//求二叉树的高度h
int BinaryTreeHeight(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	int LeftHeight = BinaryTreeHeight(root->left);
	int RightHeight = BinaryTreeHeight(root->right);
	return LeftHeight > RightHeight ? LeftHeight + 1 : RightHeight + 1;
}

判断是否为完全二叉树需要用到队列: 队列的实现_元清加油的博客-CSDN博客

相关推荐
努力写代码的熊大15 分钟前
链式二叉树数据结构(递归)
数据结构
yi.Ist16 分钟前
数据结构 —— 键值对 map
数据结构·算法
爱学习的小邓同学16 分钟前
数据结构 --- 队列
c语言·数据结构
s1533519 分钟前
数据结构-顺序表-猜数字
数据结构·算法·leetcode
闻缺陷则喜何志丹20 分钟前
【前缀和 BFS 并集查找】P3127 [USACO15OPEN] Trapped in the Haybales G|省选-
数据结构·c++·前缀和·宽度优先·洛谷·并集查找
Coding小公仔22 分钟前
LeetCode 8. 字符串转换整数 (atoi)
算法·leetcode·职场和发展
GEEK零零七27 分钟前
Leetcode 393. UTF-8 编码验证
算法·leetcode·职场和发展·二进制运算
DoraBigHead1 小时前
小哆啦解题记——异位词界的社交网络
算法
木头左3 小时前
逻辑回归的Python实现与优化
python·算法·逻辑回归
lifallen7 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree