Question
JAVA: Using HeapSort implementation and the example code template given below, implement a node class and priority queue to contain items of the node class
JAVA:
Using HeapSort implementation and the example code template given below, implement a node class and priority queue to contain items of the node class. The node class should have instance variables that define an ID and a key. It should also have a compare method that can be adjusted to make the queue either min or max priority. The priority queue implementation should extend the heap class by adding the additional methods listed above. You should also add a location table to the heap. The location table keeps track of the location of each node in the heap by ID.
HeapSort implementation:
class HeapSort implements Sortable{ public int[] arr; private int heapsize; private int elements; private String type;
public HeapSort(int elements, String randomOrSorted){ arr = new int[elements]; type=randomOrSorted; this.elements=elements; this.heapsize=elements; if(randomOrSorted==("Random")){ for(int n=0; n
public void buildMaxHeap(){ for(int i=heapsize/2-1; i>=0; i--){ maxHeapify(i); } } public void show(){ for(int i=0;i
Program code template: public class Node implements Comparable
public Heap(Node[] a) { // create a heap from an array of nodes } public int parent(int i) { // code } public int left(int i) { // code } public int right(int i) { // code } public void heapify(int i) { // code } public void buildheap() { // code } } public class PriorityQueue extends Heap{ public PriorityQueue(int maxsize) { // create a priority queue of size maxsize } public void insert(Node newNode) { // code } public Node peek() { // code } public Node extract() { // code } public void changeKey(int i, int k) { // code } } public class Main { public static void main(String[] args) { PriorityQueue pq = new PriorityQueue(7);
pq.insert(new Node(0,3)); pq.insert(new Node(1,14)); pq.insert(new Node(2,7)); pq.insert(new Node(3,9)); pq.insert(new Node(4,99)); pq.insert(new Node(5,2)); pq.insert(new Node(6,46)); for (int i = 0; i < 7; i++) { System.out.print(pq.extract().key + " "); } } }
The above code should print out 99 46 14 9 7 3 2
For use in troubleshooting: The heap should contain 99 14 46 3 9 2 7 The location table should contain 3 1 6 4 0 5 2
The code should work correctly for any input and size.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started