Find the Largest Value in a Min Heap
Find the Largest Value in a Min Heap
Complete min_heap_largest. It hands back the LARGEST value in the heap through the pointer value and returns true, or returns false when the heap is empty.
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 min heap keeps the smallest value at the root, but it says much less about the largest one. What it does say: a parent is never bigger than its children, so the largest value can never be a parent with a bigger child below it. It must be a LEAF (a value with no children). In the array the leaves are the back half: indexes count / 2 up to count - 1.
1
/ \
5 2
/ \ /
9 6 7
index: 0 1 2 | 3 4 5
values: 1 5 2 | 9 6 7
parents leaves: count / 2 = 3, so indexes 3, 4, 5
largest of the leaves: 9So you only need to search the leaves, which is half the work of searching every slot.
Requirements:
1. When the heap is empty, return false and leave *value alone.
2. Otherwise find the largest value among indexes count / 2 to count - 1, copy it into *value and return true.
3. Do not change the heap.
Do NOT write a main function.
Expected Output:
largest: 12= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...