hello-algo/codes/c/chapter_heap/my_heap.c

153 lines
3.6 KiB
C
Raw Normal View History

2023-01-15 22:54:34 +08:00
/**
* File: my_heap.c
* Created Time: 2023-01-15
* Author: Reanon (793584285@qq.com)
*/
#include "../utils/common.h"
2023-01-15 22:54:34 +08:00
#define MAX_SIZE 5000
2023-02-07 19:05:19 +08:00
/* 大顶堆 */
typedef struct {
2023-01-15 22:54:34 +08:00
// size 代表的是实际元素的个数
int size;
// 使用预先分配内存的数组,避免扩容
int data[MAX_SIZE];
} MaxHeap;
// 函数声明
2023-11-01 05:14:22 +08:00
void siftDown(MaxHeap *maxHeap, int i);
void siftUp(MaxHeap *maxHeap, int i);
int parent(MaxHeap *maxHeap, int i);
/* 构造函数,根据切片建堆 */
MaxHeap *newMaxHeap(int nums[], int size) {
2023-01-15 22:54:34 +08:00
// 所有元素入堆
2023-11-01 05:14:22 +08:00
MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));
maxHeap->size = size;
memcpy(maxHeap->data, nums, size * sizeof(int));
for (int i = parent(maxHeap, size - 1); i >= 0; i--) {
// 堆化除叶节点以外的其他所有节点
2023-11-01 05:14:22 +08:00
siftDown(maxHeap, i);
2023-01-15 22:54:34 +08:00
}
2023-11-01 05:14:22 +08:00
return maxHeap;
2023-01-15 22:54:34 +08:00
}
/* 析构函数 */
2023-11-01 05:14:22 +08:00
void delMaxHeap(MaxHeap *maxHeap) {
// 释放内存
2023-11-01 05:14:22 +08:00
free(maxHeap);
}
/* 获取左子节点索引 */
2023-11-01 05:14:22 +08:00
int left(MaxHeap *maxHeap, int i) {
2023-01-15 22:54:34 +08:00
return 2 * i + 1;
}
/* 获取右子节点索引 */
2023-11-01 05:14:22 +08:00
int right(MaxHeap *maxHeap, int i) {
2023-01-15 22:54:34 +08:00
return 2 * i + 2;
}
/* 获取父节点索引 */
2023-11-01 05:14:22 +08:00
int parent(MaxHeap *maxHeap, int i) {
2023-01-15 22:54:34 +08:00
return (i - 1) / 2;
}
/* 交换元素 */
2023-11-01 05:14:22 +08:00
void swap(MaxHeap *maxHeap, int i, int j) {
int temp = maxHeap->data[i];
maxHeap->data[i] = maxHeap->data[j];
maxHeap->data[j] = temp;
2023-01-15 22:54:34 +08:00
}
/* 获取堆大小 */
2023-11-01 05:14:22 +08:00
int size(MaxHeap *maxHeap) {
return maxHeap->size;
2023-01-15 22:54:34 +08:00
}
/* 判断堆是否为空 */
2023-11-01 05:14:22 +08:00
int isEmpty(MaxHeap *maxHeap) {
return maxHeap->size == 0;
2023-01-15 22:54:34 +08:00
}
/* 访问堆顶元素 */
2023-11-01 05:14:22 +08:00
int peek(MaxHeap *maxHeap) {
return maxHeap->data[0];
2023-01-15 22:54:34 +08:00
}
/* 元素入堆 */
2023-11-01 05:14:22 +08:00
void push(MaxHeap *maxHeap, int val) {
// 默认情况下,不应该添加这么多节点
2023-11-01 05:14:22 +08:00
if (maxHeap->size == MAX_SIZE) {
2023-01-15 22:54:34 +08:00
printf("heap is full!");
2023-04-17 22:15:06 +08:00
return;
2023-01-15 22:54:34 +08:00
}
// 添加节点
2023-11-01 05:14:22 +08:00
maxHeap->data[maxHeap->size] = val;
maxHeap->size++;
2023-01-15 22:54:34 +08:00
// 从底至顶堆化
2023-11-01 05:14:22 +08:00
siftUp(maxHeap, maxHeap->size - 1);
2023-01-15 22:54:34 +08:00
}
/* 元素出堆 */
2023-11-01 05:14:22 +08:00
int pop(MaxHeap *maxHeap) {
2023-01-15 22:54:34 +08:00
// 判空处理
2023-11-01 05:14:22 +08:00
if (isEmpty(maxHeap)) {
2023-01-15 22:54:34 +08:00
printf("heap is empty!");
2023-04-18 14:31:23 +08:00
return INT_MAX;
2023-01-15 22:54:34 +08:00
}
// 交换根节点与最右叶节点(交换首元素与尾元素)
2023-11-01 05:14:22 +08:00
swap(maxHeap, 0, size(maxHeap) - 1);
// 删除节点
2023-11-01 05:14:22 +08:00
int val = maxHeap->data[maxHeap->size - 1];
maxHeap->size--;
2023-01-15 22:54:34 +08:00
// 从顶至底堆化
2023-11-01 05:14:22 +08:00
siftDown(maxHeap, 0);
2023-01-15 22:54:34 +08:00
// 返回堆顶元素
return val;
}
/* 从节点 i 开始,从顶至底堆化 */
2023-11-01 05:14:22 +08:00
void siftDown(MaxHeap *maxHeap, int i) {
2023-01-15 22:54:34 +08:00
while (true) {
// 判断节点 i, l, r 中值最大的节点,记为 max
2023-11-01 05:14:22 +08:00
int l = left(maxHeap, i);
int r = right(maxHeap, i);
2023-01-16 10:17:55 +08:00
int max = i;
2023-11-01 05:14:22 +08:00
if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {
2023-01-15 22:54:34 +08:00
max = l;
}
2023-11-01 05:14:22 +08:00
if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {
2023-01-15 22:54:34 +08:00
max = r;
}
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
2023-01-15 22:54:34 +08:00
if (max == i) {
break;
}
// 交换两节点
2023-11-01 05:14:22 +08:00
swap(maxHeap, i, max);
2023-01-15 22:54:34 +08:00
// 循环向下堆化
i = max;
}
}
/* 从节点 i 开始,从底至顶堆化 */
2023-11-01 05:14:22 +08:00
void siftUp(MaxHeap *maxHeap, int i) {
2023-01-15 22:54:34 +08:00
while (true) {
// 获取节点 i 的父节点
2023-11-01 05:14:22 +08:00
int p = parent(maxHeap, i);
// 当“越过根节点”或“节点无须修复”时,结束堆化
2023-11-01 05:14:22 +08:00
if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {
2023-01-15 22:54:34 +08:00
break;
}
// 交换两节点
2023-11-01 05:14:22 +08:00
swap(maxHeap, i, p);
2023-01-15 22:54:34 +08:00
// 循环向上堆化
i = p;
}
}