1、什么是堆
这里说的堆并不是操作系统中的堆区,而是指将一个数的集合,按照完全二叉树的顺序存储在一个一维数组里,并满足一些条件的就是堆。
堆的性质
堆就是一颗完全二叉树,但是完全二叉树不一定是堆。堆有大根堆和小根堆两种,如果不满足这两种堆的性质,就不是堆。
大堆:树中所有的父亲节点的值都大于等于孩子结点的值。
小堆:树中所有的父亲节点的值都小于等于孩子结点的值。
2、堆的实现
堆的实现有多种方式,这里我用数组实现,大体的实现逻辑和顺序表有些类似,最主要的差别就是实现Push和Pop这两个函数接口的时候,要调整堆的元素,插入元素时,插入到数组末尾,然后向上调整;删除数据,堆删除数据是删除堆顶的数据,思路是:将堆顶数据与数组末尾数据交换,删除末尾数据,然后向下调整。
代码实现
(1)Heap.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <time.h>
//数组实现堆
//
//结构声明
typedef int HPDataType;
typedef struct Heap
{
HPDataType* a;
int size;
int capacity;
}Heap;
//函数声明
void HeapInit(Heap* hp);
void HeapDestroy(Heap* hp);
void HeapPush(Heap* hp, HPDataType x);
void HeapPop(Heap* hp);
void HeapPrint(Heap* hp);
//向上调整
void AdjustUp(HPDataType* a, int num,int child);//num是数组大小
void Swap(HPDataType* x, HPDataType* y);
void AdjustDown(HPDataType* a, int num,int parent);
bool HeapEmpty(Heap* hp);
HPDataType HeapTop(Heap* hp);
int HeapSize(Heap* hp);
(2)Heap.c
#include "heap.h"
void HeapInit(Heap* hp)
{
assert(hp);
hp->a = NULL;
hp->size = hp->capacity = 0;
}
void HeapDestroy(Heap* hp)
{
assert(hp);
free(hp->a);
hp->size = hp->capacity = 0;
hp->a = NULL;
}
void HeapPush(Heap* hp, HPDataType x)
{
assert(hp);
//判断空间是否够
if (hp->size == hp->capacity)
{
int newCapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
HPDataType* tmp = realloc(hp->a, sizeof(HPDataType) * newCapacity);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
hp->capacity = newCapacity;
hp->a = tmp;
}
//插入数据
//向上调整
hp->a[hp->size] = x;
hp->size++;
AdjustUp(hp->a,hp->size,hp->size-1);
}
void AdjustUp(HPDataType* a,int num,int child)
{
assert(a);
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;
}
}
}
//删除,是要删堆顶的数据
//1、将堆顶的数与a[size-1]的数据交换,删掉a[size-1]
//2、向下调整
void HeapPop(Heap* hp)
{
assert(hp);
assert(!HeapEmpty(hp));
//交换
Swap(&hp->a[0],&hp->a[hp->size-1]);
//删除
hp->size--;
//向下调整
AdjustDown(hp->a,hp->size,0);
}
void AdjustDown(HPDataType* a,int num,int parent)
{
int child = 2 * parent + 1;
while (child<num)
{
//如果是大堆就找左右孩子中最大的孩子反之找小的
if (child+1<num && 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 Swap(HPDataType* x, HPDataType* y)
{
HPDataType tmp = *x;
*x = *y;
*y = tmp;
}
void HeapPrint(Heap* hp)
{
assert(hp);
for (int i = 0; i < hp->size; i++)
{
printf("%d ", hp->a[i]);
}
printf("\n");
}
bool HeapEmpty(Heap* hp)
{
assert(hp);
return hp->size == 0;
}
HPDataType HeapTop(Heap* hp)
{
assert(hp);
assert(!HeapEmpty(hp));
return hp->a[0];
}
int HeapSize(Heap* hp)
{
assert(hp);
return hp->size;
}
3、Topk问题
Topk问题是指在N个数中找出最大的前k个数或在N个数中找出最小的前k个数,
一般N远远大于k,所以用排序找的话效率较低,下面给出堆的解决思路:
假设现在要找最大的前k个数
(1)建立1个k个数据的小堆
(2)剩下的N-k个数分别依次与堆顶的数比较,如果比堆顶的数大,就和堆顶的数交换,然后向下调整。
(3)最后堆中的k个数就是最大的前k个数,因为每一次的比较和交换、调整,都会把大的数“沉到”堆底。
代码实现:
堆的实现上面已经给出,所以这里只展示解决Topk问题的函数接口
void HeapPrintTopk(int* a, int N, int k)
{
Heap hp;
HeapInit(&hp);
//建立k个数的小堆
for (int i = 0; i < k; i++)
{
HeapPush(&hp,a[i]);
}
//与堆顶的的数据比较,比堆顶大就交换,向下调整
for (int i = k; i < N; i++)
{
if (a[i] > HeapTop(&hp))
{
HeapPop(&hp);
HeapPush(&hp, a[i]);
//也可以直接操作调整的函数接口,但是不建议这么做
//hp.a[0] = a[i];
//AdjustDown(&hp,k,0);
}
}
//此时堆中的数就是最大的前k个数
HeapPrint(&hp);
HeapDestroy(&hp);
}
给出一段验证程序的代码
void testHeapTopK()
{
srand(time(NULL));
int N = 10000;//找前10个
int k = 10;
int arr[10000] = { 0 };
for (int i = 0; i < N; i++)
{
arr[i] = rand() / 1000000;//随机产生1万个范围是0-1000000的数
}
//为了更好的验证程序是否正确,随机把其中的十个数设置成大于1000000的数
arr[234] = 1000000 + 2;
arr[294] = 1000000 + 5;
arr[2134] = 1000000 + 3;
arr[5234] = 1000000 + 1;
arr[24] = 1000000 + 8;
arr[634] = 1000000 + 9;
arr[3794] = 1000000 + 6;
arr[555] = 1000000 + 7;
arr[902] = 1000000 + 11;
arr[1564] = 1000000 + 33;
HeapPrintTopk(arr,N,k);
}
int main()
{
//testHeap();
testHeapTopK();
return 0;
}