import java.util.Random; /** * CSE 373, Winter 2011, Jessica Miller * This program tests the PriorityQueue implementations. * YOU SHOULD ADD YOUR TESTS TO THIS FILE. */ public class P3PriorityQueueTest { public static void main(String[] args) { testMinMaxBinaryHeap(true); System.out.println(); testMinMaxBinaryHeap(false); System.out.println(); } /** * A very minimal test for the MinMaxBinaryHeap. * @param isMin */ public static void testMinMaxBinaryHeap(boolean isMin) { PriorityQueue minInts = new MinMaxBinaryHeap(isMin); minInts.add(13); minInts.add(20); minInts.add(11); minInts.add(44); minInts.add(3); minInts.add(7); minInts.add(9); System.out.println(minInts); System.out.println(minInts.peek()); while (!minInts.isEmpty()) { System.out.println(minInts.remove()); System.out.println(minInts); } System.out.println(); System.out.println(); PriorityQueue minStrs = new MinMaxBinaryHeap(isMin); minStrs.add("Kona"); minStrs.add("Daisy"); minStrs.add("Meghan"); minStrs.add("Martin"); while (!minStrs.isEmpty()) { System.out.println(minStrs.remove()); } System.out.println(); } /** * Builds a binary heap with of the given size with random integers. * This method could be used to time your binary heap inserts. * @param size * @return */ private static PriorityQueue buildBinaryHeap(int size) { Random rand = new Random(); PriorityQueue bh = new BinaryHeap(); for (int i = 0; i < size; i++) { bh.add(rand.nextInt(2 * size) - size / 2); } return bh; } /** * Builds a four heap with of the given size with random integers. * This method could be used to time your four heap inserts. * @param size * @return */ private static PriorityQueue buildFourHeap(int size) { Random rand = new Random(); PriorityQueue fh = new FourHeap(); for (int i = 0; i < size; i++) { fh.add(rand.nextInt(2 * size) - size / 2); } return fh; } /** * Empties a priority queue through a series of removes. * This method could be used to time your binary and four heap removes. * @param size * @return */ private static void emptyHeap(PriorityQueue pq) { while (!pq.isEmpty()) { pq.remove(); } } }