Question: I need to make a priority queue based off of a heap With class node. I have some of it finished but I am having

I need to make a priority queue based off of a heap With class node. I have some of it finished but I am having issues with implementing the actual priority queue since I dont have much experience with writing them. I understand how they work but not how to implement. If your help doesnt involve a Node class then dont answer please.

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.println(priorityQueue.extract().key + ""); } } }

______________________________________________________________________________________

import java.util.*;

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

public Heap(){ heapNode = new Node[20]; } public Heap(int n){ heapsize = n; heapNode = new Node[heapsize]; } public Heap(Node[] a){ //create a heap from an array of nodes heapsize = a.length; heapNode = new Node[heapsize]; for(int i=0; heapsize>0; i++){ heapNode[i] = a[i]; } } 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(leftheapNode[largest].key){ largest = left; } if(rightheapNode[largest].key){ largest = right; } if(largest!=i){ heapNode[i].key = heapNode[i].key - heapNode[largest].key; heapNode[largest].key= heapNode[i].key + heapNode[largest].key; heapNode[i].key= heapNode[largest].key - heapNode[i].key; heapify(largest); } int significant = heapNode[0].ID; if(significant != i) { // exchange heap[i] with heap[significant] Node temp = heapNode[i]; heapNode[i] = heapNode[significant]; heapNode[significant] = temp; // swap the location table as well location[heapNode[significant].ID] = significant; location[heapNode[i].ID] = i; heapify(significant); } } public void buildHeap(){ for(int i=heapsize/2-1; i>=0; i--){ heapify(i); } } } _____________________________________________________________________________________

class PriorityQueue extends Heap{ int place = 0; public PriorityQueue(int maxSize){ heapNode = new Node[heapsize]; } public void insert(Node newNode){ heapNode[place] = newNode; place++; } public Node peek(){ return heapNode[0]; } public Node extract(){ Node remove = heapNode[0]; for(int i=1; i

________________________________________________________________________________________-

public class Node implements Comparable{ public int ID; public int key; public Node(int ID, int key){ ID = this.ID; key = this.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!