001package algs24; 002 003import stdlib.StdIn; 004import stdlib.StdOut; 005 006/** 007 * The {@code PMytrHeap} class is the priorityQ class from Question 2.4.24. It 008 * represents a priority queue of generic keys. 009 * 010 * It supports the usual <em>insert</em> and <em>delete-the-maximum</em> 011 * operations, along with methods for peeking at the maximum key, testing if the 012 * priority queue is empty, and iterating through the keys. For additional 013 * documentation, see <a href="http://algs4.cs.princeton.edu/24pq">Section 014 * 2.4</a> of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin 015 * Wayne. 016 */ 017 018public class MyPtrHeap<K extends Comparable<? super K>> { 019 020 /** Create an empty priority queue */ 021 public MyPtrHeap () { 022 // TODO 023 } 024 025 /** Is the priority queue empty? */ 026 public boolean isEmpty () { 027 // TODO 028 return true; 029 } 030 031 /** Return the number of items on the priority queue. */ 032 public int size () { 033 // TODO 034 return 0; 035 } 036 037 /** 038 * Return the largest key on the priority queue. Throw an exception if the 039 * priority queue is empty. 040 */ 041 public K max () { 042 // TODO 043 return null; 044 } 045 046 /** Add a new key to the priority queue. */ 047 public void insert (K x) { 048 // TODO 049 return; 050 } 051 052 /** 053 * Delete and return the largest key on the priority queue. Throw an 054 * exception if the priority queue is empty. 055 */ 056 public K delMax () { 057 // TODO 058 return null; 059 } 060 061 private void showHeap () { 062 // a method to print out the heap 063 // useful for debugging 064 } 065 066 public static void main (String[] args) { 067 MyPtrHeap<String> pq = new MyPtrHeap<> (); 068 StdIn.fromString ("10 20 30 40 50 - - - 05 25 35 - - - 70 80 05 - - - - "); 069 while (!StdIn.isEmpty ()) { 070 StdOut.print ("pq: "); 071 pq.showHeap (); 072 String item = StdIn.readString (); 073 if (item.equals ("-")) StdOut.println ("max: " + pq.delMax ()); 074 else pq.insert (item); 075 } 076 StdOut.println ("(" + pq.size () + " left on pq)"); 077 078 } 079 080}