Question

In: Computer Science

I have created a MinHeap program without using arrays. Below is the followup question : The...

I have created a MinHeap program without using arrays. Below is the followup question :

The heap class is a collection. Determine the correct location in your language’s collection
class hierarchy. Find all methods that you need to implement in order to add your class in
the language’s collection class hierarchy.

What does this mean and how to implement it?

Solutions

Expert Solution

The heap class is a collection as collection class is used exclusively with static methods that operate on or return collections. It can also inherit the Object class.

Also, We use the PriorityQueue class to implement Heaps in Java. By default Min Heap is implemented by this class. To implement Max Heap, we use Collections.reverseOrder() mwthod

Below is the java code using collections in java for better understanding

// code
import java.util.*;

class Example {
    public static void main(String args[])
    {
        // Created empty priority queue 
        PriorityQueue<Integer> priorityQueue =
                new PriorityQueue<Integer>(Collections.reverseOrder());

        // Add items to the priorityQueue using add() 
        priorityQueue.add(10);
        priorityQueue.add(30);
        priorityQueue.add(20);
        priorityQueue.add(400);

        // Print the highest priority element 
        System.out.println("Head value using peek function:" +
                priorityQueue.peek());

        // Print all elements 
        System.out.println("The queue elements:");
        Iterator iterator = priorityQueue.iterator();
        while (iterator.hasNext())
            System.out.println(iterator.next());

        // Remove the top priority element and print the modified priorityQueue using poll() 
        priorityQueue.poll();
        System.out.println("After removing an element "
                + "with poll function:");
        Iterator<Integer> itr2 = priorityQueue.iterator();
        while (itr2.hasNext())
            System.out.println(itr2.next());

        // Remove 30 using remove() 
        priorityQueue.remove(30);
        System.out.println("after removing 30 with" + " remove function:");
        Iterator<Integer> iterator1 = priorityQueue.iterator();
        while (iterator1.hasNext())
            System.out.println(iterator1.next());

        // Check if an element is present or not using contains() 
        boolean b = priorityQueue.contains(20);
        System.out.println("Priority queue contains 20 "
                + "or not?: " + b);

        // Get objects from the queue using toArray() in an array and print the array 
        Object[] ar = priorityQueue.toArray();
        System.out.println("Value in array: ");
        for (int j = 0; j < ar.length; j++)
            System.out.println("Value: " + ar[j].toString());
    }
} 

Related Solutions

Using the below program as a starting point C++ Program - Arrays- Chapter 9     Include...
Using the below program as a starting point C++ Program - Arrays- Chapter 9     Include the following header files in your program:     string, iomanip, iostream Suggestion: code steps 1 thru 4 then test then add requirement 5, then test, then add 6, then test etc. Add comments to display assignment //step 1., //step 2. etc. This program is to have no programer created functions. Just do everything in main and make sure you comment each step so I can...
I have this program in C that takes three char arrays that each have a first...
I have this program in C that takes three char arrays that each have a first and last name. I have two functions that reverese the name and change it to all upper case. I have the program completeed but need to change both functions to use pointers instead of arrays. I will bold the functions I need to use pointers. #include <stdio.h> void upper_string(char []); int main() { char name1[100]="John Smith"; char name2[100]="Mary Cohen"; char name3[100]="Carl Williams"; upper_string(name1);// calling...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
The question I have is that I want to draw without having to drag and then...
The question I have is that I want to draw without having to drag and then clicking the button whether I want a line or rectangle. It should work first by hitting either the line or rectangle button and then making a drag on the panel with mouse, then after releasing the drag it creates the graphic. Here is my current code. package Mod1; import java.awt.*; import java.awt.event.*; import javax.swing.*; class paintGUI extends JComponent { // Image in which we're...
Python Question I have created a dictionary shown below: import pandas as pd records_dict = {'FirstName':...
Python Question I have created a dictionary shown below: import pandas as pd records_dict = {'FirstName': [ 'Jim', 'John', 'Helen'], 'LastName': [ 'Robertson', 'Adams', 'Cooper'], 'Zipcode': [ '21801', '22321-1143', 'edskd-2134'], 'Phone': [ '555-555-5555', '4444444444', '323232'] } I have stored this dictionary in a data frame, like shown below: records = pd.DataFrame(records_dict) print(records) I am able to print the records just fine. My issue is, I want to eliminate, or put a blank space in, the values of the zipcode and...
We are to make a program about a car dealership using arrays. I got the code...
We are to make a program about a car dealership using arrays. I got the code to display all cars in a list, so I'm good with that. What I'm stuck at is how to make it so when a user inputs x for search, it allows them to search the vehicle. We need two classes, one that shows the car information and another that shows the insert, search, delete, display methods. Here is what I have so far package...
I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
Description: One problem with dynamic arrays is that once the array is created using the new...
Description: One problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This assignment asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have the following: • A private member variable called...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT