Question: Please help with this, it has to be coded in C++. Below are some test cases ELEMENT is a data type that contains a field

Please help with this, it has to be coded in C++. Please help with this, it has to be coded in C++. Below

Below are some test cases

are some test cases ELEMENT is a data type that contains a

field named key o key is of type int o Note: ELEMENT

ELEMENT is a data type that contains a field named key o key is of type int o Note: ELEMENT itself is not of type int HEAP is a data type that contains three fields o capacity is of type int o size is of type int. o H is an array of type ELEMENT, with an index ranging from 0 to capacity The functions that you are required to implement are initialize(n): returns an object of type HEAP with capacity n and size 0 o This function requires you to perform dynamic memory allocation buildHeap(heap, A, n): where heap is a HEAP object, A is an array of type ELEMENT, n is an int which represents the size of the array A o This function copies the elements of A into heap-H (starting from H[1]), and then uses the linear time "build heap algorithm" to obtain a min heap of size n from the given array A (see lecture 05_heaps_heapsort.pptx and Section 6.3 in the book for a max-heap version) insert(heap, flag, k): inserts an ELEMENT item with key-k into the min heap heap o if flag1, the function does not do any printing o if flag 2, the function prints out the heap contents before the insertion, and then prints the heap contents after the insertion deleteMin(heap, flag): deletes the ELEMENT with the minimum key and returns it to the caller o if flag1, the function does not do any printing o if flag 2, the function prints out the heap contents before the deletion, and then prints out the heap contents after the deletion decreaseKey(heap, flag, index, value): decreases the key of the heap element pointed to by index to the value provided by value o The new key's value will be set by the value parameter, which should not be larger than the current value o Note that you have to make any necessary adjustments to make sure that the "min heap property is maintained throughout the heap o if flag:#1, the function does not do any additional printing o if flag 2, the function prints out the heap contents before the key is decreased, and then prints out the heap contents after the key has been decreased (any any necessary adjustments have been made) printHeap(heap): prints out the heap information, including capacity, size, and the key fields of the element in the H array, with index going from 1 to heap size You should implement a module that takes the following commands from the keyboard and feeds them to the main program: "S", "C n", "R", "W", "I f k", "D f". Kfiv. Reading these do the following S: the program stops C n: the program creates an empty heap with capacity equal to n, and waits for the next command R: the program reads in the array A from the file HEAPinput.txt, calls the linear time build heap algorithm (which builds the min heap based on A), and waits for the next command W: the program writes the current heap information to the screen and waits for the next command. The output should be in the same format as HEAPinput.txt, proceeded by the heap capacity If k: (capital i-f-k) the program inserts an ELEMENT with key equal to k into the current heap with the corresponding flag set to f, and waits for the next command D f: the program deletes the minimum element from the heap with the corresponding flag set to f, and prints the key field of the deleted element on the screen, and waits for the next command Kfiv: the program decreases the key of the element with index i to v with the corresponding flag set to f, and waits for the next command The file HEAPinput.txt is a text file. The first line of the file contains an integer n, which indicates the number of array elements. The next n lines contain n integers, one per line. These integers are the key values of the n array elements, from the 1st element to the n'th element. Test case 1: There is no file na Commands are: C 20 med HEAPinput.txt Output: C 20 COMMAND: C20 COMMAND: R There was a problem opening file HEAPinput.txt for reading. Test case 2: Content of HEAPinput.txt Commands are Output: COMMAND: R Sorry!!! It cannot be done. Please initialize the heap first Test case 3: Content of HEAPinput.txt Commands are: Output: COMMAND: W. Sorrv!!! It cannot be done. Please initialize the heap first Test case . Content of HEAPinput.txt Commands are: C 20 C 5 C 10 Output: C 20 COMMAND: C 20 C 5 COMMAND: C 5 COMMAND: W. The capacity is 5 Size is 0. C 10 COMMAND: C 10 COMMAND: W. The capacity is 10. Size is 0

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!