Question: In Java using use the Java - 1 1 JRE in Eclipse, implement a class named MaxHeap using an array as a class member variable
In Java using use the Java JRE in Eclipse, implement a class named MaxHeap using an array as a class member variable data field, storing elements of generic type T requiring Do not use any libraries of any kind, except the java.util.Arrays.toString, array copying methods, random number generators in main's class, and console IO The Java Standard Library has multiple good implementations of a Heap, but the idea of this course is to learn how these things are made, by making them ourselves!
Define a MaxHeap class. It should have:
A private data field member variable of type T array. This array will be used to store the heap at all times. T will be a Java generic type required to be by the MaxHeap class.
A private data field of type int called heapsize. Array indices to heapsize are defined to be in the heap," and any values after that are not. You may write your code with oneindexing or zeroindexing. Bear in mind the pseudocode uses one indexing!
A public constructor method with a T array parameter. This constructor will take a T array filled with arbitrary values as a parameter, make a copy of it into its data field array made the same length, set heapsize to the array length, and then invoke buildMaxHeap see below You may want to use the java.util.Arrays.copyOfRange method in the constructor, or you can do it yourself.
A private method named leftChildOf, which takes an index parameter. Return the index where the left child is stored. Indices beyond the end of the heap have no children, so return If the left child is beyond the end of the heap, return Don't forget the zero indexing!
A private method named rightChildOf, which takes an index parameter. Return the index where the right child is stored. Indices beyond the end of the heap have no children, so return If the right child is beyond the end of the heap, return Don't forget the zero indexing!
A public method named printMaxHeap to write out the contents of its array data field to standard output in a nicely formatted print statement. Print out the heapsize, and then only print the values of indices through heapsize inclusive. You can do this yourself, or use the java.util.Arrays.toString method. You must only print indices from to heapSize inclusive, so you may want to use java.util.Arrays.copyOfRange before invoking java.util.Arrays.toString.
A private recursive method named maxHeapify based on the MAXHEAPIFY method, whose pseudocode is in the textbook and the slides. Make sure to invoke your leftChildOf and rightChildOf methods to obtain the indices of the two children. The only parameter will be the index, because the array itself is a class member variable and thus need not be a parameter. Remember the heap ends at index heapsize inclusive.
A private method buildMaxHeap based on the BUILDMAXHEAP method, whose pseudocode is in the textbook and the slides. This is a loop that invokes maxHeapify. It has no parameters since the array is a class member variable. Remember the heap ends at index heapsize inclusive.
A public method named heapsort, based on the HEAPSORT method, whose pseudocode is in the textbook and the slides. This method has no parameters, since the array is a class member variable. It will use buildMaxHeap and maxHeapify. Remember the heap ends at index heapsize inclusive. This method modifies heapsize continuously, so remember to restore a backup copy of heapsize after the sorting is done.
Create a second class in the same package as the MaxHeap class; this class will contain only the main method. Its purpose is to create some MaxHeap objects and test them. Bear in mind that the only public methods of MaxHeap are the constructor, printMaxHeap, and heapsort. Use at least different arrays filled with values to make at least different MaxHeap objects. For each object:
Print out the original array that it was constructed from the one with values in arbitrary order. You can use the java.util.Arrays.toString method.
Print out the heap with printMaxHeap. By looking at the console it should be obvious the values are rearranged, from the buildMaxHeap that was invoked in the constructor.
Invoke heapsort.
Print out the heap again with printMaxHeap. In the console you should notice all the values are sorted.
Remember to comment your code! You should have a header comment with a brief description of what your code does.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
