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

【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:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331976.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gradle junit5_JUnit 5和Selenium –使用Gradle,JUnit 5和Jupiter Selenium设置项目

gradle junit5Selenium是一组支持浏览器自动化的工具和库&#xff0c;主要用于Web应用程序测试。 Selenium的组件之一是Selenium WebDriver&#xff0c;它提供客户端库&#xff0c;JSON有线协议&#xff08;与浏览器驱动程序进行通信的协议&#xff09;和浏览器驱动程序。 Sele…

ubuntu 两块硬盘挂载不上_win10 轉 Ubuntu

目前用了win10兩三年。發現越來越慢&#xff0c;況且已習慣mac OS&#xff0c;所以想用自己的機子來裝個雙系統Linux&#xff0c;慢慢的將win的東西都轉到Ubuntu上。已清空一個磁盤300G,打算就是在這300G裡裝一個Ubuntu&#xff0c;不知道是否夠用&#xff08;雖然很想裝在三星…

包+类导入+静态导入+类放入包中+包作用域

【0】README 0.1&#xff09;本文转自 core java volume 1&#xff0c; 旨在理清 包和类导入的相关知识&#xff1b; 【1】 包 1.1&#xff09; java 允许使用包将类组织起来&#xff0c;包可以方便组织代码&#xff0c;并将自己的代码与别人提供的代码库分开管理&#xff1b…

selenium自动化测试_使用Selenium自动化测试处理多个浏览器选项卡

selenium自动化测试使用Selenium进行自动化测试一直是将萌芽的自动化测试人员培养为专业人员的生命线。 Selenium是开源的&#xff0c;在全球范围内被广泛采用。 结果&#xff0c;您会得到社区的大力支持。 提供了与Selenium绑定的不同语言的多种框架。 因此&#xff0c;您已经…

qt和c#怎么选_请问目前做windows桌面应用程序,MFC、QT、C#哪个更好?

回答问题之前&#xff0c;先装个逼——没有主导过生命周期三年以上的桌面软件项目的&#xff0c;闭嘴。你连一个桌面软件项目的生命周期都没经历过&#xff0c;你凭什么做技术选型&#xff1f;凭信仰吗&#xff1f;装逼结束&#xff0c;正文开始。首先&#xff0c;非主流技术和…

java 白皮书的关键术语

【0】README 0.1&#xff09; 本文转自 core java volume 1&#xff0c;仅供了解&#xff0c;所谓爱屋及乌嘛&#xff1b; 0.2&#xff09; java的设计者编写了颇有影响力的白皮书&#xff0c;用来解释设计的初衷以及完成的情况&#xff0c;并发布了一个摘要&#xff1b;【1】…

当集合a为空集时a的取值范围_高中数学必修一第一章集合分节练习和章末测试题含答案[1] 2...

高中数学必修1 第一章 集合 分节练习和章末综合测试题含答案1 集合的含义与表示1、下列各组对象能否组成一个集合&#xff1f;(1)接近于0的数的全体&#xff1b; (2)2的近似值的全体&#xff1b; (3)平面上到点O 的距离等于1的点的全体&#xff1b; (4)正三角形的全体&#xff…

spring jpa 流式_从响应式Spring Data存储库流式传输实时更新

spring jpa 流式这篇文章详细介绍了从数据库到对该数据感兴趣的任何其他组件进行流更新的幼稚实现。 更准确地说&#xff0c;如何更改Spring Data R2DBC存储库以向相关订阅者发出事件。 对R2DBC和Spring的一点背景知识将对这篇文章有所帮助。 我以前的著作《 使用 Microsoft S…

弹窗页面交互_UI进阶知识-信息提交类弹窗该如何设计?

原文作者&#xff1a;风筝KK 信息提交类弹窗大家应该都比较熟悉&#xff0c;和其他弹窗的区别在于他有输入、选择等操作&#xff0c;比如我们常见的输入验证码、留言回复、充值转账、任务设置等。看上去设计都比较简单&#xff0c;但是当你验收时就会发现问题&#xff0c;为什么…

selenium并行_如何在不同的浏览器中设置Selenium网格以并行执行

selenium并行到目前为止&#xff0c;Selenium是最常用的Web自动化测试工具。 如此受欢迎的原因之一是Selenium的自动跨浏览器测试功能。 Selenium自动化测试可以帮助您在所有主要浏览器&#xff0c;所有主要操作系统甚至移动设备浏览器上进行测试。 您可以在所有功能测试中获得…

java 发展简史

【0】README 0.1&#xff09; 本文转自 core java volume 1&#xff0c;仅供了解Java 的发展历史&#xff0c;它的前世今生&#xff0c;所谓知己知彼&#xff0c;百战不殆&#xff08;just a joke&#xff09; &#xff1b; 【1】java 发展简史 1.1&#xff09;java的历史要…

axios代理跨域 cli4_跨域本质及解决办法

1、什么是跨域&#xff1f;2、如何解决&#xff1f;跨域是前端所独有的&#xff0c;后端不存在跨域问题。是浏览器的一种安全保护手段&#xff0c;为了防止别人抓取、篡改你的网站数据信息。遵循同源策略、同协议&#xff08;http&#xff09;、同域名、同端口&#xff0c;少一…

如何使用eclemma插件_如何集成和使用EclEmma插件来获得良好的Junit覆盖率

如何使用eclemma插件你好朋友&#xff0c; 如果编写好的代码很重要&#xff0c;那么编写覆盖所有业务逻辑的优良Junit测试用例也同样重要。通过编写覆盖业务逻辑的Junit测试用例&#xff0c;我们实际上确保代码的每种方法都能正常工作按照预期进行&#xff0c;因此减少了在软…

Java 相关术语

【0】README 0.1&#xff09; 本文转自 core java volume 1&#xff0c;仅供了解Java 的相关术语&#xff0c;包括像JDK、JRE等 &#xff1b;而且我感觉&#xff0c;了解了这些过后&#xff0c;你会更懂 Java&#xff0c; 更热爱它&#xff1b; 【2】Java术语 2.1&#xff09;…

一般试卷的纸张大小是多少_pdf试卷怎么打印在A3纸上

一般的PDF试卷的纸张大小都是A4大小&#xff0c;没有现成A3大小Word文档的试卷时&#xff0c;需要将PDF试卷转换Word文档之后进行排版&#xff0c;如何实现这一操作呢&#xff1f;请接着往下学习吧~一、将试卷的PDF格式转换为Word1.进入PDF快转官网&#xff0c;点击下载按钮下载…

设置 JDK环境变量(Windows)

【0】README 0.1&#xff09; 本文转自 core java volume 1&#xff0c;旨在说明如何设置 JDK环境变量&#xff0c;以及为什么要设置的问题&#xff1b;【1】JDK目录树 Attention&#xff09;就Java 而言&#xff0c; docs 和 src 是两个最有用的子目录&#xff1a;因为 docs …

光流法测试代码_高效的企业测试-工作流和代码质量(4/6)

光流法测试代码本文的这一部分将讨论在开发过程中拥有有效工作流程的影响&#xff0c;以及适当的测试代码质量如何使我们能够创建可维护的测试&#xff0c;尤其是对于复杂项目。 开发工作流程和管道 编程是一项流程活动&#xff0c;我们开发人员应该对保持工作流程高效和缩短…

水泵怎么做_泳池设备日常怎么维护和保养?

点击上方“蓝字”&#xff0c;关注我们.●电机突然停止运转怎么办&#xff1f;1.检查电路和电线接口等。2.检查输送到电机的电压是否过低(通常由于电源线太小而引致的电流不足)。3.检查是否有过载而引起电流过大。注&#xff1a;Hayward 水泵的单相电机有自动过热保护装置。该装…

鸡肉部位英文对照_鸡肉和鸡蛋–测试前解决Spring属性

鸡肉部位英文对照考虑一个负责进行远程调用和获取详细信息的服务类&#xff1a; ... public class CitiesService { private final WebClient.Builder webClientBuilder; private final String baseUrl; public CitiesService( WebClient.Builder webClientBuilder, Value ( &…

git pull忽略指定文件_Git忽略提交规则

在使用Git的过程中&#xff0c;我们喜欢有的文件比如日志&#xff0c;临时文件&#xff0c;编译的中间文件等不要提交到代码仓库&#xff0c;这时就要设置相应的忽略规则&#xff0c;来忽略这些文件的提交。简单来说一个场景&#xff1a;在你使用git add .的时候&#xff0c;遇…