@Override
    @SuppressWarnings("unchecked")
    public Handle<K, V> deleteMax() {
        if (size == 0) {
            throw new NoSuchElementException();
        } else if (size == 1) {
            Handle<K, V> max = free;
            free = null;
            size--;
            return max;
        } else if (size % 2 == 0) {
            // find max
            AddressableHeap.Handle<K, HandleMap<K, V>> maxInner = maxHeap.deleteMin();
            ReflectedHandle<K, V> maxOuter = maxInner.getValue().outer;
            maxOuter.inner = null;
            maxOuter.minNotMax = false;

            // delete min and keep as free
            AddressableHeap.Handle<K, HandleMap<K, V>> minInner = maxInner.getValue().otherInner;
            ReflectedHandle<K, V> minOuter = minInner.getValue().outer;
            minInner.delete();
            minOuter.inner = null;
            minOuter.minNotMax = false;

            free = minOuter;
            size--;

            return maxOuter;
        } else {
            // find max
            AddressableHeap.Handle<K, HandleMap<K, V>> maxInner = maxHeap.findMin();
            int c;
            if (comparator == null) {
                c = ((Comparable<? super K>) maxInner.getKey()).compareTo(free.key);
            } else {
                c = comparator.compare(maxInner.getKey(), free.key);
            }
            if (c < 0) {
                Handle<K, V> max = free;
                free = null;
                size--;
                return max;
            }

            // maxInner is larger
            maxInner.delete();
            ReflectedHandle<K, V> maxOuter = maxInner.getValue().outer;
            maxOuter.inner = null;
            maxOuter.minNotMax = false;

            // delete min
            AddressableHeap.Handle<K, HandleMap<K, V>> minInner = maxInner.getValue().otherInner;
            ReflectedHandle<K, V> minOuter = minInner.getValue().outer;
            minInner.delete();
            minOuter.inner = null;
            minOuter.minNotMax = false;

            // reinsert min with free
            insertPair(minOuter, free);
            free = null;
            size--;

            return maxOuter;
        }
    }
@ Override @ SuppressWarnings ( "unchecked" ) public Handle < K , V > deleteMax ( ) { if ( size == 0 ) { throw new NoSuchElementException ( ) ; } else if ( size == 1 ) { Handle < K , V > max = free ; free = null ; size -- ; return max ; } else if ( size % 2 == 0 ) { AddressableHeap . Handle < K , HandleMap < K , V > > maxInner = maxHeap . deleteMin ( ) ; ReflectedHandle < K , V > maxOuter = maxInner . getValue ( ) . outer ; maxOuter . inner = null ; maxOuter . minNotMax = false ; AddressableHeap . Handle < K , HandleMap < K , V > > minInner = maxInner . getValue ( ) . otherInner ; ReflectedHandle < K , V > minOuter = minInner . getValue ( ) . outer ; minInner . delete ( ) ; minOuter . inner = null ; minOuter . minNotMax = false ; free = minOuter ; size -- ; return maxOuter ; } else { AddressableHeap . Handle < K , HandleMap < K , V > > maxInner = maxHeap . findMin ( ) ; int c ; if ( comparator == null ) { c = ( ( Comparable < ? super K > ) maxInner . getKey ( ) ) . compareTo ( free . key ) ; } else { c = comparator . compare ( maxInner . getKey ( ) , free . key ) ; } if ( c < 0 ) { Handle < K , V > max = free ; free = null ; size -- ; return max ; } maxInner . delete ( ) ; ReflectedHandle < K , V > maxOuter = maxInner . getValue ( ) . outer ; maxOuter . inner = null ; maxOuter . minNotMax = false ; AddressableHeap . Handle < K , HandleMap < K , V > > minInner = maxInner . getValue ( ) . otherInner ; ReflectedHandle < K , V > minOuter = minInner . getValue ( ) . outer ; minInner . delete ( ) ; minOuter . inner = null ; minOuter . minNotMax = false ; insertPair ( minOuter , free ) ; free = null ; size -- ; return maxOuter ; } }
{@inheritDoc}