二叉树遍历与操作的C语言实现B

接上篇内容,在本文中,我们将探讨二叉树的前序、中序、后序遍历,节点个数、叶子节点个数、第k层节点个数、查找值为x的节点、判断是否为完全二叉树、深度计算、层序遍历以及二叉树的销毁。下面,我将给出各个操作的C语言实现。

1. 二叉树节点定义

首先,我们需要定义一个二叉树节点的结构体。

typedef char BTDataType;

typedef struct BinaryTreeNode
{
	BTDataType _data;
	struct BinaryTreeNode* _left;
	struct BinaryTreeNode* _right;
}BTNode;
 通过前序遍历的数组"ABD##E#H##CF##G##"构建个二叉树

BTNode* BuyNode(char x)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	if (newnode == NULL)
	{
		perror("malloc fail"); 
		return NULL;
	}
	newnode->_data = x;
	newnode->_left = NULL;
	newnode->_right = NULL;
	return newnode;
}
// 通过前序遍历的数组"A B D # # E # H # # C F # # G # # "构建二叉树
BTNode* BinaryTreeCreate()
{
	BTNode* nodeA = BuyNode('A');
	BTNode* nodeB = BuyNode('B');
	BTNode* nodeC = BuyNode('C');
	BTNode* nodeD = BuyNode('D');
	BTNode* nodeE = BuyNode('E');
	BTNode* nodeF = BuyNode('F');
	BTNode* nodeG = BuyNode('G');
	BTNode* nodeH = BuyNode('H');
	nodeA->_left = nodeB;
	nodeB->_right = nodeE;
	nodeB->_left = nodeD;
	nodeA->_right = nodeC;
	nodeE->_right = nodeH;
	nodeC->_left = nodeF;
	nodeC->_right = nodeG;
	return nodeA;
}
2. 前序遍历

前序遍历的顺序是:根节点 -> 左子树 -> 右子树。

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

中序遍历的顺序是:左子树 -> 根节点 -> 右子树。

// 二叉树中序遍历
void BinaryTreeInOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("%c ", 'N');
		return;
	}
	BinaryTreeInOrder(root->_left);
	printf("%c ", root->_data);
	BinaryTreeInOrder(root->_right);
}
4. 后序遍历

后序遍历的顺序是:左子树 -> 右子树 -> 根节点。

// 二叉树后序遍历
void BinaryTreePostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("%c ", 'N');
		return;
	}
	BinaryTreePostOrder(root->_left);
	BinaryTreePostOrder(root->_right);
	printf("%c ", root->_data);
}
5. 节点个数

递归计算二叉树的节点个数。

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

}
6. 叶子节点个数

递归计算二叉树的叶子节点个数。

// 二叉树叶子节点个数
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);
}
7. 第k层节点个数

使用队列实现层序遍历,并计算第k层的节点个数。

// 二叉树第k层节点个数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
	if (root == NULL)
		return 0;
	if (k == 1)
	{
		return 1;
	}
	//子问题
	return BinaryTreeLevelKSize(root->_left, k - 1) +
		BinaryTreeLevelKSize(root->_right, k - 1);
}
8. 查找值为x的节点

递归查找值为x的节点。

// 二叉树查找值为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;
}
9. 判断是否为完全二叉树

完全二叉树的定义是:除了最后一层外,每一层上的节点数均达到最大值;在最后一层上只缺少右边的若干节点。要用到队列内容(可查往期内容).

// 判断二叉树是否是完全二叉树
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;
}
10. 二叉树层序遍历

为(A B C D E F G H)广度优先遍历(BFS),要用到队列内容(可查往期内容).

// 层序遍历
void BinaryTreeLevelOrder(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	if (root)
		QueuePush(&q, root);
	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q); 
		QueuePop(&q);
		printf("%c ", front->_data);
		if (front->_left)
			QueuePush(&q, front->_left);
		if (front->_right)
			QueuePush(&q, front->_right);
	}
	QueueDestroy(&q);
}
11. 二叉树深度

二叉树的最大高度

// 二叉树深度
int BinaryTreeweight(BTNode* root)
{
	if (root == NULL)
		return 0;
	int letfHeight = BinaryTreeweight(root->_left);
	int rightHeight = BinaryTreeweight(root->_right);
	return letfHeight > rightHeight ?
		letfHeight + 1 : rightHeight + 1;
}
12. 二叉树的销毁
// 二叉树销毁
void BinaryTreeDestory(BTNode* root)
{
	if (root == NULL)
		return;
	BinaryTreeDestory(root->_left);
	BinaryTreeDestory(root->_right);
	free(root);
}

主函数如下

int main()
{
	BTNode* root = BinaryTreeCreate();
	int x = 3;
	BinaryTreePrevOrder(root);
	puts("");
	BinaryTreeInOrder(root);
	puts("");
	BinaryTreePostOrder(root);
	puts("");
	printf("TreeSize:%d\n",BinaryTreeSize(root));
	printf("TreeLeafSize:%d\n", BinaryTreeLeafSize(root));
	printf("TreeSizeHeight:%d\n", BinaryTreeweight(root));
	printf("BinaryTreeLevelKSize:%d\n", BinaryTreeLevelKSize(root, x));

	BTNode* zi = BinaryTreeFind(root, 'H');
	printf("%c\n", zi->_data);
	bool sa = BinaryTreeComplete;
	printf("TreeComplete:%d\n", sa);
	BinaryTreeLevelOrder(root);
	BinaryTreeDestory(root);
	return 0;
}

结果如下

  • 19
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小志biubiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值