Question: Data Structure/ convert this code to Maxheap. Java code. import java.util.Scanner; public class MinHeap { public static void main(String[] args) { int arrSize = 0;
Data Structure/ convert this code to Maxheap. Java code.
import java.util.Scanner;
public class MinHeap { public static void main(String[] args) { int arrSize = 0; Scanner in = new Scanner(System.in); System.out.print("Enter the size of the array you want to construct: "); arrSize = in.nextInt(); int A[] = new int[arrSize]; while(true) { System.out.println( "1. Insert " + "2. Delete " + "3. Search " + "4. Print ( sorted array , the array size and sort time to the screen) " + "5. Quit");
long start = 0; int data = 0; System.out.print("Enter your choice: "); switch(in.nextInt()) { case 1: start = System.currentTimeMillis(); for(int i = arrSize - 1; i >= 0; i--) { A[i] = i * 10; } System.out.println("It took you " + (double)(System.currentTimeMillis() - start)/1000 + " seconds to insert data."); break; case 2: System.out.print("Enter the data to delete: "); data = in.nextInt(); start = System.currentTimeMillis(); arrSize = delete(A, arrSize, data); System.out.println("It took you " + (double)(System.currentTimeMillis() - start)/1000 + " seconds to delete data."); break; case 3: System.out.print("Enter the data to search: "); data = in.nextInt(); start = System.currentTimeMillis(); search(A, arrSize, data); System.out.println("It took you " + (double)(System.currentTimeMillis() - start)/1000 + " seconds to search data."); break; case 4: start = System.currentTimeMillis(); MinHeap ob = new MinHeap(); ob.sort(A); System.out.println("It took you " + (double)(System.currentTimeMillis() - start)/1000 + " seconds to sort data.");
start = System.currentTimeMillis(); printSortedArray(A); System.out.println("It took you " + (double)(System.currentTimeMillis() - start)/1000 + " seconds to print data."); break; case 5: default: System.exit(0); } } }
private static void search(int[] A, int arrSize, int data) { for(int i = 0; i < arrSize; i++) { if(A[i] == data) { System.out.println("Data[" + data + "] found at index[" + i + "]."); return; } } }
private static int delete(int[] A, int arrSize, int data) { boolean isDelete = false; for(int i = 0; i < arrSize; i++) { if(A[i] == data) { isDelete = true; arrSize--; } if(isDelete) { A[i] = A[i + 1]; } } return arrSize; }
private void sort(int A[]) { int n = A.length;
for (int i = n / 2 - 1; i >= 0; i--) { makeHeap(A, n, i); }
for (int i = n - 1; i >= 0; i--) { int temporary = A[0]; A[0] = A[i]; A[i] = temporary;
makeHeap(A, i, 0); } }
private void makeHeap(int A[], int n, int i) { int largest = i; int left_child = 2*i + 1; int right_child = 2*i + 2;
if (left_child < n && A[left_child] > A[largest]) { largest = left_child; }
if (right_child < n && A[right_child] > A[largest]) { largest = right_child; }
if (largest != i) { int swap = A[i]; A[i] = A[largest]; A[largest] = swap; makeHeap(A, n, largest); } }
private static void printSortedArray(int A[]) { System.out.print("Sorted array: "); int n = A.length; for (int i = 0; i < n; ++i) { System.out.print(A[i] + " "); } System.out.println(""); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
