【数据结构】堆的实现&堆排序&Top-K

一、堆的概念及结构

认识堆之前先简单认识一下俩种树:

  1. 满二叉树:一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是 说,如果一个二叉树的层数为K,且结点总数是 ,则它就是满二叉树。
  2. 完全二叉树:完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。对于深度为K 的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对 应时称之为完全二叉树。
    要注意的是满二叉树是一种特殊的完全二叉树。

在这里插入图片描述

如果有一个关键码的集合K = { 0,1,2,3,4…i },把它的所有元素按完全二叉树的顺序存储方式存储在一个一维数组中,并满足: i<2i+1且i<2i+2称为小堆,反之则为大堆。如图
在这里插入图片描述

二、堆实现

(1)创建结构体

typedef int HPDataType;
typedef struct Heap
{
    
    
	HPDataType* a;
	int size;
	int capacity;
}Heap;

先使用typedef int HPateType,是为了方便改类型,在结构体里创建3个成员变量,HPDataType a,为了方便增容,用指针的形式,int size代表a指向空间里的个数,int capacity代表a指向空间里的容量。

(2)具体函数实现及解析

1.0 交换函数

void Swap(HPDataType* p1, HPDataType* p2)//交换数据
{
    
    
	HPDataType tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

交换数据为堆的向上,向下调整及堆的删除做铺垫,创建临时变量进行交换。

1.1 堆的打印

void HeapPrintf(Heap* hp)//堆的打印
{
    
    
	assert(hp);
	for (int i = 0; i < hp->size; i++)
	{
    
    
		printf("%d ", hp->a[i]);
	}
	printf("\n");
}

断言hp,不能为空,用for循环进行打印。

1.2 堆的初始化

void HeapInit(Heap* hp)//堆的初始化
{
    
    
	assert(hp);
	hp->a =NULL;
	hp->capacity = hp->size = 0;
}

初始化堆,置a指针为空,容量大小和堆个数为0。

1.3 堆的销毁

void HeapDestroy(Heap* hp)// 堆的销毁
{
    
    
	assert(hp);
	free(hp->a);
	hp->a = NULL;
	hp->capacity = hp->size = 0;
}

堆销毁释放指针啊,并置为空,容量大小和堆个数置为0。

1.4 堆的插入

void HeapPush(Heap* hp, HPDataType x)// 堆的插入(相当于在建堆),并保持它是一个堆(O(logN))
{
    
    
	assert(hp);
	if (hp->capacity == hp->size)//扩容
	{
    
    
		int newcapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
		HPDataType* tmp = (HPDataType*)realloc(hp->a, sizeof(HPDataType)*newcapacity);
		if (tmp == NULL)
		{
    
    
			perror("realloc fail ");
			exit(-1);
		}
		hp->a = tmp;
		hp->capacity = newcapacity;
	}
	hp->a[hp->size] = x;
	hp->size++;
	AdjustUp(hp->a, hp->size-1);
}

堆的插入也是核心内容,其实是在建堆,假设建大堆,那么首先插入数据前,要先考虑扩容的问题,如果capacity和size大小相等,说明满了需要扩,之后进行数据插入,因为此过程是插入数据并且使它成为大堆,所以当插完数据之后,需要调用向上调整函数,让它成为大堆。

1.5堆的向上调整算法

void AdjustUp(HPDataType* a, int child)//向上调整
{
    
    
	int parent = (child - 1) / 2;
	while (child > 0)
	{
    
    
		if (a[child] > a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
    
    
			break;
		}
	}

}

假设是建大堆,向上调整的算法思路是从孩子结点开始,从下到上比较孩子结点和父亲结点的大小,如果比父亲大,就交换父子结点,并更新孩子结点,即让孩子结点到父亲的位置,并求出此时父亲的大小,继续比较,直到孩子结点为0停止,也就是到根了;反之如果比父亲结点小就停止循环,不用调整。

1.6 堆的删除

void HeapPop(Heap* hp)// 堆的删除,并保持它是一个堆
{
    
    
	assert(hp);
	assert(hp->size > 0);
	Swap(&hp->a[0],&hp->a[hp->size - 1]);
	hp->size--;
	AdjustDown(hp->a, hp->size, 0);
}

堆的删除主要用于TopK问题,找最大或最小的前K个,根位置与最后一个交换,方便删除)时间复杂度为O(logN)。
需要断言两次,堆为空不能删除。调用Swap函数,把堆顶数据和最后一个数据交换,再–,删掉数据,再调用向下调整算法,进行调整。

1.7堆的向下调整算法

void AdjustDown(HPDataType* a, int n, int parent)//向下调整
{
    
    
	assert(a);
	int child = 2 * parent + 1;
	while (child<n)
	{
    
    
		if (child + 1 < n&&a[child + 1] > a[child])//确认指向的是那个大孩子
		{
    
    
			child++;
		}
		if (a[child]>a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			parent = child;
			child = 2 * parent + 1;
		}
		else
		{
    
    
			break;
		}
	}

}

向下调整算法有一个条件:左右子树必须是一个堆,才能调整。正好可以被堆删除函数(因为它除去堆顶,左右是一个堆,但整体不是堆)调用。如图:
在这里插入图片描述
整体思路是:假设是大堆,从根节点即父亲结点开始,和他两个孩子结点最大的比较,从上到下调整。我先假设左孩子child就是最大,之后再来个if函数进行比较,如果child + 1 < n,确保有右孩子,并且child+1如果大于child,说明右孩子大,就++。之后如果最大的孩子比父亲节点大,那就调整,并更新父亲结点,让父亲到孩子结点的位置,并求出它下一个孩子结点下标,如果小于就停止。

1.8 取堆顶的数据

HPDataType HeapTop(Heap* hp)// 取堆顶的数据
{
    
    
	assert(hp);
	assert(hp->size > 0);
	return hp->a[0];
}

取堆顶数据直接返回0位置的数据。

1.9 堆的数据个数

int HeapSize(Heap* hp)// 堆的数据个数
{
    
    
	assert(hp);
	return hp->size;
}

取堆的数据个数就更简单,直接返回hp->size。

2.0 堆的判空

bool HeapEmpty(Heap* hp)// 堆的判空
{
    
    
	assert(hp);
	return hp->size == 0;
}

判空如果size为0,即为空。

(3) 堆实现代码

(1)Heap.h

#pragma once 
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
#include<string.h>
typedef int HPDataType;
typedef struct Heap
{
    
    
	HPDataType* a;
	int size;
	int capacity;
}Heap;

void Swap(HPDataType* p1, HPDataType* p2);//交换数据
void HeapPrintf(Heap* hp);//堆的打印
void HeapInit(Heap* hp);//堆的初始化

void HeapDestroy(Heap* hp);// 堆的销毁

void HeapPush(Heap* hp, HPDataType x);// 堆的插入,并保持它是一个堆(O(logN))
void AdjustUp(HPDataType* a, int child);//向上调整

void HeapPop(Heap* hp);// 堆的删除,并保持它是一个堆(用于TopK,找最大或最小的前K个)(根位置与最后一个交换,方便删除)O(logN)s
void AdjustDown(HPDataType* a,int n,int parent);//向下调整
HPDataType HeapTop(Heap* hp);// 取堆顶的数据

HPDataType HeapSize(Heap* hp);// 堆的数据个数

bool HeapEmpty(Heap* hp);// 堆的判空

void HeapCreate(Heap* hp, HPDataType* aa, int n);// 堆的构建

(2)Heap.c

#include"Heap.h"
void Swap(HPDataType* p1, HPDataType* p2)//交换数据
{
    
    
	HPDataType tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void HeapPrintf(Heap* hp)//堆的打印
{
    
    
	assert(hp);
	for (int i = 0; i < hp->size; i++)
	{
    
    
		printf("%d ", hp->a[i]);
	}
	printf("\n");
}
void HeapInit(Heap* hp)//堆的初始化
{
    
    
	assert(hp);
	hp->a =NULL;
	hp->capacity = hp->size = 0;
}
void HeapDestroy(Heap* hp)// 堆的销毁
{
    
    
	assert(hp);
	free(hp->a);
	hp->a = NULL;
	hp->capacity = hp->size = 0;
}
void AdjustUp(HPDataType* a, int child)//向上调整
{
    
    
	int parent = (child - 1) / 2;
	while (child > 0)
	{
    
    
		if (a[child] > a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
    
    
			break;
		}
	}

}

void HeapPush(Heap* hp, HPDataType x)// 堆的插入(相当于在建堆),并保持它是一个堆(O(logN))
{
    
    
	assert(hp);
	if (hp->capacity == hp->size)//扩容
	{
    
    
		int newcapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
		HPDataType* tmp = (HPDataType*)realloc(hp->a, sizeof(HPDataType)*newcapacity);
		if (tmp == NULL)
		{
    
    
			perror("realloc fail ");
			exit(-1);
		}
		hp->a = tmp;
		hp->capacity = newcapacity;
	}
	hp->a[hp->size] = x;
	hp->size++;
	AdjustUp(hp->a, hp->size-1);
}

void AdjustDown(HPDataType* a, int n, int parent)//向下调整
{
    
    
	assert(a);
	int child = 2 * parent + 1;
	while (child<n)
	{
    
    
		if (child + 1 < n&&a[child + 1] > a[child])//确认指向的是那个大孩子
		{
    
    
			child++;
		}
		if (a[child]>a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			parent = child;
			child = 2 * parent + 1;
		}
		else
		{
    
    
			break;
		}
	}

}
void HeapPop(Heap* hp)// 堆的删除,并保持它是一个堆(用于TopK,找最大或最小的前K个)(根位置与最后一个交换,方便删除)O(logN)
{
    
    
	assert(hp);
	assert(hp->size > 0);
	Swap(&hp->a[0],&hp->a[hp->size - 1]);
	hp->size--;
	AdjustDown(hp->a, hp->size, 0);
}
HPDataType HeapTop(Heap* hp)// 取堆顶的数据
{
    
    
	assert(hp);
	assert(hp->size > 0);
	return hp->a[0];
}

int HeapSize(Heap* hp)// 堆的数据个数
{
    
    
	assert(hp);
	return hp->size;
}
bool HeapEmpty(Heap* hp)// 堆的判空
{
    
    
	assert(hp);
	return hp->size == 0;
}

void HeapCreate(Heap* hp, HPDataType* aa, int n)// 堆的构建 最优算法
{
    
    
	assert(hp);
    hp->a = (HPDataType*)malloc(sizeof(HPDataType)*n);
	if (hp->a == NULL)
	{
    
    
		perror("malloc fail");
		exit(-1);
	}
  memcpy(hp->a, aa, sizeof(HPDataType)*n);
	hp->capacity = hp->size = n;
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
    
    
		AdjustDown(hp->a, n, i);
	}
}

(3)Test.c

#include"Heap.h"
void Test1()
{
    
    
	int arr[] = {
    
     56, 99, 14, 23, 53, 87, 22, 36, 45, 12 };
	Heap  hp;
	HeapInit(&hp);
	for (int i = 0; i< sizeof(arr) / sizeof(arr[0]); i++)
	{
    
    
		HeapPush(&hp, arr[i]);//建立大堆
	}
	HeapPrintf(&hp);


	while (!HeapEmpty(&hp))
	{
    
    
		printf("%d ", HeapTop(&hp));//从大到小打印
		HeapPop(&hp);
	}
	printf("\n");
	HeapDestroy(&hp);

}
void Test2()
{
    
    
	int array[] = {
    
     27, 15, 19, 18, 28, 34, 65, 49, 25, 37 };
	Heap hp;
	HeapCreate(&hp, array, sizeof(array) / sizeof(int));
	HeapPrintf(&hp);

	HeapDestroy(&hp);
}
int main()
{
    
    
	Test1();
	Test2();
	return 0;
}

(4)堆测试结果

在这里插入图片描述

三、建堆比较

(1)建堆方法

建堆可以有两种方法:
第一种是用堆的插入(里面简介使用向上调整)进行建堆,但效率不高,时间复杂度为O(N*logN)。
第二种是向下调整算法,因为使用必须满足左右子树必须是一个堆,才能调整。所以需要倒着进行向下调整,从非叶子结点开始。最后一个结点下标是n-1,有孩子算父亲结点,再减1除2,找到非叶子结点,之后就从这开始,每减一次1,就向下调整,直到为0,就能使之成为大堆或小堆。它的时间复杂度为O(N)
如下代码:

//第一种
void HeapCreate(Heap* hp, HPDataType* aa, int n)//时间复杂度不优
{
    
    
	aasert(hp);
	HeapInit(hp);
	for (int i = 0; i < n; i++)
	{
    
    
		HeapPush(hp, aa[i]);(效率不高)(N*logN)-时间复杂度
	}
}
//第二种
void HeapCreate(Heap* hp, HPDataType* aa, int n)//最优算法
{
    
    
	assert(hp);
    hp->a = (HPDataType*)malloc(sizeof(HPDataType)*n);
	if (hp->a == NULL)
	{
    
    
		perror("malloc fail");
		exit(-1);
	}
  memcpy(hp->a, aa, sizeof(HPDataType)*n);
	hp->capacity = hp->size = n;
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
    
    
		AdjustDown(hp->a, n, i);
	}
}

(2)向下调整建堆时间复杂度证明

因为堆是完全二叉树,而满二叉树也是完全二叉树,此处为了简化使用满二叉树来证明。向下调整建堆 O(N)
在这里插入图片描述
我们用的是向下调整算法从倒数第一层进行,也就是h-1层到根节点。(按最坏的情况)
第h-1层:2^h-2个结点,需要向下移动1层。
第h-2层:2^h-3个结点,需要向下移动2层。

第4层:2^3个结点,需要向下移动h-4层
第3层:2^2个结点,需要向下移动h-3层
第2层:2^1个结点,需要向下移动h-2层
第1层:2^0个结点,需要向下移动h-1层
得到:
在这里插入图片描述
用错位相减2-1得到:

在这里插入图片描述

向上调整建堆  N*logN
	for (int i = 1; i < n; ++i)
	{
    
    
	AdjustUp(a, i);
	}

向下调整建堆 O(N)
	for (int i = (n - 1 - 1) / 2; i >= 0; --i)
	{
    
    
		AdjustDown(a, n, i);
	}

向下和向上时间复杂度比较,向下更优,所以建堆用向下调整建。

四、堆排序

(1)堆排序建堆

如果降序,就建小堆
如果升序,就建大堆

(2)堆排序思想

堆排序用堆删除思想,堆删除是在已经是大堆或小堆的条件下,把堆顶和尾进行交换,再把它删除。我们就利用这一思想,把大的或小的换到后面,不删除最后一个即保留它,到最后就形成了从小到大或从大到小的序列。而且它们都是用了向下调整。

用堆排序也是倒着用向下调整算法去排,在调整的过程中,因为最后一层结点数占了总结点一半,向下调整是从倒数二层开始,最后一层所有结点直接跳过并且高度越高调整的次数越少,更快。比如倒数第二层只调整1次

(3)堆排序实现核心代码

void HeapSort(int* a, int n)
{
    
    

	for (int i = (n - 1 - 1) / 2; i >= 0; --i)
	{
    
    
		AdjustDown(a, n, i);
	}// 向下调整建堆 时间复杂度O(N)

	int end = n - 1;
	while (end > 0)	//时间复杂度 O(N*logN)
	{
    
    
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		--end;
	}
}

首先用向下调整算法建堆,之后用变量end记录最后一个数的下标,交换之后,用向下调整算法恢复原来的大堆或小堆,最后需要保留最后一个数据,直接end–,即是把倒数第二个赋为end。相当于假删除。

(4)堆排序实现代码

(1)HeapSort.c

#include<stdio.h>
#include<assert.h>
void Swap(int* p1, int* p2)//交换数据
{
    
    
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void AdjustDown(int* a, int n, int parent)//向下调整
{
    
    
	assert(a);
	int child = 2 * parent + 1;
	while (child<n)
	{
    
    
		if (child + 1 < n&&a[child + 1] > a[child])//确认指向的是那个大孩子
		{
    
    
			child++;
		}
		if (a[child]>a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			parent = child;
			child = 2 * parent + 1;
		}
		else
		{
    
    
			break;
		}
	}

}
void HeapSort(int* a, int n)
{
    
    
	
	for (int i = (n - 1 - 1) / 2; i >= 0; --i)
	{
    
    
		AdjustDown(a, n, i);
	}

	int end = n - 1;
	while (end > 0)
	{
    
    
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		--end;
	}
}

void TestHeap1()
{
    
    
	int array[] = {
    
     26, 14, 17, 59, 29, 33, 99, 46, 22, 36 };
	HeapSort(array, sizeof(array) / sizeof(int));

	for (int i = 0; i < sizeof(array) / sizeof(int); ++i)
	{
    
    
		printf("%d ", array[i]);
	}
	printf("\n");
}
int main()
{
    
    
	TestHeap1();
	return 0;
}

(2)堆排序测试结果

在这里插入图片描述

五、Top-K

(1)认识TopK

求TOP-K问题就是求数据集合中前K个最大的元素或者最小的元素,一般情况下数据量都比较大。
对于Top-K问题,可以用最简单直接的方式就是排序,但如果数据量非常大,排序就不可取(数据量太多都不能一下子全部加载到内存中)。可以用堆来解决。
基本思路是:

1、用数据集合中前K个元素来建堆
如果找前K个最大元素建小堆,反之建大堆
2、依次遍历剩余的N-K个元素与堆顶元素来比较,如果比堆顶大就替换,最后向下调整。
3、将剩余N-K个元素依次与堆顶元素比完之后,堆中剩余的K个元素就是所求的前K个最小或者最大的元素。

(2)核心代码及解析

void TestHeap()
{
    
    
	// 造数据
	int n, k;
	printf("请输入n和k:>");
	scanf("%d%d", &n, &k);
	srand(time(0));
	FILE* fin = fopen("data.txt", "w");//创建文件
	if (fin == NULL)
	{
    
    
		perror("fopen fail");
		return;
	}
	for (size_t i = 0; i < n; ++i)
	{
    
    
		int val = rand() ;
		fprintf(fin, "%d\n", val);//把随机产生的数写入fin中
	}
	fclose(fin);


	FILE* fout = fopen("data.txt", "r");//找Topk,先以读的方式打开
	if (fout == NULL)
	{
    
    
		perror("fopen fail");
		return;
	}

	
	int* minHeap = malloc(sizeof(int)*k);//开辟k个空间
	if (minHeap == NULL)
	{
    
    
		perror("malloc fail");
		return;
	}

	for (int i = 0; i < k; ++i)
	{
    
    
		fscanf(fout, "%d", &minHeap[i]);//从文件中依次先读k个数据到minHeap中
	}


	for (int i = (k - 1 - 1) / 2; i >= 0; --i)
	{
    
    
		AdjustDown(minHeap, k, i);	// 建小堆
	}

	int val = 0;
	while (fscanf(fout, "%d", &val) != EOF)
	{
    
    
		if (val > minHeap[0])//在N-K个数据里读一个数到val中,与堆顶进行比较
		{
    
    
			minHeap[0] = val;
			AdjustDown(minHeap, k, 0);
		}
	}

	for (int i = 0; i < k; ++i)
	{
    
    
		printf("%d ", minHeap[i]);//打印前K个最大元素
	}
	printf("\n");
	fclose(fout);
}

假设求前K个最大元素
1、先造数据,这里用到了rand函数,并使用srand函数设置随机数种子,生成随机数。先以写的方式创建一个文件,把生成的随机数写到该文件中,关闭文件。
2、之后再以读的方式打开,并开辟K个空间,把文件前K个数据读到k个空间里。
3、再对这个K个数据进行建小堆
4、再从N-K个数据 ,剩下的数据中依次读到变量val中,与堆顶进行比较,如果比堆顶大,就交换,并进行向下调整。这样在堆中大的数据就沉到了底下。
5、打印数据。

(3)TopK完整代码

#include<assert.h>
#include<time.h>
void Swap(int* p1, int* p2)//交换数据
{
    
    
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void AdjustDown(int* a, int n, int parent)//向下调整
{
    
    
	assert(a);
	int child = 2 * parent + 1;
	while (child<n)
	{
    
    
		if (child + 1 < n&&a[child + 1] <a[child])//确认指向的是那个大孩子
		{
    
    
			child++;
		}
		if (a[child]<a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			parent = child;
			child = 2 * parent + 1;
		}
		else
		{
    
    
			break;
		}
	}

}
void TestHeap()
{
    
    
	// 造数据
	int n, k;
	printf("请输入n和k:>");
	scanf("%d%d", &n, &k);
	srand(time(0));
	FILE* fin = fopen("data.txt", "w");//创造文件
	if (fin == NULL)
	{
    
    
		perror("fopen fail");
		return;
	}
	for (size_t i = 0; i < n; ++i)
	{
    
    
		int val = rand() ;
		fprintf(fin, "%d\n", val);//把随机产生的数写入fin中
	}
	fclose(fin);


	FILE* fout = fopen("data.txt", "r");//找Topk,先以读的方式打开
	if (fout == NULL)
	{
    
    
		perror("fopen fail");
		return;
	}

	
	int* minHeap = malloc(sizeof(int)*k);//开辟k个空间
	if (minHeap == NULL)
	{
    
    
		perror("malloc fail");
		return;
	}

	for (int i = 0; i < k; ++i)
	{
    
    
		fscanf(fout, "%d", &minHeap[i]);//从文件中依次先读k个数据到minHeap中
	}


	for (int i = (k - 1 - 1) / 2; i >= 0; --i)
	{
    
    
		AdjustDown(minHeap, k, i);	// 建小堆
	}

	int val = 0;
	while (fscanf(fout, "%d", &val) != EOF)
	{
    
    
		if (val > minHeap[0])//在N-K个数据里读一个数到val中,与堆顶进行比较
		{
    
    
			minHeap[0] = val;
			AdjustDown(minHeap, k, 0);
		}
	}

	for (int i = 0; i < k; ++i)
	{
    
    
		printf("%d ", minHeap[i]);//打印前K个最大元素
	}
	printf("\n");
	fclose(fout);
}

int main()
{
    
    
	TestHeap();
	return 0;
}

(4)测试结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_59292239/article/details/127965106