Write min_heap_remove With the Sink-Down Loop Inside
Write min_heap_remove With the Sink-Down Loop Inside
Write all of min_heap_remove, including the loop that sinks the moved value back down. There is no heapify_down helper in this file.
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.
After the root is saved, the last value is moved into index 0. It is probably too big to stay there, so it sinks: at each step, find the SMALLER of its children and swap with it, until it has no children or neither child is smaller.
remove from 1 3 2 7 4 5 (count 6):
last value (5) to root children 3 and 2: 5 is now at index 2,
2 is smaller, 5 > 2, swap which has no children: stop
5 2 2
/ \ / \ / \
3 2 3 5 3 5
/ \ / \ / \
7 4 7 4 7 4
5 3 2 7 4 2 3 5 7 4 2 3 5 7 4Why the smaller child: whatever moves up becomes the parent of the other child, so it must be the smaller of the two. A node may have two children, one child (only a left one), or none. A child exists only when its index is less than count.
Requirements:
1. When the heap is empty, return false and leave *value alone.
2. Copy the root into *value, subtract 1 from count, and move the value from the old last slot into index 0.
3. Starting at index 0: while there is a left child, pick the smaller child (the right one only when it exists and is smaller), stop when the value is less than or equal to that child, otherwise swap and continue from the child's slot.
4. Return true.
Do NOT write a main function.
Expected Output:
removed 1 [2, 4, 3, 7, 5, 6] removed 2 [3, 4, 6, 7, 5] removed 3 [4, 5, 6, 7]
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...