Create a Min Heap With malloc
Create a Min Heap With malloc
Complete min_heap_initialize. It builds a new, empty heap in memory from malloc and returns a pointer to it.
Reminder: how this min heap is stored. A min_heap is a struct with three fields: capacity (the most values it can hold), count (how many it holds now) and values (a pointer to an array made with malloc). The heap is a complete binary tree kept in that array, filled row by row with no gaps, so only slots 0 to count - 1 are in use. Every parent is smaller than or equal to its children, so the smallest value is always at index 0. For the value at index i, the parent is at (i - 1) / 2 and the children are at 2 * i + 1 and 2 * i + 2.
A heap needs TWO blocks of memory: one small block for the struct itself, and one block for the array its values pointer points to.
min_heap_initialize(4) makes two blocks:
block 1: the struct block 2: the array
+--------------+ +----+----+----+----+
| capacity: 4 | | ? | ? | ? | ? |
| count: 0 | +----+----+----+----+
| values: ----+----------------> 0 1 2 3
+--------------+
the function returns the address of block 1The array starts with whatever was in that memory (the ? marks). That is fine: count is 0, so no slot is in use yet.
Requirements:
1. Use malloc to make one min_heap.
2. Set its capacity to the parameter and its count to 0.
3. Use malloc again to make an array of capacity ints, and store its address in values.
4. Return the pointer to the new heap.
Do NOT write a main function.
Expected Output:
capacity 5, count 0 slots read back: 10 20 30 40 50
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...