Skip to main content

Practice ยท 1 of 2

The Array Heap

Implement a fixed-capacity min-heap. The boilerplate declares: ``c #define HCAP 64 typedef struct { long a[HCAP]; size_t n; } Heap; void heap_init(Heap *h); int heap_push(Heap *h, long v); /* 0 ok, -1 full/NULL */ int heap_pop(Heap *h, long *out); /* 0 ok, -1 empty/NULL */ int heap_peek(const Heap *h, long *out); /* 0 ok, -1 empty */ size_t heap_size(const Heap *h); /* 1 if the heap property holds for every parent/child pair */ int heap_ok(const Heap *h); `` Min-heap: the smallest element is at index 0.

Difficulty: intermediate

Back to lesson: Practice: Heap Gym