C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
heap.h
Go to the documentation of this file.
1/**
2 * @file heap.h
3 * @brief Binary min-heap (array-backed priority queue) of `int`.
4 *
5 * Min-heap: parent ≤ children. Array layout — for index `i`,
6 * left child = 2i+1, right child = 2i+2, parent = (i-1)/2.
7 *
8 * | Operation | Time |
9 * |---------------|----------|
10 * | insert | O(log n) |
11 * | extract_min | O(log n) |
12 * | peek_min | O(1) |
13 * | heapify (build from array) | O(n) — linear via sift-down |
14 */
15
16#ifndef HEAP_H
17#define HEAP_H
18
19#include <stdbool.h>
20#include <stddef.h>
21
22typedef struct heap heap_t;
23
24heap_t *heap_create(void);
25void heap_destroy(heap_t *h);
26
27bool heap_insert(heap_t *h, int value);
28bool heap_extract_min(heap_t *h, int *out_value);
29bool heap_peek_min(const heap_t *h, int *out_value);
30
31size_t heap_size(const heap_t *h);
32bool heap_is_empty(const heap_t *h);
33
34/** Build a min-heap from an existing array in O(n). The array is reordered in place. */
35void heap_build_min(int *arr, size_t n);
36
37/** Sift-down at index `i` for an n-element array — exposed for heap sort. */
38void heap_sift_down_max(int *arr, size_t i, size_t n);
39
40#endif /* HEAP_H */
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
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
Definition heap.c:10