heap.h
// 1. 堆是一个完全二叉树
// 2. 堆有两种, 一种叫小堆(小根堆, 最小堆),
// 一种叫大堆(大根堆, 最大堆).
// 3. 以小堆为例, 这个树的根节点是这个树中的最小的元素
// 对于任意一个子树来说, 子树的根节点, 小于左右孩子节点的值.
// 4. 以大堆为例, 这个树的根节点是这个树种的最大元素
// 对于任意一个子树来说, 子树的根节点, 大于左右孩子节点的值.
#pragma once
#include<stdio.h>
#define HeapMaxSize 1000
typedef int HeapType;
typedef int (*Compare)(HeapType a, HeapType b);
typedef struct Heap {
HeapType data[HeapMaxSize];
size_t size;
Compare cmp;
} Heap;
//在对堆初始化的时候,需要决定是大堆还是小堆。
void HeapInit(Heap* heap, Compare compare);
void HeapInsert(Heap* heap, HeapType value);
// 取堆顶元素
int HeapRoot(Heap* heap, HeapType* value);
// 删除堆顶元素
void HeapErase(Heap* heap);
int HeapEmpty(Heap* heap);
size_t HeapSize(Heap* heap);
void HeapDestroy(Heap* heap);
// 在我们不想开辟额外的空间, 或者消耗额外的时间的前提下,
// 如果我们想进行从小到大排序, 就需要一个大堆
// 如果我们想进行从大到小排序, 就需要一个小堆
void HeapSort(HeapType array[], size_t size);
heap.c
#include<stdio.h>
#include"heap.h"
#include<string.h>
//为小堆打造比较函数
int Less(HeapType a,HeapType b){
return a < b ? 1:0;
}
//为大堆打造比较函数
int Greater(HeapType a,HeapType b){
return a > b ? 1:0;
}
//初始化堆
void HeapInit(Heap* heap,Compare cmp){
if(heap == NULL){
return;
}
heap->size = 0;
heap->cmp = cmp;
return;
}
void Swap(HeapType* a,HeapType* b){
HeapType tmp = *a;
*a = *b;
*b = tmp;
}
//上浮式交换堆中元素
void AdjustUp(Heap* heap,size_t index){
size_t child = index;
size_t parent = (child-1)/2;
while (child>0){
if(!heap->cmp(heap->data[parent],heap->data[child])){
Swap(&heap->data[parent],&heap->data[child]);
}
else{
//如果发现某一个位置下,child和parent已经满足堆的要求,
//此时就可以停止上浮
//因为更上面的节点一定也都满足堆的要求。
break;
}
child = parent;
parent = (child - 1) / 2;
}
}
//往堆中插入元素
void HeapInsert(Heap* heap,HeapType value){
if(heap == NULL){
return;
}
if(heap->size >= HeapMaxSize){
return;
}
heap->data[heap->size++] = value;
AdjustUp(heap,heap->size-1);
return;
}
//用来打印堆中元素的辅助函数
void Print(Heap* heap){
int i = 0;
for (;i<heap->size;++i){
printf("[%c|%lu]\t",heap->data[i],i);
}
printf("\n");
}
//取堆顶元素
int HeapRoot(Heap* heap,HeapType *value){
if(heap == NULL){
return 0;
}
if(heap->size == 0){
return 0;
}
*value = heap->data[0];
return 1;
}
//下沉式调整堆中元素大小
void AdjustDown(Heap* heap,size_t index){
size_t parent = index;
size_t child = 2 * index + 1;
while(child < heap->size){
if(child + 1 < heap->size && heap->cmp(heap->data[child+1],heap->data[child])){
//如果右子树存在,且右子树比左子树更符合堆的要求
//假设我们这个是小堆,
//如果右子树比左子树小,那么就让child指向右子树
child = child + 1;
}
//child就指向了左右子树中的那个更小的元素
if(heap->cmp(heap->data[child],heap->data[parent])){
Swap(&heap->data[child],&heap->data[parent]);
}
else{
break;
}
parent = child;
child = 2 * parent + 1;
}
}
//删除堆中元素
void HeapErase(Heap* heap){
if(heap == NULL){
return;
}
if(heap->size == 0){
return;
}
//交换堆顶和最后一个元素的位置
Swap(&heap->data[0],&heap->data[heap->size-1]);
--heap->size;
//从根节点开始一次下沉
AdjustDown(heap,0);
return;
}
//创建一个新的堆
void HeapCreat(Heap* heap,HeapType array[],size_t size){
if(heap == NULL)
{
return;
}
//遍历array数组,把数组的元素依次插入到堆中
size_t i = 0;
for (;i<size;++i) {
HeapInsert(heap,array[i]);
}
return;
}
//堆排序
void HeapSort(HeapType array[],size_t size){
Heap heap;
HeapInit(&heap,Greater);
HeapCreat(&heap,array,size);
//循环的对堆进行删除
while(heap.size>0){
HeapErase(&heap);
}
//循环结束后排序结束
memcpy(array,heap.data,size*sizeof(HeapType));
}