mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-12 20:00:58 +00:00
Bug fixes to C code.
This commit is contained in:
@@ -17,111 +17,111 @@ typedef struct {
|
||||
} MaxHeap;
|
||||
|
||||
// 函数声明
|
||||
void siftDown(MaxHeap *h, int i);
|
||||
void siftUp(MaxHeap *h, int i);
|
||||
int parent(MaxHeap *h, int i);
|
||||
void siftDown(MaxHeap *maxHeap, int i);
|
||||
void siftUp(MaxHeap *maxHeap, int i);
|
||||
int parent(MaxHeap *maxHeap, int i);
|
||||
|
||||
/* 构造函数,根据切片建堆 */
|
||||
MaxHeap *newMaxHeap(int nums[], int size) {
|
||||
// 所有元素入堆
|
||||
MaxHeap *h = (MaxHeap *)malloc(sizeof(MaxHeap));
|
||||
h->size = size;
|
||||
memcpy(h->data, nums, size * sizeof(int));
|
||||
for (int i = parent(h, size - 1); i >= 0; i--) {
|
||||
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--) {
|
||||
// 堆化除叶节点以外的其他所有节点
|
||||
siftDown(h, i);
|
||||
siftDown(maxHeap, i);
|
||||
}
|
||||
return h;
|
||||
return maxHeap;
|
||||
}
|
||||
|
||||
/* 析构函数 */
|
||||
void freeMaxHeap(MaxHeap *h) {
|
||||
void delMaxHeap(MaxHeap *maxHeap) {
|
||||
// 释放内存
|
||||
free(h);
|
||||
free(maxHeap);
|
||||
}
|
||||
|
||||
/* 获取左子节点索引 */
|
||||
int left(MaxHeap *h, int i) {
|
||||
int left(MaxHeap *maxHeap, int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* 获取右子节点索引 */
|
||||
int right(MaxHeap *h, int i) {
|
||||
int right(MaxHeap *maxHeap, int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* 获取父节点索引 */
|
||||
int parent(MaxHeap *h, int i) {
|
||||
int parent(MaxHeap *maxHeap, int i) {
|
||||
return (i - 1) / 2;
|
||||
}
|
||||
|
||||
/* 交换元素 */
|
||||
void swap(MaxHeap *h, int i, int j) {
|
||||
int temp = h->data[i];
|
||||
h->data[i] = h->data[j];
|
||||
h->data[j] = temp;
|
||||
void swap(MaxHeap *maxHeap, int i, int j) {
|
||||
int temp = maxHeap->data[i];
|
||||
maxHeap->data[i] = maxHeap->data[j];
|
||||
maxHeap->data[j] = temp;
|
||||
}
|
||||
|
||||
/* 获取堆大小 */
|
||||
int size(MaxHeap *h) {
|
||||
return h->size;
|
||||
int size(MaxHeap *maxHeap) {
|
||||
return maxHeap->size;
|
||||
}
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
int isEmpty(MaxHeap *h) {
|
||||
return h->size == 0;
|
||||
int isEmpty(MaxHeap *maxHeap) {
|
||||
return maxHeap->size == 0;
|
||||
}
|
||||
|
||||
/* 访问堆顶元素 */
|
||||
int peek(MaxHeap *h) {
|
||||
return h->data[0];
|
||||
int peek(MaxHeap *maxHeap) {
|
||||
return maxHeap->data[0];
|
||||
}
|
||||
|
||||
/* 元素入堆 */
|
||||
void push(MaxHeap *h, int val) {
|
||||
void push(MaxHeap *maxHeap, int val) {
|
||||
// 默认情况下,不应该添加这么多节点
|
||||
if (h->size == MAX_SIZE) {
|
||||
if (maxHeap->size == MAX_SIZE) {
|
||||
printf("heap is full!");
|
||||
return;
|
||||
}
|
||||
// 添加节点
|
||||
h->data[h->size] = val;
|
||||
h->size++;
|
||||
maxHeap->data[maxHeap->size] = val;
|
||||
maxHeap->size++;
|
||||
|
||||
// 从底至顶堆化
|
||||
siftUp(h, h->size - 1);
|
||||
siftUp(maxHeap, maxHeap->size - 1);
|
||||
}
|
||||
|
||||
/* 元素出堆 */
|
||||
int pop(MaxHeap *h) {
|
||||
int pop(MaxHeap *maxHeap) {
|
||||
// 判空处理
|
||||
if (isEmpty(h)) {
|
||||
if (isEmpty(maxHeap)) {
|
||||
printf("heap is empty!");
|
||||
return INT_MAX;
|
||||
}
|
||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||
swap(h, 0, size(h) - 1);
|
||||
swap(maxHeap, 0, size(maxHeap) - 1);
|
||||
// 删除节点
|
||||
int val = h->data[h->size - 1];
|
||||
h->size--;
|
||||
int val = maxHeap->data[maxHeap->size - 1];
|
||||
maxHeap->size--;
|
||||
// 从顶至底堆化
|
||||
siftDown(h, 0);
|
||||
siftDown(maxHeap, 0);
|
||||
|
||||
// 返回堆顶元素
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 从节点 i 开始,从顶至底堆化 */
|
||||
void siftDown(MaxHeap *h, int i) {
|
||||
void siftDown(MaxHeap *maxHeap, int i) {
|
||||
while (true) {
|
||||
// 判断节点 i, l, r 中值最大的节点,记为 max
|
||||
int l = left(h, i);
|
||||
int r = right(h, i);
|
||||
int l = left(maxHeap, i);
|
||||
int r = right(maxHeap, i);
|
||||
int max = i;
|
||||
if (l < size(h) && h->data[l] > h->data[max]) {
|
||||
if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {
|
||||
max = l;
|
||||
}
|
||||
if (r < size(h) && h->data[r] > h->data[max]) {
|
||||
if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {
|
||||
max = r;
|
||||
}
|
||||
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
||||
@@ -129,23 +129,23 @@ void siftDown(MaxHeap *h, int i) {
|
||||
break;
|
||||
}
|
||||
// 交换两节点
|
||||
swap(h, i, max);
|
||||
swap(maxHeap, i, max);
|
||||
// 循环向下堆化
|
||||
i = max;
|
||||
}
|
||||
}
|
||||
|
||||
/* 从节点 i 开始,从底至顶堆化 */
|
||||
void siftUp(MaxHeap *h, int i) {
|
||||
void siftUp(MaxHeap *maxHeap, int i) {
|
||||
while (true) {
|
||||
// 获取节点 i 的父节点
|
||||
int p = parent(h, i);
|
||||
int p = parent(maxHeap, i);
|
||||
// 当“越过根节点”或“节点无须修复”时,结束堆化
|
||||
if (p < 0 || h->data[i] <= h->data[p]) {
|
||||
if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {
|
||||
break;
|
||||
}
|
||||
// 交换两节点
|
||||
swap(h, i, p);
|
||||
swap(maxHeap, i, p);
|
||||
// 循环向上堆化
|
||||
i = p;
|
||||
}
|
||||
|
||||
@@ -11,31 +11,31 @@ int main() {
|
||||
/* 初始化堆 */
|
||||
// 初始化大顶堆
|
||||
int nums[] = {9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2};
|
||||
MaxHeap *heap = newMaxHeap(nums, sizeof(nums) / sizeof(int));
|
||||
MaxHeap *maxHeap = newMaxHeap(nums, sizeof(nums) / sizeof(int));
|
||||
printf("输入数组并建堆后\n");
|
||||
printHeap(heap->data, heap->size);
|
||||
printHeap(maxHeap->data, maxHeap->size);
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
printf("\n堆顶元素为 %d\n", peek(heap));
|
||||
printf("\n堆顶元素为 %d\n", peek(maxHeap));
|
||||
|
||||
/* 元素入堆 */
|
||||
push(heap, 7);
|
||||
push(maxHeap, 7);
|
||||
printf("\n元素 7 入堆后\n");
|
||||
printHeap(heap->data, heap->size);
|
||||
printHeap(maxHeap->data, maxHeap->size);
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
int top = pop(heap);
|
||||
int top = pop(maxHeap);
|
||||
printf("\n堆顶元素 %d 出堆后\n", top);
|
||||
printHeap(heap->data, heap->size);
|
||||
printHeap(maxHeap->data, maxHeap->size);
|
||||
|
||||
/* 获取堆大小 */
|
||||
printf("\n堆元素数量为 %d\n", size(heap));
|
||||
printf("\n堆元素数量为 %d\n", size(maxHeap));
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
printf("\n堆是否为空 %d\n", isEmpty(heap));
|
||||
printf("\n堆是否为空 %d\n", isEmpty(maxHeap));
|
||||
|
||||
// 释放内存
|
||||
freeMaxHeap(heap);
|
||||
delMaxHeap(maxHeap);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ int *topKHeap(int *nums, int sizeNums, int k) {
|
||||
}
|
||||
int *res = getMinHeap(maxHeap);
|
||||
// 释放内存
|
||||
freeMaxHeap(maxHeap);
|
||||
delMaxHeap(maxHeap);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user