Question

In: Computer Science

Java Question 5: Count elements in the heap Write a function that returns the number of...

Java Question 5: Count elements in the heap

Write a function that returns the number of elements in a min heap strictly less than a given number.

Method signature: public static int elemNumHeap(PriorityQueue minHeap, int val)

Please also include testers.

Solutions

Expert Solution

========================

JAVA PROGRAM

========================

///////PriorityQueueMinHeap.JAVA///////////////////

import java.util.PriorityQueue;

import java.util.Random;

/**

*Class: PriorityQueueMinHeap

*The class constructs a mean heap of

*integers through priority queue

*The mean heap is constructed with MAX_ELELEMTS

*number of integers , which are bound between 0 to

*value set in BOUND.

*Main program calls elemNumHeap() method with

*a random integer between 0 to BOUND and prints count

*of total integers in minHeap which are strictly

*less than the random integer.

*

*/

public class PriorityQueueMinHeap {

//constants

private static final int MAX_ELELEMTS = 15;

private static final int BOUND = 100;

//main method

public static void main(String[] args){

//Create a PriorityQueue of integers

//indicating a min heap

PriorityQueue<Integer> pq = new PriorityQueue<>();

//Create a Random instance

Random random = new Random();

//iterate through for loop MAX_ELELEMTS number of times

for(int i = 1 ; i <= MAX_ELELEMTS; i++){

//get a random integer between 0 and BOUND(exclusive)

int v = random.nextInt(BOUND);

//System.out.println(v);

pq.add(v);//add v to pq

}

//print the min heap

System.out.println("The mean heap is => ");

System.out.println(pq);

//get a random number between 0 and BOUND (exclusive) to check

int randomNumber = random.nextInt(BOUND);

//call elemNumHeap() method to get the count

int count = elemNumHeap(pq,randomNumber);

//print the count

System.out.println("Total number of elements in min heap "

+ "strictly less than "+randomNumber+" is ="+count);

}

/**

* method: elemNumHeap

* It counts total number of integer in minHeap

* which are strictly less than val

* @param minHeap

* @param val

* @return

*/

public static int elemNumHeap(PriorityQueue<Integer> minHeap, int val){

int count = 0;

for(int currentValue : minHeap){

//check if currentValue is less than val

if(currentValue < val){

count++;//increase the count

}

}

return count;//return

}

}

===================================

OUTPUT

===================================

RUN1

----

The mean heap is =>

[0, 12, 2, 63, 19, 13, 16, 98, 66, 95, 27, 61, 22, 88, 28]

Total number of elements in min heap strictly less than 93 is =13

RUN2

-----

The mean heap is =>

[0, 11, 6, 43, 50, 10, 13, 78, 68, 52, 74, 63, 30, 16, 93]

Total number of elements in min heap strictly less than 80 is =14

RUN3

-----

The mean heap is =>

[27, 32, 36, 42, 59, 47, 60, 97, 69, 88, 93, 70, 83, 77, 98]

Total number of elements in min heap strictly less than 13 is =0


Related Solutions

in JAVA, Hash table The goal is to count the number of common elements between two...
in JAVA, Hash table The goal is to count the number of common elements between two sets. Download the following data sets, and add them to your project: girlNames2016.txt boyNames2016.txt These files contain lists of the 1,000 most popular boy and girl names in the US for 2016, as compiled by the Social Security Administration. Each line of the file consists of a first name, and the number of registered births that year using that name. Your task is to...
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
Write a method that returns the percentage of  the number of elements that have the value true...
Write a method that returns the percentage of  the number of elements that have the value true in an array booleans.
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary...
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary tree that contain odd numbers. You are essentially writing a method that will become part of the IntegerTree class. You may define private helper methods to solve this problem. Make your own trees in the main method of the code. I specifically kept vague the way the nodes are being added to the tree so you can practice building your own trees from scratch...
We can build a heap by repeatedly calling the insert function to insert the elements into...
We can build a heap by repeatedly calling the insert function to insert the elements into the heap. Here is pseudocode: buildHeap(A) h = new empty heap   for each element e in A       h.insert(e)             What is the Big-O runtime of this version of buildHeap? Justify your answer.
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT