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 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...
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...
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
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C 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. 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
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT