【数据结构】——二叉树的创建

创建


#include "Queue.h"//等会要用到队列
typedef int BTDataType;

typedef struct BinaryTreeNode
{
	BTDataType _data;
	struct BinaryTreeNode* _left;
	struct BinaryTreeNode* _right;
}BTNode;
BTNode* BuyNode()
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	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);

	node1->_left = node2;
	node1->_right = node4;
	node2->_left = node3;
	node4->_left = node5;
	node4->_right = node6;
	return node1;
}

销毁

销毁要注意一下,先把左右子树销毁了
如果先销毁根,那就找不到左右子树了

// 二叉树销毁
void BinaryTreeDestory(BTNode* root)
{
	if (root == NULL)
		return;
	BinaryTreeDestory(root->_left);
	BinaryTreeDestory(root->_right);
	free(root);
}

一,前序遍历

// 二叉树前序遍历 
void BinaryTreePrevOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
	}
	printf("%d", root->_data);
	BinaryTreePrevOrder(root->_left);
	BinaryTreePrevOrder(root->_right);
}

在这里插入图片描述

二,中序遍历

// 二叉树中序遍历
void BinaryTreeInOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
	}
	
	BinaryTreePrevOrder(root->_left);
	printf("%d", root->_data);
	BinaryTreePrevOrder(root->_right);
}

在这里插入图片描述

三、后序遍历

// 二叉树后序遍历
void BinaryTreePostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
	}

	BinaryTreePrevOrder(root->_left);
	BinaryTreePrevOrder(root->_right);
	printf("%d", root->_data);
}

在这里插入图片描述

四、层序遍历

再层序遍历之前,我们先把队列的val设置为树节点的指针
在这里插入图片描述

// 层序遍历
void BinaryTreeLevelOrder(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	//BTNode* front = QueueFront(&q);
	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);
	}
	QueueDestroy(&q);
}

跟着代码走读一下:
在这里插入图片描述

在这里插入图片描述

五、判断二叉树是否为完全二叉树

思路:
1.层序遍历走,空也进队列
2.遇到第一个空节点就开始判断,如果后面全是空,那就是完全二叉树,如果后面还有非空,就不是完全二叉树

/ 判断二叉树是否是完全二叉树
bool BinaryTreeComplete(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	BTNode* front = QueueFront(&q);
	QueuePush(&q, front);
	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)//第一个空后还有非空就不是完全二叉树
			return false;
		QueuePush(&q, front->_left);
		QueuePush(&q, front->_right);
	}
	QueueDestroy(&q);
	return true;

}

六、二叉树节点个数

// 二叉树节点个数
int BinaryTreeSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	return BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right) + 1;
}

在这里插入图片描述

七、二叉树叶子节点个数

// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root)
{
	if (root->_left == NULL && root->_right == NULL)//左子树和右子树都为空就是叶子节点
	{
		return 1;
	}
	return
	BinaryTreeLeafSize(root->_left)+
	BinaryTreeLeafSize(root->_right);
}

八、二叉树查找值为x的节点

// 二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	assert(root);
	if (root->_data == x)
	{
		return root;
	}
	
	if (BinaryTreeFind(root->_left, x))//如果左树为空就向右树查找
		return BinaryTreeFind(root->_left, x);
	else
		return BinaryTreeFind(root->_right, x);
}

九、二叉树层高

//二叉树层高
int TreeHight(BTNode* root)
{
	if (root == NULL)
		return 0;
	int LeftHight = TreeHight(root->_left);
	int RightHight = TreeHight(root->_right);
	return LeftHight > RightHight ? LeftHight + 1 : RightHight + 1;
}

在这里插入图片描述

希望这篇博客对你有所帮助!!!

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值