Files
hello-algo/en/codes/c/chapter_heap/my_heap.c
Yudong Jin 2778a6f9c7 Translate all code to English (#1836)
* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
2025-12-31 07:44:52 +08:00

153 lines
3.6 KiB
C

/**
* File: my_heap.c
* Created Time: 2023-01-15
* Author: Reanon (793584285@qq.com)
*/
#include "../utils/common.h"
#define MAX_SIZE 5000
/* Max heap */
typedef struct {
// size represents the actual number of elements
int size;
// Use pre-allocated array to avoid expansion
int data[MAX_SIZE];
} MaxHeap;
// Function declaration
void siftDown(MaxHeap *maxHeap, int i);
void siftUp(MaxHeap *maxHeap, int i);
int parent(MaxHeap *maxHeap, int i);
/* Constructor, build heap from slice */
MaxHeap *newMaxHeap(int nums[], int size) {
// Push all elements to heap
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--) {
// Heapify all nodes except leaf nodes
siftDown(maxHeap, i);
}
return maxHeap;
}
/* Destructor */
void delMaxHeap(MaxHeap *maxHeap) {
// Free memory
free(maxHeap);
}
/* Get index of left child node */
int left(MaxHeap *maxHeap, int i) {
return 2 * i + 1;
}
/* Get index of right child node */
int right(MaxHeap *maxHeap, int i) {
return 2 * i + 2;
}
/* Get index of parent node */
int parent(MaxHeap *maxHeap, int i) {
return (i - 1) / 2; // Round down
}
/* Swap elements */
void swap(MaxHeap *maxHeap, int i, int j) {
int temp = maxHeap->data[i];
maxHeap->data[i] = maxHeap->data[j];
maxHeap->data[j] = temp;
}
/* Get heap size */
int size(MaxHeap *maxHeap) {
return maxHeap->size;
}
/* Check if heap is empty */
int isEmpty(MaxHeap *maxHeap) {
return maxHeap->size == 0;
}
/* Access top element */
int peek(MaxHeap *maxHeap) {
return maxHeap->data[0];
}
/* Element enters heap */
void push(MaxHeap *maxHeap, int val) {
// By default, should not add this many nodes
if (maxHeap->size == MAX_SIZE) {
printf("heap is full!");
return;
}
// Add node
maxHeap->data[maxHeap->size] = val;
maxHeap->size++;
// Heapify from bottom to top
siftUp(maxHeap, maxHeap->size - 1);
}
/* Element exits heap */
int pop(MaxHeap *maxHeap) {
// Handle empty case
if (isEmpty(maxHeap)) {
printf("heap is empty!");
return INT_MAX;
}
// Delete node
swap(maxHeap, 0, size(maxHeap) - 1);
// Remove node
int val = maxHeap->data[maxHeap->size - 1];
maxHeap->size--;
// Return top element
siftDown(maxHeap, 0);
// Return heap top element
return val;
}
/* Starting from node i, heapify from top to bottom */
void siftDown(MaxHeap *maxHeap, int i) {
while (true) {
// Find node with maximum value among nodes i, l, r, denoted as max
int l = left(maxHeap, i);
int r = right(maxHeap, i);
int max = i;
if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {
max = l;
}
if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {
max = r;
}
// Swap two nodes
if (max == i) {
break;
}
// Swap two nodes
swap(maxHeap, i, max);
// Loop downwards heapification
i = max;
}
}
/* Starting from node i, heapify from bottom to top */
void siftUp(MaxHeap *maxHeap, int i) {
while (true) {
// Get parent node of node i
int p = parent(maxHeap, i);
// When "crossing root node" or "node needs no repair", end heapify
if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {
break;
}
// Swap two nodes
swap(maxHeap, i, p);
// Loop upward heapify
i = p;
}
}