Print a Heap in Array Order
Print a Heap in Array Order
Complete heap_print. It prints the heap on one line in array order, from index 0 up to the last value in use: the values live in heap->data, and heap->size says how many slots are in use. Most later questions in this tile hand you heap_print already written and use it to show the heap, so the line you build here is the one you will read in their tests.
The C version of the heap. C has no std::vector, so here a MinHeap is a fixed array, heap->data, with room for HEAP_CAPACITY (15) ints, plus a counter, heap->size, that says how many of those slots hold the heap. Only slots 0 to size - 1 are the heap. The slots after them are spare room and can still hold old numbers, because loading fewer values (or, later, removing one) lowers size without clearing the slots past it.
Reminder: how a min heap lives in an array. A min heap is a complete binary tree kept in an array, filled row by row, left to right, with no gaps, so its values sit in slots 0 up to size - 1. Every parent is smaller than or equal to its children, so the smallest value is at index 0, but it is NOT a binary search tree: nothing orders a left child against its right sibling. Because there are no gaps, walking the array from index 0 to size - 1 is the same as reading the tree like a page of a book: the root, then the second row from left to right, then the third row, and so on.
1 row 0: 1
/ \
4 2 row 1: 4 2
/ \ / \
7 5 3 6 row 2: 7 5 3 6
the same rows in the array (bars mark row ends):
index: 0 | 1 2 | 3 4 5 6
value: 1 | 4 2 | 7 5 3 6Array order is not sorted order: here 4 comes before 2 and 7 before 3. Print the slots exactly as they sit, do not sort them. The bars are only in the picture: the line you print has none. The tests compare your line character for character, so the shape is fixed: the label MinHeap: first, then every value with one space in front of it, then a newline. There is no space after the last value, and an empty heap prints only the label and the newline.
Requirements:
1. Print the label, then for each index from 0 up to heap->size - 1, a space followed by the value.
2. End the line with a single \n, with no space before it.
3. Print only the slots in use. A number in a slot at size or beyond is not part of the heap, even though it is still in the array.
4. Do not change the heap, and leave the MinHeap struct and heap_load as they are.
Do NOT write a main function.
Expected Output:
MinHeap: 2 MinHeap: 2 8 5 MinHeap: 2 8 5 10 9 7 6
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...