private boolean upHeap ( ) { int currIdx = size - 1 ; E elem = storage . array [ currIdx ] ; int steps = 0 ; while ( currIdx > 0 ) { int parentIdx = currIdx / 2 ; E parent = storage . array [ parentIdx ] ; int cmp = comparator . compare ( elem , parent ) ; if ( cmp == 0 ) { storage . array [ parentIdx ] = mergeOp . merge ( parent , elem ) ; return false ; } else if ( cmp > 0 ) { break ; } currIdx = parentIdx ; steps ++ ; } currIdx = size - 1 ; for ( int i = 0 ; i < steps ; i ++ ) { int parentIdx = currIdx / 2 ; storage . array [ currIdx ] = storage . array [ parentIdx ] ; currIdx = parentIdx ; } storage . array [ currIdx ] = elem ; return true ; }
Moves the last element upwards in the heap until the heap condition is restored .