MaxHeap extends AbstractHeap implements IMaxHeap<IResetableComparable> { @Override public void replaceMax(IResetableComparable newElement) { entries[0].reset(newElement); trickleDown(0); } MaxHeap(IResetableComparableFactory factory, int capacity); @Override void getMax(IResetableComparable result); @Override void peekMax(IResetableComparable result); @Override void replaceMax(IResetableComparable newElement);  }
@Test public void testReplaceMax() { int capacity = 10; MaxHeap maxHeap = new MaxHeap(new IntFactory(), capacity); for (int i = capacity; i < capacity * 2; i++) { maxHeap.insert(new Int(i)); } assertEquals(capacity, maxHeap.getNumEntries()); assertFalse(maxHeap.isEmpty()); for (int i = 0; i < capacity; i++) { maxHeap.replaceMax(new Int(i)); } assertEquals(capacity, maxHeap.getNumEntries()); assertFalse(maxHeap.isEmpty()); Int maxI = new Int(); Int peekI = new Int(); int i = 0; while (!maxHeap.isEmpty()) { maxHeap.peekMax(peekI); maxHeap.getMax(maxI); assertTrue(peekI.compareTo(maxI) == 0); assertEquals(i++, capacity - 1 - maxI.i); } }
@Test public void testReplaceMax() { int capacity = 10; MaxHeap maxHeap = new MaxHeap(new IntFactory(), capacity); for (int i = capacity; i < capacity * 2; i++) { maxHeap.insert(new Int(i)); } assertEquals(capacity, maxHeap.getNumEntries()); assertFalse(maxHeap.isEmpty()); for (int i = 0; i < capacity; i++) { maxHeap.replaceMax(new Int(i)); } assertEquals(capacity, maxHeap.getNumEntries()); assertFalse(maxHeap.isEmpty()); Int maxI = new Int(); Int peekI = new Int(); int i = 0; while (!maxHeap.isEmpty()) { maxHeap.peekMax(peekI); maxHeap.getMax(maxI); assertTrue(peekI.compareTo(maxI) == 0); assertEquals( i++, capacity - 1 - maxI.i); } }