Question: I need to make a priority queue with a max heap that I already created. I am having troubles with the queue since I haven't

I need to make a priority queue with a max heap that I already created. I am having troubles with the queue since I haven't made many and have thoroughly confused myself and destroyed my code.

JAVA on BlueJ

public class MainClass{ public static void main(String args[]){ PriorityQueue priorityQueue = new PriorityQueue(4); priorityQueue.insert(new Node(0, 12)); priorityQueue.insert(new Node(1, 22)); priorityQueue.insert(new Node(2, 4)); priorityQueue.insert(new Node(3, 57)); for(int i=0; i<7; i++){ System.out.print(priorityQueue.extract().key + ""); } } }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class Heap{ protected Node[] heap; protected int heapsize; protected int[] location;

public Heap(){ heap = new Node[20]; } public Heap(int n){ heap = new Node[n]; } public Heap(Node[] a){ //create a heap from an array of nodes } public int parent(int i){ return ((i-1) /2); } public int right(int i){ return i*2+2; } public int left(int i){ return i*2+1; } public void heapify(int i){ int left = left(i); int right = right(i); int largest = i; if(leftheap[largest]){ largest = left; } if(rightheap[largest]){ largest = right; } if(largest!=i){ heap[i] = heap[i] - heap[largest]; heap[largest]= heap[i] + heap[largest]; heap[i]= heap[largest] - heap[i]; heapify(largest); } if(significant != i) { // exchange heap[i] with heap[significant] Node temp = heap[i]; heap[i] = heap[significant]; heap[significant] = temp; // swap the location table as well location[heap[significant].ID] = significant; location[heap[i].ID] = i; heapify(significant); } } public void buildHeap(){ for(int i=heapsize/2-1; i>=0; i--){ heapify(i); } } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class PriorityQueue extends Heap{ public PriorityQueue(int maxSize){ PriorityQueue priorityQueue = new PriorityQueue<>(maxSize); } public void insert(Node newNode){ } public Node peek(){ System.out.println(priorityQueue.peek()); } public Node extract(){ System.out.println(priorityQueue.poll()); } public void changeKey(int i, int k){ } }

/////////////////////////////////////////////////////////////////////////////////////////////////////

public class Node implements Comparable{ public int ID; public int key; public Node(int ID, int key){ } @Override public int compareTo(Node node2){ if(this.ID < node2.ID){ return -1; } else{ return 1; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!