其他分享
首页 > 其他分享> > 二叉堆

二叉堆

作者:互联网

插入:

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 ) ;
}

 

标签:repair,cnt,tar,int,void,pos,二叉
来源: https://www.cnblogs.com/xqk0225/p/16078391.html