首页 技术 正文
技术 2022年11月12日
0 收藏 393 点赞 3,443 浏览 840 个字

堆数据结构的一个重要用处就是:最为高效的优先级队列。优先级队列分为最大优先级队列和最小优先级队列,其中最大优先级队列的一个应用实在一台分时计算机上进行作业的调度。当用堆来实现优先级队列时,需要在队中的每个元素里存储对应的应用对象句柄(handle)。这里对象柄用数组下标表示,因为在堆操作中,堆元素会改变在数组中的位置,所以具体实现中为了重新定义堆元素,我们需要更新应用对象中的数组下标。简单的实现代码如下:

int  HeapMaximum(int a[], int &heapSize)
{
return a[];
}
int HeapExtractMax(int a[], int &iHeapSize)
{
if(iHeapSize < )
{
cout << "heap underflow." << endl;
}
int iMax = a[];
a[] = a[iHeapSize];
iHeapSize = iHeapSize - ;
MaxHeapify(a, , iHeapSize);
return iMax;
}
void HeapIncreaseKey(int a[], int iPos, int iKey)
{
if(iKey < a[iPos])
{
cout << "new key is smaller than current key." << endl;
return;
}
a[iPos] = iKey;
while(iPos > && a[parent(iPos)] < a[iPos])
{
swap(a[iPos], a[parent(iPos)]);
iPos = parent(iPos);
}
}
void MaxHeapInsert(int a[], int iKey, int &iHeapSize)
{
iHeapSize += ;
a[iHeapSize] = -INT_MAX;
HeapIncreaseKey(a, iHeapSize, iKey);
}

同时还找到一份不错的学习代码http://www.cnblogs.com/Anker/archive/2013/01/23/2873951.html

好好学习,总会有收获。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295