Question
JAVA // Trying to implement BinaryHeap PQ, max-Oriented , need help with swim, sink and delMax functions, using Node ; import java.util.Comparator; import java.util.NoSuchElementException; import
JAVA // Trying to implement BinaryHeap PQ, max-Oriented , need help with swim, sink and delMax functions, using Node
import java.util.Comparator; import java.util.NoSuchElementException;
import stdlib.StdIn; import stdlib.StdOut;
/** * The PtrHeap class is the priorityQ class from Question 2.4.24. * It represents a priority queue of generic keys. * * It supports the usual insert and delete-the-maximum * operations, along with methods for peeking at the maximum key, * testing if the priority queue is empty, and iterating through * the keys. * For additional documentation, see Section 2.4 of * Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. */
public class PtrHeap
/** Return the number of items on the priority queue. */ public int size() { return size; }
/** * Return the largest key on the priority queue. * Throw an exception if the priority queue is empty. */ public K max() //isRoot method { if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); else { return pq[1]; //remember in a maxOrientedHeap we will always keep index 1 as the largest value; } } public boolean less(K fst, K snd) { return fst.compareTo(snd) < 0; //true when i is smaller than j; }
public void sink(Node
/** * Delete and return the largest key on the priority queue. * Throw an exception if the priority queue is empty. */ public K delMax() { if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); //first exchange max to the end of the list to erase exch(root,size); //exch(parent,lchild);normally we want to exchange index 1 with N; and then decrement N by one because we will be decreasing Node so size needs to go down by 1; return null; }
private void showHeap() { for(int i=1; i<= size;i++) StdOut.print(pq[i]+ " "); StdOut.println(); }
public static void main(String[] args) { PtrHeap
}
}
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