插入
void modify ( int pos ) {
if ( pos == 1 || H[ pos / 2 ] <= H[ pos ] )
return ;
else {
swap ( H[ pos / 2 ] , H[ pos ] ) ;
modify ( pos / 2 ) ;
}
}
void Hinsert ( int val ) {
H[ ++cnt ] = val ;
modify ( cnt ) ;
}
删除
void repair ( int pos ) {
if ( pos * 2 > cnt ) return ;
int tar = pos * 2 ;
if ( tar + 1 <= cnt )
tar = H[ tar ] < H[ tar + 1 ] ? tar : tar + 1 ;
if ( H[ pos ] > H[ tar ] ) {
swap ( H[ pos ] , H[ tar ] ) ;
repair ( tar ) ;
}
}
void Hdelete ( ) {
swap ( H[ cnt-- ] , H[ 1 ] ) ;
repair ( 1 ) ;
}