MinMaxHeap extends AbstractHeap implements IMinMaxHeap<IResetableComparable> { @Override public void replaceMax(IResetableComparable newElement) { int maxChild = getMaxChild(0); if (maxChild == NOT_EXIST) { replaceMin(newElement); return; } entries[maxChild].reset(newElement); bubbleUp(maxChild); trickleDown(maxChild); } MinMaxHeap(IResetableComparableFactory factory, int capacity); @Override void getMin(IResetableComparable result); @Override void getMax(IResetableComparable result); boolean isEmpty(); @Override void peekMin(IResetableComparable result); @Override void peekMax(IResetableComparable result); @Override void replaceMin(IResetableComparable newElement); @Override void replaceMax(IResetableComparable newElement);  }
@Test public void testReplaceMax() { int capacity = 10; MinMaxHeap minMaxHeap = new MinMaxHeap(new IntFactory(), capacity); for (int i = 0; i < capacity; i++) { minMaxHeap.insert(new Int(i + capacity)); } assertEquals(capacity, minMaxHeap.getNumEntries()); assertFalse(minMaxHeap.isEmpty()); Int maxI = new Int(); for (int i = capacity; i < capacity * 2; i++) { minMaxHeap.peekMax(maxI); minMaxHeap.replaceMax(new Int(i - capacity)); } assertEquals(capacity, minMaxHeap.getNumEntries()); assertFalse(minMaxHeap.isEmpty()); System.out.println(); Int peekI = new Int(); int i = 0; while (!minMaxHeap.isEmpty()) { minMaxHeap.peekMax(peekI); minMaxHeap.getMax(maxI); assertTrue(peekI.compareTo(maxI) == 0); assertEquals(capacity - i - 1, maxI.i); i++; } }