Question

In: Computer Science

Java programming Write the max-heapify code and test it and then write the program to find...

Java programming

Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.

Solutions

Expert Solution

CODE

public class Main {

private int[] arr;

private int size;

private int maximum;

public Main(int maxsize)

{

this.maximum = maxsize;

this.size = 0;

arr = new int[this.maximum + 1];

arr[0] = Integer.MAX_VALUE;

}

private int parent(int pos)

{

return pos / 2;

}

private int leftChild(int pos)

{

return (2 * pos);

}

private int rightChild(int pos)

{

return (2 * pos) + 1;

}

private boolean isLeaf(int pos)

{

if (pos >= (size / 2) && pos <= size) {

return true;

}

return false;

}

private void swap(int fpos, int spos)

{

int tmp;

tmp = arr[fpos];

arr[fpos] = arr[spos];

arr[spos] = tmp;

}

private void maxHeapify(int pos)

{

if (isLeaf(pos))

return;

if (arr[pos] < arr[leftChild(pos)] ||

arr[pos] < arr[rightChild(pos)]) {

if (arr[leftChild(pos)] > arr[rightChild(pos)]) {

swap(pos, leftChild(pos));

maxHeapify(leftChild(pos));

}

else {

swap(pos, rightChild(pos));

maxHeapify(rightChild(pos));

}

}

}

public void insert(int element)

{

arr[++size] = element;

int current = size;

while (arr[current] > arr[parent(current)]) {

swap(current, parent(current));

current = parent(current);

}

}

public int findMax()

{

int popped = arr[1];

arr[1] = arr[size--];

maxHeapify(1);

return popped;

}

public static void main(String[] arg)

{

int arr[] = {14, 1, -9, 17, 85, 11, 45, 9};

Main maxHeap = new Main(arr.length);

for (int i=0; i<arr.length; i++) {

maxHeap.insert(arr[i]);

}

System.out.println("The largest three values are: ");

System.out.println(maxHeap.findMax());

System.out.println(maxHeap.findMax());

System.out.println(maxHeap.findMax());

}

}


Related Solutions

Write a Java test program, all the code should be in a single main method, that...
Write a Java test program, all the code should be in a single main method, that prompts the user for a single character. Display a message indicating if the character is a letter (a..z or A..Z), a digit (0..9), or other. Java's Scanner class does not have a nextChar method. You can use next() or nextLine() to read the character entered by the user, but it is returned to you as a String. Since we are only interested in the...
Java programming language Write a Java application that simulates a test. The test contains at least...
Java programming language Write a Java application that simulates a test. The test contains at least five questions. Each question should be a multiple-choice question with 4 options. Design a QuestionBank class. Use programmer-defined methods to implement your solution. For example: - create a method to simulate the questions – simulateQuestion - create a method to check the answer – checkAnswer - create a method to display a random message for the user – generateMessage - create a method to...
Java. If possible please put explanations for each line of code. Write a test program that...
Java. If possible please put explanations for each line of code. Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one...
Java Programming Write a program that displays the following pattern *                         *       &nbsp
Java Programming Write a program that displays the following pattern *                         *          *          * *          *          *          *          *          *          *          *          *          *          *          *             *          *          *          *          *                         *          *          *                                     * Printing Pattern A * ** *** **** ***** ****** ******* Printing Pattern B ******* ****** ***** **** *** ** * Printing Pattern C * ** *** **** ***** ****** *******
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT