【0】README

0.1)本文总结于 数据结构与算法分析;源代码均为原创, 旨在了解到我们学习了优先队列后,还能干些什么东西出来, 增加学习的interest;
0.2)以下列出了 关于二叉堆(优先队列)的其他有用的操作, 其内容基本可以囊括大部分的优先队列知识点, 亮点是这里还引入到了 堆排序
0.3)二叉堆的insert 和 deleteMin 基本操作和概念,参见 http://blog.csdn.net/pacosonswjtu/article/details/49498023


【1】二叉堆的操作应用

1.1)降低关键字的值——decreaseKey

  • 1)decreaseKey概述: decreaseKey(P, △, H) 操作降低在位置 P处的关键字的值, 降值的幅度为正的量△; 由于这可能破坏堆的序, 因此必须通过 上滤 对堆进行调整;
  • 2)decreaseKey应用: 该操作对 系统管理程序是有用的, 系统管理程序能够使它们的程序以最高的优先级来运行;

1.2)增加关键字的值——increaseKey

  • 1)increaseKey概述: increaseKey(P, △, H) 操作增加在位置 P处的关键字的值,增值的幅度为正的量△; 由于这可能破坏堆的序, 因此必须通过 下滤 对堆进行调整;
  • 2)increaseKey应用: 许多os 调度程序自动地降低正在过多地消耗 CPU 时间的进程的优先级;

1.3)删除某个位置上的元素——deleteElement

  • 1)deleteElement概述: delete(P, H)操作删除堆中位置P 上的节点,这通过首先执行 decreaseKey(P, ∞, H), 然后再执行 deleteMin 来完成;
  • 2)deleteElement 应用:当一个进程被用户终止(而不是正常终止时), 它必须从优先队列中除去;

1.4)构建堆——buildHeap

  • 1)buildHeap 概述: buildHeap(H) 操作把N个关键字作为输入并把他们放入空堆中;显然, 这可以使用 N个相继的insert操作来完成;
  • 2)buildHeap 应用:

1.5)优先队列的应用(我这里只列出 了选择问题, 当然远远不止这一个应用, 这个应用我还没有编写源代码实现)

  • 1)我们将要考察的第一个问题是选择问题: 当时的输入是 N 个元素以及一个整数 k, 这N 个元素的集可以是全序的,该选择问题是要找出 第k个最大的元素;
  • 2)利用优先队列来解决(二叉堆属于优先队列): 对上述N个元素建立 大根堆, 然后删除k-1个最大元素, 那么二叉堆的根节点的元素就是第k个最大元素;
  • 3)堆排序: 注意, 如果我们对 k=N 运行该程序并在 元素离开对时记录他们的值, 那么实际上已经对输入文件以时间 O(NlogN) 做了排序,这样就得到一种快速的排序算法——堆排序;

Attention)

  • A1) 改动的节点的儿子情况只有3种: 要么一个儿子都没有, 要么只有一个左儿子,要么有两个儿子 (不可能只有一个右儿子);
  • A2) 就编程而言,上滤的难度 小于 下滤的难度;
    其他堆操作的源代码(核心是 堆的上滤 percolateUp 和下滤percolateDown操作):

【2】source code+printing

2.0)Warning of percolateDown :

  • W1)因为涉及到利用下滤操作把随机数组(下标从0开始)建立堆(buildHeap 操作) , 然而在堆的其他操作,如插入,删除操作中,使用的下标是从1开始以便于编程;所以我们的下滤操作分为下标从0开始的percolateDownFromZero 和 下标从1开始的percolateDownFromOne,不过上述percolateDown 的两个版本的 编程idea 都是一样的,只是数组的起始下标不一样而已,这是我们应该注意的;
  • W2) 代码中, 只有buildHeap 使用的下滤 percolateDownFromZero 版本,而其他操作使用的是 percolateDownFromOne;
  • W3)你要知道实现 堆操作的代码核心是: 上滤percolateUpFromOne 和下滤操作 percolateDownFromOne + percolateDownFromZero;

2.1)download source code :
https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter6
2.2)source code at a glance:
[1st file binaryheap.h]

#include <stdio.h>
#include <malloc.h>#define ElementType int
#define Error(str) printf("\n error: %s \n",str)   struct BinaryHeap;
typedef struct BinaryHeap *BinaryHeap;void swap(ElementType *x, ElementType *y);
BinaryHeap initBinaryHeap(int capacity);
void insert(ElementType value, BinaryHeap bh);
ElementType deleteMin(BinaryHeap);
int isFull(BinaryHeap bh);
int isEmpty(BinaryHeap bh);
void percolateUp(int index, BinaryHeap bh);
void percolateDownFromOne(int index, BinaryHeap bh);
void printBinaryHeap(BinaryHeap bh);
void printBinaryHeapFromZero(BinaryHeap bh);struct BinaryHeap
{int capacity;int size;   ElementType *elements;
};

[2st file binaryHeapBasicOperation.c]

#include "binaryheap.h"//judge whether the BinaryHeap is full or not , also 1 or 0
int isFull(BinaryHeap bh)
{return bh->size == bh->capacity - 1 ? 1 : 0 ;
}//judge whether the BinaryHeap is empty or not , also 1 or 0
int isEmpty(BinaryHeap bh)
{return bh->size == 0 ? 1 : 0 ;
}void swap(ElementType *x, ElementType *y)
{ElementType temp;temp = *x;*x = *y;*y = temp;
}// get the left child of node under index with startup 1
int leftChildFromOne(int index)
{return index * 2;
}ElementType deleteMin(BinaryHeap bh)
{   ElementType minimum;ElementType *data;  if(isEmpty(bh)){Error("failed deleting minimum , for the BinaryHeap is empty, from func deleteMin !");return -1;  }data = bh->elements;    minimum = data[1];swap(&data[1], &data[bh->size]);bh->size-- ; // size-- occurs prior to percolateDownFromOne percolateDownFromOne(1, bh) ;   return minimum;
} // Attention, the index of the heap starts from 1
void insert(ElementType value, BinaryHeap bh)
{int i;if(isFull(bh)){Error("failed insertion , for the BinaryHeap is full, from func insert!");return ;    }for(i = ++bh->size; bh->elements[i/2] > value; i /= 2)bh->elements[i] = bh->elements[i / 2];bh->elements[i] = value;
}BinaryHeap initBinaryHeap(int capacity)
{BinaryHeap bh;ElementType *temp;bh = (BinaryHeap)malloc(sizeof(struct BinaryHeap));if(!bh) {Error("out of space, from func initBinaryHeap");        return NULL;}  bh->capacity = capacity;bh->size = 0;temp = (ElementType *)malloc(capacity * sizeof(ElementType));if(!temp) {Error("out of space, from func initBinaryHeap");        return NULL;} bh->elements = temp;return bh;
}void printBinaryHeap(BinaryHeap bh)
{int i;ElementType *temp;if(!bh)Error("printing execution failure, for binary heap is null, from func printBinaryHeap");    temp = bh->elements;for(i = 1; i < bh->capacity; i++){printf("\n\t index[%d] = ", i);if(i <= bh->size)printf("%d", bh->elements[i]);elseprintf("NULL");}printf("\n");
}  //print the binary heap who starts from index 0
void printBinaryHeapFromZero(BinaryHeap bh)
{int i;ElementType *temp;if(!bh)Error("printing execution failure, for binary heap is null, from func printBinaryHeap");    temp = bh->elements;for(i = 0; i < bh->capacity; i++){printf("\n\t index[%d] = ", i);if(i < bh->size)printf("%d", bh->elements[i]);elseprintf("NULL");}printf("\n");
}

[3rd file binaryHeapOtherOperation.c]

 #include "binaryheap.h"#define Infinity 10000// switch the elements void swap(ElementType *x, ElementType *y)
{ElementType temp;temp = *x;*x = *y;*y = temp;
}// get the parent of the element with index
int parentFromOne(int index)
{return index / 2;
}// percolating up the element when its value is greater than children (minimal heap)//Attention: all of bh->elements starts from index 1void percolateUpFromOne(int index, BinaryHeap bh){  ElementType *data;ElementType temp;int size;   int parent;data = bh->elements;size = bh->size;for(temp = data[index]; parentFromOne(index) > 0; index = parent){parent = parentFromOne(index);if(parent == 0 || temp > data[parent])              break;else        data[index] = data[parent];                                         }data[index] = temp;}// get the left child of node under index with startup one
int leftChildFromOne(int index)
{return index * 2;
}// percolating down the element when its value is greater than children (minimal heap)//Attention: all of bh->elements starts from index 1void percolateDownFromOne(int index, BinaryHeap bh){  ElementType *data;int size;ElementType temp;int child;data = bh->elements;size = bh->size;for(temp = data[index]; leftChildFromOne(index) <= size; index = child){child = leftChildFromOne(index);if(child < size && data[child] > data[child+1])child++;if(temp > data[child])data[index] = data[child];elsebreak;}data[index] = temp;}//decreasing value of the element under index by increment
void decreaseKey(int index, ElementType decrement, BinaryHeap bh)
{   if(index > bh->size || index < 1){Error(" failed decreaseKey, since overstep the boundary! ");return ;}bh->elements[index] -= decrement; // update the element under given indexpercolateUpFromOne(index, bh);
}//increasing value of the element under index by increment
void increaseKey(int index, ElementType increment, BinaryHeap bh)
{   if(index > bh->size || index < 1){Error(" failed increaseKey, since overstep the boundary! ");return ;}bh->elements[index] += increment; // update the element under given indexpercolateDownFromOne(index, bh);
}//deleting the element under index
void deleteElement(int index, BinaryHeap bh)
{decreaseKey(index, Infinity, bh); // 1st step, decreaseKey operation placing the element under index upto the root  deleteMin(bh); //2nd step, deleteMin deleting the element under the root;
} // get the left child of node under index with startup zero
int leftChildFromZero(int index)
{return index * 2 + 1;
}// percolating down the element when its value is greater than children (minimal heap)//Attention: all of bh->elements starts from index 0void percolateDownFromZero(int index, BinaryHeap bh){  ElementType *data;ElementType temp;int size;   int child;data = bh->elements;size = bh->size;for(temp = data[index]; leftChildFromZero(index) < size; index = child){child = leftChildFromZero(index);if(child < size - 1 && data[child] > data[child+1])child++;if(temp > data[child])data[index] = data[child];elsebreak;}data[index] = temp;
}// building the heap with data in array randomly
void buildHeap(BinaryHeap bh)
{int i;  ElementType *data;data = bh->elements;for(i = bh->size/2; i >= 0; i--)percolateDownFromZero(i, bh);
}int main()
{ElementType data[] = {85, 80, 40, 30, 10, 70, 110}; // P141 ElementType buildHeapData[] = {150, 80, 40, 30, 10, 70, 110, 100, 20, 90, 60, 50, 120, 140, 130};BinaryHeap bh;  int size;int i;  int capacity;printf("\n\t=== test for inserting the binary heap with {85, 80, 40, 30, 10, 70, 110} in turn ===\n");capacity = 14;bh = initBinaryHeap(capacity);size = 7;   for(i = 0; i < size; i++)insert(data[i], bh);printBinaryHeap(bh);printf("\n\t=== test for inserting the binary heap with {100, 20, 90} in turn ===\n");insert(100, bh);insert(20, bh);insert(90, bh);printBinaryHeap(bh);printf("\n\t=== test for inserting the binary heap with 5 ===\n");insert(5, bh);      printBinaryHeap(bh);printf("\n\t=== test for 3 deletings towards the minimum in binary heap ===\n");deleteMin(bh);  printBinaryHeap(bh);deleteMin(bh);      printBinaryHeap(bh);deleteMin(bh);  printBinaryHeap(bh);// other operations in bianry heap  printf("\n\t====== test for other operations in bianry heap as follows ======\n");printf("\n\t=== test for increaseKey(4, 120, bh) ===\n");increaseKey(4, 120, bh);printBinaryHeap(bh);printf("\n\t=== test for increaseKey(2, 120, bh) ===\n");increaseKey(2, 120, bh);printBinaryHeap(bh);printf("\n\t=== test for decreaseKey(9, 195, bh) ===\n");decreaseKey(9, 195, bh);printBinaryHeap(bh);printf("\n\t=== test for decreaseKey(4, 90, bh) ===\n");decreaseKey(4, 90, bh);printBinaryHeap(bh);printf("\n\t=== test for decreaseKey(7, 50, bh) ===\n");decreaseKey(7, 50, bh);printBinaryHeap(bh);printf("\n\t=== test for decreaseKey(5, 155, bh) ===\n");decreaseKey(5, 155, bh);printBinaryHeap(bh);printf("\n\t=== test for deleteElement(4, bh) ===\n");deleteElement(4, bh);printBinaryHeap(bh);printf("\n\t=== test for deleteElement(1, bh) ===\n");deleteElement(1, bh);printBinaryHeap(bh);printf("\n\t=== test for deleteElement(3, bh) ===\n");deleteElement(3, bh);printBinaryHeap(bh); // test over , Bingo!// as you know, the build heap operation is identical with other operationsprintf("\n\t=== test for building heap with {150, 80, 40, 30, 10, 70, 110, 100, 20, 90, 60, 50, 120, 140, 130} ===\n");capacity = 16;bh = initBinaryHeap(capacity);bh->size = 15;bh->elements = buildHeapData; buildHeap(bh);printBinaryHeapFromZero(bh);return 0;
}

2.3)printing results:




关于二叉堆(优先队列)的其他操作及其应用相关推荐

  1. ReviewForJob——二叉堆优先队列的实现(三种堆节点类型——int + struct HeapNode + struct HeapNode*)

    [0]README 1)本文旨在给出 二叉堆优先队列的实现 的代码实现和分析, 而堆节点类型 不外乎三种: 一, 基本类型如int: 二,结构体类型 struct HeapNode: 三,结构体指针类 ...

  2. 图论——Dijkstra+prim算法涉及到的优先队列(二叉堆)

    [0]README 0.1)为什么有这篇文章?因为 Dijkstra算法的优先队列实现 涉及到了一种新的数据结构,即优先队列(二叉堆)的操作需要更改以适应这种新的数据结构,我们暂且吧它定义为Dista ...

  3. 二叉堆--insert操作以及deleteMin操作的实现(C语言)

    二叉堆--insert操作以及deleteMin操作的实现(C语言) 一.概念回顾 1. 什么是二叉堆? 二叉堆其实就是一棵完全二叉树,但是它相比一般的完全二叉树又多了一些限制: (1)对于二叉堆来说 ...

  4. 【数据结构与算法】二项队列与二叉堆的比较

    导语 二叉堆确实是入门级的重要数据结构了,而二项队列也是慢慢要去掌握的一种支持高效合并的优先队列实现.本文稍作比较,望抛砖引玉. 列个表格比较基本操作性能 基本操作 insert(平均) delete ...

  5. 动画 | 什么是二叉堆?

    点击蓝色"五分钟学算法"关注我哟 加个"星标",天天中午 12:15,一起学算法 来源 | 算法无遗策 二叉堆的解释 (动态选择优先级最高的任务执行) 堆,又称 ...

  6. 看动画学算法之:二叉堆Binary Heap

    文章目录 简介 二叉堆的特性 二叉堆的作用 二叉堆的构建 获取二叉堆的最大值 二叉堆的插入 insert操作的时间复杂度 二叉堆的提取Max操作 extractMax的时间复杂度 创建二叉堆 简介 我 ...

  7. 漫画:什么是二叉堆?(修正版)

    转载自  漫画:什么是二叉堆?(修正版) 什么是二叉堆? 二叉堆本质上是一种完全二叉树,它分为两个类型: 1.最大堆 2.最小堆 什么是最大堆呢?最大堆任何一个父节点的值,都大于等于它左右孩子节点的值 ...

  8. 【tree】二叉堆(大顶堆或小顶堆)

    本文目录 一.二叉堆的定义 结构性质 堆序性质 二.二叉堆的底层存储结构 三.二叉堆的插入 四.二叉堆的删除 五.源码和测试 系列目录 <树> <树的遍历> <二叉查找树 ...

  9. 二叉堆(彻底整明白堆排序)

    数据结构与算法笔记 总结不易,如果对你有帮助,请点赞关注支持一下 微信搜索程序dunk,关注公众号,获取博主的数据结构与算法的代码笔记 前言:之前写过堆排序,因为对堆数据结构还是不够了解,懵懵逼逼的写 ...

最新文章

  1. 怎么将一个十进制数转化为二进制数并打印出来
  2. Android ContentResolver
  3. MySQL 的覆盖索引与回表
  4. 中石油训练赛 - 奎奎画画(思维+并查集+离线处理)
  5. 过山车(HDU-2063)
  6. mysql 执行计划 视频_MySQL执行计划的讲解
  7. SQLSERVER对加密的存储过程、视图、触发器进行解密
  8. Linux多线程实践(四 )线程的特定数据
  9. 数据库设计(五)第一范式(1NF)?
  10. 随机函数(Pascal入门)
  11. vmware的原理和影子页表
  12. 【阅读理解】机器阅读理解方向有什么值得follow的大佬,网站等等?
  13. 京东一面:如何用 Nginx 禁止国外 IP 访问网站,直接凉凉!
  14. java计算机毕业设计企业员工工资管理系统源码+系统+数据库+lw文档+mybatis+运行部署
  15. IOS捷径早安,创建自动化可实现自动化叫醒
  16. 网络磁干扰仿真测试软件,上面这些软件哪个用来做电路的电磁干扰仿真比较好?...
  17. 初中计算机数学,初中数学
  18. 一个正项级数收敛, 它的平方也收敛吗?
  19. 深入理解取整、取余与取模问题
  20. 思科《计算机网络》第一章测试答案

热门文章

  1. 【ROI 2019 Day2】课桌【贪心】【决策单调性】【分治】
  2. 快速沃尔什变换:从入门到背板(含推导过程)
  3. P4292-[WC2010]重建计划【长链剖分,线段树,0/1分数规划】
  4. P3750-[六省联考2017]分手是祝愿【期望dp】
  5. 【DP】【容斥】Nice to Meet You(AT3634)
  6. 【结论】立体井字棋(jzoj 2124)
  7. NOIP2013货车运输
  8. Problem G. Graph 2015-2016 acmicpc neerc 拓扑排序模拟
  9. art-template入门(五)之模板变量
  10. Java 集合系列04之 fail-fast总结