数据结构C语言版清华大学严蔚敏(学习笔记总结4,代码在下面)——树、空树的建立、创建树、销毁树、统计叶子结点、树的遍历、前序中序后序层次遍历、完全二叉、哈夫曼树(前面是笔记方便理解,最下面有详细代码)

数据结构C语言版清华大学严蔚敏(学习笔记总结3,代码在下面)——栈和队列、进栈出栈、栈空、入队、出队、循环队列(前面是笔记方便理解,最下面有详细代码)_玛卡巴卡的博客-CSDN博客

1、创建树

#define _CRT_SECURE_NO_WARNINGS 1
#include "Tree.h"


BTNode *BinaryTreeCreate(BTDataType*a, int* pi)  //创建树
{
		if (a[*pi] == '#')
		{
			(*pi)++;
			return NULL;
		}
			BTNode* root = (BTNode*)malloc(sizeof(BTNode));
			root->_left = root->_right = NULL;
			root->_data = a[*pi];
			(*pi)++;
			root->_left = BinaryTreeCreate(a, pi);
			root->_right = BinaryTreeCreate(a, pi);
			return root;		
}

2、销毁树

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

3、统计叶子节点

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

int BinaryTreeLeafSize(BTNode* root)   //统计树的叶子节点个数
{
	if (root == NULL)
		return 0;
	else
	{
		if ((root->_left == NULL) && (root->_right == NULL))
			return 1;
		else
		return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
	}
}

4、统计第k层结点数

int BinaryTreeLevelkSize(BTNode* root, int k)  //递归--统计树中第k层的节点个数
{
	if (root == NULL)		return 0;
	if (k==1)
		return 1;
	return BinaryTreeLevelkSize(root->_left,k-1) + BinaryTreeLevelkSize(root->_right,k-1);
}

BTNode *BinaryTreeFind(BTNode* root, BTDataType x)  //找到某个树节点
{
	BTNode* ret;
	if (root == NULL)	
		return NULL;
	if (root->_data == x)   
		return root;
	 ret = BinaryTreeFind(root->_left, x);
	{
		if (ret)
			return ret;
		ret = BinaryTreeFind(root->_right, x);
		if (ret)
			return ret;
	}	
	return NULL;
}

5、前序遍历、中序遍历、后序遍历(递归和非递归共六种)

void BinaryTreePrevOrder(BTNode* root)   //递归--前序遍历
{
	if (root == NULL)
		return;
	printf("%c ", root->_data);
	BinaryTreePrevOrder(root->_left);
	BinaryTreePrevOrder(root->_right);
}

void BinaryTreeInOrder(BTNode* root)   //递归--中序遍历
{
	if (root == NULL)
		return;
	BinaryTreeInOrder(root->_left);
	printf("%c ", root->_data);
	BinaryTreeInOrder(root->_right);
}

void BinaryTreePostOrder(BTNode* root)   //递归--后序遍历
{
	if (root == NULL)
		return;
	BinaryTreePostOrder(root->_left);
	BinaryTreePostOrder(root->_right);
	printf("%c ", root->_data);
}
void BinaryPrevOrder(BTNode* root)   //非递归--前序遍历
{
	Stack st;
	StackInit(&st);
	BTNode* cur = root;
	while (StackEmpty(&st) != 0 || cur != NULL)
	{
		while (cur != NULL)
		{
			printf("%c ", cur->_data);
			StackPush(&st, cur);
			cur = cur->_left;
		}
		BTNode* top = StackTop(&st);
		StackPop(&st);
		cur=top->_right;
	}
}

void BinaryInOrder(BTNode* root)    //非递归--中序遍历
{
	Stack st;
	StackInit(&st);
	BTNode* cur = root;
	while (StackEmpty(&st) != 0 || cur != NULL)
	{
		while (cur != NULL)
		{
			StackPush(&st, cur);
			cur = cur->_left;
		}
		BTNode* top = StackTop(&st);
		printf("%c ", top->_data);
		StackPop(&st);
		cur = top->_right;
	}
}

void BinaryPostOrder(BTNode* root)    //非递归--后序遍历
{
	BTNode* cur = root;
	BTNode* prev = NULL;
	Stack st;
	StackInit(&st);
	while (cur || StackEmpty(&st) != 0)
	{
		while (cur != NULL)
		{
			StackPush(&st, cur);
			cur = cur->_left;
		}
		BTNode* top = StackTop(&st); 
		if (top->_right == NULL || top->_right == prev)
		{
			StackPop(&st);
			printf("%c ", top->_data);
			prev = top;
		}
		else cur=top->_right;
	} 
}

6、层序遍历

void BinaryTreeLevelOrder(BTNode* root)  //层序遍历
{
	Qu q;
	QueueInit(&q);
	if (root != NULL)
	{
		QueuePush(&q, root);
		while (QueueEmpty(&q) != 0)
		{
			BTNode* front = QueueFront(&q);
			printf("%c ", front->_data);
			QueuePop(&q);

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

7、判断是否是完全二叉树

int BinaryTreeComplete(BTNode* root)   //判断树是否是完全二叉树
{
	Qu q;
	QueueInit(&q);
	if (root != NULL)
	{
		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);
			if (front != NULL)
			{
				QueueDestory(&q);
				return 0;
			}	
			QueuePop(&q);
		}
		return 1;
	}	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

玛卡巴卡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值