堆 堆排序及TOPk问题

1.什么是堆

特殊的树:二叉树-CSDN博客 中我们介绍了完全二叉树,且堆属于完全二叉树这一数据结构,就分类而言,堆只分为大堆、小堆。

大堆:所有的父节点值都大于其子节点值,小堆反之。

 2.实现堆及其功能

 Heap.h头文件


typedef int HPDataType;
typedef struct Heap
{
	HPDataType* _a;    //和顺序表一样
	int _size;
	int _capacity;
}Heap;
//堆的初始化
void HeapInit(Heap* php);
// 堆的销毁
void HeapDestory(Heap* hp);
// 堆的插入
void HeapPush(Heap* hp, HPDataType x);
// 堆的删除
void HeapPop(Heap* hp);
// 取堆顶的数据
HPDataType HeapTop(Heap* hp);
// 堆的数据个数
int HeapSize(Heap* hp);
// 堆的判空
int HeapEmpty(Heap* hp);
void Swap(int* a, int* b);
void Adjustup(Heap* hp, int child);
void Adjustdown(Heap* hp,int parent);
// 对数组进行堆排序
void HeapSort(int* a, int n);
//解决TOPk问题
void CreateNDate();
void PrintTopK(int k);

 Heap.c文件:

//堆的初始化
void HeapInit(Heap* php)
{
	php->_a = NULL;
	php->_size = php->_capacity = 0;
}
// 堆的销毁
void HeapDestory(Heap* hp)
{
	assert(hp);
	free(hp->_a);
	hp->_a = NULL;
	hp->_size = hp->_capacity = 0;
}
// 堆的插入
void HeapPush(Heap* hp, HPDataType x)
{
	assert(hp);
	int newcapacity;
	if (hp->_size == hp->_capacity|| hp->_size+1 == hp->_capacity)
	{
		newcapacity = hp->_capacity == 0 ? 4 : 2 * sizeof(hp->_capacity);
		hp->_capacity = newcapacity;
		HPDataType* tmp = (HPDataType*)realloc(hp->_a, newcapacity * sizeof(HPDataType));
		assert(tmp);
		hp->_a = tmp;
	}
	else
		;
	hp->_a[hp->_size] = x;
	hp->_size++;
	Adjustup(hp, hp->_size - 1);
}
// 堆的删除(注意是删除堆顶并重排)
void HeapPop(Heap* hp)
{
	assert(hp);
	assert(hp->_size > 0);
	int end = hp->_size;
	Swap(&hp->_a[0], &hp->_a[end-1]);
	hp->_size--;
	Adjustdown(hp, 0);

}
// 取堆顶的数据
HPDataType HeapTop(Heap* hp)
{
	assert(hp);
	return hp->_a[0];
}
// 堆的数据个数
int HeapSize(Heap* hp)
{
	assert(hp);
	return hp->_size;
}
// 堆的判空
int HeapEmpty(Heap* hp)
{
	if (hp->_size == 0)
		return 1;
	else
		return 0;
}
//交换数据
void Swap(int* p1, int* p2)     //注意传地址
{
	int tmp = *p2;
	*p2 = *p1;
	*p1 = tmp;
}
void Adjustup(Heap* hp,int child)    //向上调整,只与其父节点比较,建小堆
{                                     //一般插入新数据要用
	int parent;
	parent = (child - 1) / 2;
	while (parent>=0)
	{
		if ((hp->_a[child]) < (hp->_a[parent]))    //满足条件则交换(可以是大于号建大堆)
		{                                          
			Swap(&hp->_a[child], &hp->_a[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
			break;
	}
}
void Adjustdown(Heap*hp,int parent)           //向下调整
{
	int leftchild, rightchild;
	leftchild = parent * 2 + 1;
	rightchild = parent * 2 + 2;
	while (leftchild < hp->_size && rightchild < hp->_size)   //左右节点都存在
	{                                              //要比较两者大小(永远和最小或最大的交换)
		if (hp->_a[leftchild] <= hp->_a[rightchild])
		{
			if (hp->_a[leftchild] < hp->_a[parent])
			{
				Swap(&hp->_a[leftchild], &hp->_a[parent]);
				parent = leftchild;
				leftchild = parent * 2 + 1;
				rightchild = parent * 2 + 2;
			}
			else
				break;
		}
		else if (hp->_a[leftchild] > hp->_a[rightchild])
		{
			if (hp->_a[rightchild] < hp->_a[parent])
			{
				Swap(&hp->_a[rightchild], &hp->_a[parent]);
				parent = rightchild;
				leftchild = parent * 2 + 1;
				rightchild = parent * 2 + 2;
			}
			else
				break;
		}
	}
	//右孩子不存在。如果两个都不存在,不用比就结束了
	if (leftchild == (hp->_size)-1 )
	{
		if (hp->_a[leftchild] < hp->_a[parent])
		{
			Swap(&hp->_a[leftchild], &hp->_a[parent]);
		}
		else
			;
	}
}

到此为止,我们已实现了堆的功能。

堆排序

我们以前学过冒泡排序,可以将数组变为有序数组,而堆也是通过数组实现的,并且大堆(小堆)

父子节点数据之间也有大小关系。堆排序也是可以实现数组排序的。

// 对数组进行堆排序
void HeapSort(int* a, int n)     //a是数组名,n是数组元素个数
{
	Heap A;
	HeapInit(&A);           //先建堆
	A._a = a;
	A._size = n ;
	for (int i = 0; i < n; i++)
	{
		Adjustup(&A,i);//将数组中数据依次向上调整建小堆      
	}
	int end = n - 1;
	while (end > 0)
	{
		 
		Swap(&a[0], &a[end]);  //交换堆的根数据和未交换的最后一个数据(根数据一定是最小的)
		A._size--;             //注意堆最后一个数据不要再交换
		Adjustdown(&A, 0);     //向下调整
		end--;
	}
}

 在每次while循环中,我们都会将小的数据排到后方,依次下来就将数组排好序了。

TOPk问题

从N个数据中选出k个最大(小)的数据。N个数据在txt文件中,此时我们就不能将N个数据放入数组中,因为N个数据是未知且很大的。

void CreateNDate()
{
	// 造数据
	int n = 20;
	srand(time(0));
	const char* file = "data.txt";
	FILE* fin = fopen(file, "w");
	if (fin == NULL)
	{
		perror("fopen error");
		return;
	}

	for (size_t i = 0; i < n; ++i)
	{
		int x = rand() % 1000000;
		fprintf(fin, "%d\n", x);
	}

	fclose(fin);
}

void PrintTopK(int k)
{
	CreateNDate();
	Heap A;
	HeapInit(&A);
	int m=0;
	const char* file = "data.txt";
	FILE* fou = fopen(file, "r");
	HPDataType* tmp = (HPDataType*)malloc(k*sizeof(int));
	A._a = tmp;
	for (int j = 0; j < k; j++)      //建k个大小的数组,将前k个数放入数组
	{
		fscanf_s(fou,"%d", &(A._a[j]));
	}
	A._size = k;
	while (m < k)
	{

		Adjustdown(&A, m);      //向下调整,调成小堆
		m++;
	}
	int x = 0;
	while (fscanf(fou, "%d", &x)!=-1)  //注意fscanf的返回值,0/-1
	{
		
		if (x > A._a[0])          //继续读取,比堆顶大的,就交换,再向下调整
		{
			A._a[0] = x;
			Adjustdown(&A, 0);
		}
		else
			;
		
	}
	for (int i = 0; i < k; i++)
	{
		printf("%d ",A._a[i]);
	}
	fclose(fou);
	HeapDestory(&A);
}

总结:介绍堆的功能,用堆将数组排序,N个数中选出k个最大(小)的数。


 

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值