数据结构 堆。

 删除优先级最高的元素 DeleteMin()

 

void down(int bab)
{
    int son=bab*2;
    int a=heap[bab];
    while(son<=hlength)
    {
        if(son<length&&heap[son]>heap[son+1])
            son++;       //确定最小的儿子        
        if(heap[son]>a)  
            break;
        else 
        {
            heap[bab]=heap[son];
            bab=son;
            son=bab*2;
        }
    }
    heap[son]=a;
}
int DeleteMin()
{
    int r=heap[1]; //delete root
    heap[1]=heap[hlength--];
    down(1);
    return r;
}


在堆中插入新元素 Insert()

 

void up(int son)
{
    int bab=son/2;
    int a=heap[son];
    
    while(bab>1&&a<heap[bab])
    {
        heap[son]=heap[bab];
        son=bab;
        bab=son/2;
    }
    heap[bab]=a;
    return ;
}

void Insert(int a)
{
    heap[++hlength]=a;  //加长并将a添加到末尾
    up(hlength);
}

 

 

 

将X位置的优先级提升到p值

void IncreasKey(int x,int p)  //将x的优先级提升到p
{
    if(heap[x]<p) // 如果成立 优先级本身就高
        return ;
    heap[x]=p;
    up(x);
}

建模

void build ()
{
   for(int i=hlength/2;i>0;i--)
    down(i);  //调整每一次
}


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值