CJCoding With Joseph
15free / daySign inCreate account

Check Whether a Heap Is Empty or Full

Complete heap_is_empty and heap_is_full. Each one looks at the heap and returns 1 for yes or 0 for no.

Reminder: `size` counts the slots in use. 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 always sit in slots 0 up to size - 1. The array has room for HEAP_CAPACITY values, which is 15 here. The slots at size and beyond are free space: they may still hold old numbers, but those numbers are not part of the heap.

a dot is a free slot

size 0      empty: yes   full: no
index:   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14
value:   .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

size 4      empty: no    full: no
index:   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14
value:   3  8  5  9  .  .  .  .  .  .  .  .  .  .  .
                     ^ size is 4, so slot 4 is the next free slot

            3
          /   \
         8     5
        /
       9

size 15     empty: no    full: yes
index:   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14
value:   1  3  2  6  4  5  9  8  7 12 10 11 13 15 14

Both answers come from comparing heap->size with a number. The values stored in the slots never matter, only how many slots are in use.

Requirements: 1. heap_is_empty(heap) returns 1 when the heap holds no values, otherwise 0. 2. heap_is_full(heap) returns 1 when all HEAP_CAPACITY slots are in use, otherwise 0. 3. Use HEAP_CAPACITY rather than typing 15, so the check still works if the capacity changes. 4. A heap that holds the single value 0 is not empty: look at size, never at the values. 5. Do not change the heap, and leave heap_init, heap_load and heap_print as they are.

Do NOT write a main function.

Expected Output:

MinHeap:
size 0: empty yes, full no
MinHeap: 0
size 1: empty no, full no

= newline. The output ends with a newline — print one after the last line.

Hints2 available
Topics:
Data Structures & AlgorithmsHeaps
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (p, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.