Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a programming project in either C++ or C, to be completed and graded on general.asu.edu, a Linux machine. You will perform modular design,

This is a programming project in either C++ or C, to be completed and graded on general.asu.edu, a Linux machine. You will perform modular design, provide a Makefile to compile various modules to generate the executable file named run. Among other things, you need to have

1. a main program, which coordinates all other modules; 2. a module that provides utility services including command line interpretation; 3. a module that implements the max-heap data structure; 4. a Makefile which compiles all modules and link them into the executable.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

You need to define the following data types. ELEMENT is a data type that contains a field named key, which is of type int. Note that ELEMENT should not be of type int. HEAP is a data type that contains three fields named capacity (of type int), size (of type int), and H (an array of type ELEMENT with index ranging from 0 to capacity). Initialize(n) which returns an object of type HEAP with capacity n and size 0. This function requires you to perform dynamic memory allocation, given the demand n. Build Heap(heap, A, n), where heap is a HEAP object, A is an array of type ELEMENT, and n is the size of array A. This function copies the elements in A into heap->H (starting from H[1] and uses the linear time build heap algorithm to obtain a max-heap of size n from the given array A. Insert(heap, flag, k) which inserts an element with key equal to k into the max-heap heap. When flag=1, the function does not do any additional printing. When flag=2, the function prints out the heap content before the insertion, and the heap content after the insertion. DeleteMax(heap, flag) which deletes the element with maximum key and returns it to the caller. When flag=1, the function does not do any additional printing. When flag=2, the function prints out the heap content before the deletion, and the heap content after the deletion. IncreaseKey(heap, flag, index, value) which increases the key field of the heap element pointed to by index to value, which should not be smaller than the current value. Note that you have to make necessary adjustment to make sure that heap order is maintained. When flag=1, the function does not do any additional printing. When flag=2, the function prints out the heap content before the increase key operation, and the heap content after the increase key operation. printHeap(heap) which prints out the heap information, including capacity, size, and the key fields of the elements in the array with index going from 1 to size. You should implement a module that takes the following commands from the key-board and feeds to the main program: S Cn R W Ifk D f Kfiv On reading S, the program stops. On reading C n, the program creates an empty heap with capacity equal to n, and waits for the next command. On reading R, the program reads in the array A from file HEAPinput.txt, calls the linear time build heap algorithm to build the max-heap based on A, and waits for the next command. On reading 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 in the file HEAPinput.txt, proceeded by the heap capacity. On reading I fk, 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. On reading 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, it waits for the next command. On reading Kfiv, the program increases the key of element with index i to v with the correspond- ing flag set to f. 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 integer per line. These integers are the key values of the n array elements, from the first element to the nth element. You need to define the following data types. ELEMENT is a data type that contains a field named key, which is of type int. Note that ELEMENT should not be of type int. HEAP is a data type that contains three fields named capacity (of type int), size (of type int), and H (an array of type ELEMENT with index ranging from 0 to capacity). Initialize(n) which returns an object of type HEAP with capacity n and size 0. This function requires you to perform dynamic memory allocation, given the demand n. Build Heap(heap, A, n), where heap is a HEAP object, A is an array of type ELEMENT, and n is the size of array A. This function copies the elements in A into heap->H (starting from H[1] and uses the linear time build heap algorithm to obtain a max-heap of size n from the given array A. Insert(heap, flag, k) which inserts an element with key equal to k into the max-heap heap. When flag=1, the function does not do any additional printing. When flag=2, the function prints out the heap content before the insertion, and the heap content after the insertion. DeleteMax(heap, flag) which deletes the element with maximum key and returns it to the caller. When flag=1, the function does not do any additional printing. When flag=2, the function prints out the heap content before the deletion, and the heap content after the deletion. IncreaseKey(heap, flag, index, value) which increases the key field of the heap element pointed to by index to value, which should not be smaller than the current value. Note that you have to make necessary adjustment to make sure that heap order is maintained. When flag=1, the function does not do any additional printing. When flag=2, the function prints out the heap content before the increase key operation, and the heap content after the increase key operation. printHeap(heap) which prints out the heap information, including capacity, size, and the key fields of the elements in the array with index going from 1 to size. You should implement a module that takes the following commands from the key-board and feeds to the main program: S Cn R W Ifk D f Kfiv On reading S, the program stops. On reading C n, the program creates an empty heap with capacity equal to n, and waits for the next command. On reading R, the program reads in the array A from file HEAPinput.txt, calls the linear time build heap algorithm to build the max-heap based on A, and waits for the next command. On reading 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 in the file HEAPinput.txt, proceeded by the heap capacity. On reading I fk, 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. On reading 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, it waits for the next command. On reading Kfiv, the program increases the key of element with index i to v with the correspond- ing flag set to f. 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 integer per line. These integers are the key values of the n array elements, from the first element to the nth element

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Describe the major barriers to the use of positive reinforcement.

Answered: 1 week ago