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(left
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
