C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
heap.c
Go to the documentation of this file.
1/**
2 * @file heap.c
3 */
4
5#include "heap.h"
6#include <stdlib.h>
7
8#define HEAP_INITIAL_CAP 16
9
10struct heap {
11 int *data;
12 size_t size;
13 size_t capacity;
14};
15
16static void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }
17
19 heap_t *h = malloc(sizeof(*h));
20 if (!h) return NULL;
21 h->data = malloc(HEAP_INITIAL_CAP * sizeof(int));
22 if (!h->data) { free(h); return NULL; }
23 h->size = 0;
25 return h;
26}
27
29 if (!h) return;
30 free(h->data);
31 free(h);
32}
33
34static bool heap_grow(heap_t *h) {
35 size_t new_cap = h->capacity * 2;
36 int *grown = realloc(h->data, new_cap * sizeof(int));
37 if (!grown) return false;
38 h->data = grown;
39 h->capacity = new_cap;
40 return true;
41}
42
43static void sift_up_min(int *arr, size_t i) {
44 while (i > 0) {
45 size_t parent = (i - 1) / 2;
46 if (arr[parent] <= arr[i]) break;
47 swap(&arr[parent], &arr[i]);
48 i = parent;
49 }
50}
51
52static void sift_down_min(int *arr, size_t i, size_t n) {
53 while (1) {
54 size_t left = 2 * i + 1;
55 size_t right = 2 * i + 2;
56 size_t smallest = i;
57 if (left < n && arr[left] < arr[smallest]) smallest = left;
58 if (right < n && arr[right] < arr[smallest]) smallest = right;
59 if (smallest == i) break;
60 swap(&arr[i], &arr[smallest]);
61 i = smallest;
62 }
63}
64
65bool heap_insert(heap_t *h, int value) {
66 if (!h) return false;
67 if (h->size == h->capacity && !heap_grow(h)) return false;
68 h->data[h->size] = value;
69 sift_up_min(h->data, h->size);
70 h->size++;
71 return true;
72}
73
74bool heap_extract_min(heap_t *h, int *out_value) {
75 if (!h || h->size == 0) return false;
76 if (out_value) *out_value = h->data[0];
77 h->size--;
78 if (h->size > 0) {
79 h->data[0] = h->data[h->size];
80 sift_down_min(h->data, 0, h->size);
81 }
82 return true;
83}
84
85bool heap_peek_min(const heap_t *h, int *out_value) {
86 if (!h || h->size == 0) return false;
87 if (out_value) *out_value = h->data[0];
88 return true;
89}
90
91size_t heap_size(const heap_t *h) { return h ? h->size : 0; }
92bool heap_is_empty(const heap_t *h) { return !h || h->size == 0; }
93
94void heap_build_min(int *arr, size_t n) {
95 if (n <= 1) return;
96 /* Floyd's algorithm: sift-down from the last non-leaf node back to root. */
97 for (size_t i = n / 2; i > 0; i--) {
98 sift_down_min(arr, i - 1, n);
99 }
100}
101
102/* Max-heap sift-down (used by heap_sort_ints). */
103void heap_sift_down_max(int *arr, size_t i, size_t n) {
104 while (1) {
105 size_t left = 2 * i + 1;
106 size_t right = 2 * i + 2;
107 size_t largest = i;
108 if (left < n && arr[left] > arr[largest]) largest = left;
109 if (right < n && arr[right] > arr[largest]) largest = right;
110 if (largest == i) break;
111 swap(&arr[i], &arr[largest]);
112 i = largest;
113 }
114}
static bool heap_grow(heap_t *h)
Definition heap.c:34
bool heap_peek_min(const heap_t *h, int *out_value)
Definition heap.c:85
void heap_destroy(heap_t *h)
Definition heap.c:28
void heap_sift_down_max(int *arr, size_t i, size_t n)
Definition heap.c:103
void heap_build_min(int *arr, size_t n)
Definition heap.c:94
heap_t * heap_create(void)
Definition heap.c:18
#define HEAP_INITIAL_CAP
Definition heap.c:8
bool heap_extract_min(heap_t *h, int *out_value)
Definition heap.c:74
bool heap_is_empty(const heap_t *h)
Definition heap.c:92
bool heap_insert(heap_t *h, int value)
Definition heap.c:65
size_t heap_size(const heap_t *h)
Definition heap.c:91
static void sift_up_min(int *arr, size_t i)
Definition heap.c:43
static void sift_down_min(int *arr, size_t i, size_t n)
Definition heap.c:52
static void swap(int *a, int *b)
Definition heap.c:16
Binary min-heap (array-backed priority queue) of int.
Definition heap.c:10
size_t size
Definition heap.c:12
int * data
Definition heap.c:11
size_t capacity
Definition heap.c:13