一、堆的定义
堆是一种完全二叉树的数据结构,可以分为大堆和小堆,基于堆可以实现堆排序
大堆:父亲节点都大于等于孩子节点的值
小堆:父亲节点都小于等于孩子节点的值
堆在物理结构上是数组存储,但逻辑结构上是二叉树
二、堆的性质:
父节点->子节点:i-左节点:2i+1 右节点:2i+2
子节点->父节点:i-(i-1)/2
三、有关堆的实现:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include <stdbool.h>
typedef int HPDataType;
typedef struct Heap
{
HPDataType* a;
int size;
int capacity;
}Heap;
//堆的初始化
void HeapInit(Heap* hp);
// 堆的销毁
void HeapDestory(Heap* hp);
// 堆的插入
void HeapPush(Heap* hp, HPDataType x);
// 堆的删除
void HeapPop(Heap* hp);
// 取堆顶的数据
HPDataType HeapTop(Heap* hp);
// 堆的数据个数
int HeapSize(Heap* hp);
// 堆的判空
bool HeapEmpty(Heap* hp);
#include"heap.h"
void swap(int* a, int* b)
{
int tem = *a;
*a = *b;
*b = tem;
}
void HeapInit(Heap* hp)
{
hp->a = NULL;
hp->capacity = 0;
hp->size = 0;
}
// 堆的销毁
void HeapDestory(Heap* hp)
{
free(hp->a);
hp->a = NULL;
hp->capacity = hp->size = 0;
}
//建立大堆
void AdjustDown(HPDataType* a, int parent, int n)
{
int child = parent * 2 + 1;
while (child < n)
{
if (child + 1 < n && a[child + 1] > a[child])
{
child++;
}
if (a[child] > a[parent])
{
swap(&a[child], &a[parent]);
}
else
{
break;
}
parent = child;
child = child * 2 + 1;
}
}
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)
{
assert(hp);
if (hp->capacity == hp->size)
{
int newcapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
HPDataType* tem = (HPDataType*)realloc(hp->a, sizeof(HPDataType)*newcapacity);
if (tem == NULL)
{
perror("realloc fail");
return;
}
hp->capacity = newcapacity;
hp->a = tem;
}
hp->a[hp->size++] = x;
AdjustUp(hp->a, hp->size - 1);
}
// 堆的删除
void HeapPop(Heap* hp)
{
assert(hp);
swap(&hp->a[0], &hp->a[hp->size - 1]);
hp->size--;
AdjustDown(hp->a, 0, hp->size);
}
// 取堆顶的数据
HPDataType HeapTop(Heap* hp)
{
assert(hp);
assert(!HeapEmpty(hp));
return hp->a[0];
}
// 堆的数据个数
int HeapSize(Heap* hp)
{
assert(hp);
return hp->size;
}
// 堆的判空
bool HeapEmpty(Heap* hp)
{
return hp->size == 0;
}
需要注意的是,向堆中添加数据是用的向上调整,删除数据时用向下调整
向上建堆:
for (int i = 1; i < n; i++)
{
AdjustUp(a, i);
}
向下建堆:
for (int i = (n - 1 - 1) / 2; i >= 0; i--)
{
AdjustDown(a, i, n);
}
这里的n是数组元素个数,在这里直接给出结论,向下建堆时间复杂度是logn,向上建堆尾nlogn
四、TopK问题:
void CreateNDate()
{
// 造数据
int n = 10000;
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)
{
//建立小堆
char* file = "data.txt";
FILE* fout = fopen(file, "r");
if (fout == NULL)
{
perror("fopen error");
return;
}
int* minheap = (int*)malloc(sizeof(int) * k);
if (minheap == NULL)
{
return;
}
for (int i = 0; i < k; i++)
{
fscanf_s(fout, "%d", &minheap[i]);
}
for (int i = (k - 1 - 1) / 2; i >= 0; i--)
{
AdjustDown(minheap,i,k);
}
int val = 0;
while (!feof(fout))
{
fscanf_s(fout,"%d",&val);
if (val > minheap[0])
{
minheap[0]=val;
AdjustDown(minheap, 0, k);
}
}
for (int i = 0; i < k; i++)
{
printf("%d ", minheap[i]);
}
}
先建立k个数据的小堆,之后与堆顶数据比较,比堆顶数据大就进行向下调整重新建堆
还有一个有关堆排序的放到之后的排序中总结