Question: For this exercise you are to implement a heap, using an array. The heap will be built by repeatedly: 1. Reading an input from a

For this exercise you are to implement a heap, using an array.

The heap will be built by repeatedly:

1. Reading an input from a file (in this case an integer)

2. Adding it after the last element of the current heap

3. Sift it up into the correct position, to make a min heap.

At the end of all the inputs you are to print out the first five elements of the min heap array. You should then convert the array into a max heap by calling makeheap and again print the first 5 elements. Your heap should be able to hold 100 integers. A pseudo?code outline for the program is given below:

Begin main

display a prompt for the file name

read in the file name

try to open the file

if (the file fails to open)

print an error message on the screen and exit

fi

while we can read an int from the file

insert the int into the array (to make a min heap)

elihw

close the file

print the first 5 elements of the heap

makeheap() (to make a max heap)

print the first 5 elements of the heap

rof

End main

-

Do not implement the heap using a class or with STL. The heap must be implemented using a fixed size array of ints (500 entries should be enough). The heap array and the index to the last item in the heap should be global variables.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!