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...
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.
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...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to enter a number of seconds. • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT