1.头函数
#pragma once
typedef int HPDataType;
typedef struct Heap {
HPDataType* a;
int size;
int capacity;
}Heap;
int (*com)(int a, int b);
//函数指针的尝试,用com1函数和com2函数作为一次尝试
//int Comp1(int a, int b); //a比b大则返回1,否则返回0
//int Comp2(int a, int b); //a比b大则返回0,否则返回1,这一段暂时不用看
//交换
void Swap(HPDataType* left, HPDataType* right);
//向下调整
void AdjustDown(Heap* hp, int parent);
//向上调整
void ADjustUp(Heap* hp);
// 堆的构建
void HeapCreate(Heap* hp, HPDataType* a, int n);
// 堆的销毁
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 HeapCapacity(Heap* hp);
//测试
void Test();
2.函数的实现
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include "heap.h"
//交换
void Swap(HPDataType* left, HPDataType* right) {
int temp = *left;
*left = *right;
*right = temp;
}
int Comp1(int a, int b) {
return a > b ? 1 : 0;
}
int Comp1(int a, int b) {
return b > a ? 1 : 0;
}
//扩容
void HeapCapacity(Heap* hp) {
assert(hp);
hp->capacity *= 2;
hp->a = (HPDataType*)realloc(hp->a, sizeof(HPDataType) * hp->capacity);
if (hp->a == NULL) {
printf("出问题了");
}
}
//向下调整
void AdjustDown(Heap* hp, int parent) {
//1.找到较小的孩子节点
int child = parent * 2 + 1;
while (child < hp->size) {
if (child + 1 < hp->size && hp->a[child + 1] < hp->a[child]) {
child += 1;
}
if (hp->a[child] < hp->a[parent]) {
Swap(&hp->a[child], &hp->a[parent]);
parent = child;
child = parent * 2 + 1;
}
else {
return;
}
}
}
//向上调整
void ADjustUp(Heap* hp) {
assert(hp);
int child = hp->size - 1;
int parent = (child - 1) / 2;
while (child > 0) {
if (hp->a[child] < hp->a[parent]) {
Swap(&hp->a[child], &hp->a[parent]);
child = parent;
parent = (child - 1) / 2;
}
else {
return;
}
}
}
// 堆的构建
void HeapCreate(Heap* hp, HPDataType* a, int n) {
//1.将堆的常规初始化
hp->capacity = n;
hp->a = (HPDataType*)malloc(sizeof(HPDataType) * hp->capacity);
if (hp->a == NULL) {
assert(0);
return;
}
hp->size = n;
memcpy(hp->a, a, n * sizeof(int));
//2.排序
int root = (hp->size - 2) / 2;
for (int i = root; i >= 0; i--) {
AdjustDown(hp, i);
}
}
// 堆的删除
void HeapPop(Heap* hp) {
assert(hp);
//1.交换堆顶和最后一个节点
int size = hp->size;
Swap(&hp->a[size - 1], &hp->a[0]);
hp->size--;
//2.堆顶向下调整
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) {
assert(hp);
//是空就返回1,不是空就返回0;
return hp->size == 0;
}
// 堆的插入
void HeapPush(Heap* hp, HPDataType x) {
assert(hp);
//1.判断堆是否满了,如果满了,则进行扩容
if (hp->capacity == hp->size) {
HeapCapacity(hp);
}
//2.将新加的元素放在堆的末尾
int size = hp->size;
hp->a[size] = x;
hp->size++;
//3.向上调整
ADjustUp(hp);
}
// 堆的销毁
void HeapDestory(Heap* hp) {
assert(hp);
if (hp->size != 0) {
free(hp->a);
hp->capacity = 0;
hp->size = 0;
}
}
//测试函数
void Test() {
Heap hp;
int arr[] = { 11, 7, 4, 2, 3 };
com = Comp1;
HeapCreate(&hp, arr, sizeof(arr) / sizeof(arr[0]));
HeapPop(&hp);
printf("%d ", HeapTop(&hp));
printf("%d ", HeapEmpty(&hp));
printf("%d ", HeapSize(&hp));
HeapPush(&hp, 1);
HeapPush(&hp, 20);
HeapDestory(&hp);
}
3.主函数(没啥用,运行要用,仅此而已)
#include "heap.h"
int main() {
Test();
}