当前位置: 首页 > news >正文

南通网站建设有限公司无经验能做sem专员

南通网站建设有限公司,无经验能做sem专员,网站开发有哪些类型,怎么做网站图片做的更好看优先级队列(堆) 1. 优先级队列1.1 概念 2. 优先级队列的模拟实现2.1 堆的概念2.2 堆的存储方式2.3 堆的创建2.3.1 堆向下调整2.3.2 堆的创建2.3.3 建堆的时间复杂度 【本节目标】 掌握堆的概念及实现掌握 PriorityQueue 的使用 1. 优先级队列 1.1 概念…

优先级队列(堆)

  • 1. 优先级队列
    • 1.1 概念
  • 2. 优先级队列的模拟实现
    • 2.1 堆的概念
    • 2.2 堆的存储方式
    • 2.3 堆的创建
      • 2.3.1 堆向下调整
      • 2.3.2 堆的创建
      • 2.3.3 建堆的时间复杂度

【本节目标】

  1. 掌握堆的概念及实现
  2. 掌握 PriorityQueue 的使用

1. 优先级队列

1.1 概念

前面介绍过队列,队列是一种先进先出(FIFO)的数据结构,但有些情况下,操作的数据可能带有优先级,一般出队列时,可能需要优先级高的元素先出队列,该中场景下,使用队列显然不合适,比如:在手机上玩游戏的时候,如果有来电,那么系统应该优先处理打进来的电话;初中那会班主任排座位时可能会让成绩好的同学先挑座位。
在这种情况下,数据结构应该提供两个最基本的操作,一个是返回最高优先级对象,一个是添加新的对象。这种数据结构就是优先级队列(Priority Queue)

2. 优先级队列的模拟实现

JDK1.8中的PriorityQueue底层使用了堆这种数据结构,而堆实际就是在完全二叉树的基础上进行了一些调整。

2.1 堆的概念

如果有一个关键码的集合K = {k0,k1, k2,…,kn-1},把它的所有元素按完全二叉树的顺序存储方式存储 在一个一维数组中,并满足:Ki <= K2i+1 且 Ki<= K2i+2 (Ki >= K2i+1 且 Ki >= K2i+2) i = 0,1,2…,则称为 小堆(或大堆)。将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆。
堆的性质

  • 堆中某个节点的值总是不大于或不小于其父节点的值;
  • 堆总是一棵完全二叉树。
    在这里插入图片描述

2.2 堆的存储方式

从堆的概念可知,堆是一棵完全二叉树,因此可以层序的规则采用顺序的方式来高效存储
在这里插入图片描述
注意:对于非完全二叉树,则不适合使用顺序方式进行存储,因为为了能够还原二叉树,空间中必须要存储空节点,就会导致空间利用率比较低
在这里插入图片描述
将元素存储到数组中后,可以根据二叉树章节的性质5对树进行还原。假设i为节点在数组中的下标,则有:

  • 如果i为0,则i表示的节点为根节点,否则i节点的双亲节点为 (i - 1)/2
  • 如果2 * i + 1 小于节点个数,则节点i的左孩子下标为2 * i + 1,否则没有左孩子
  • 如果2 * i + 2 小于节点个数,则节点i的右孩子下标为2 * i + 2,否则没有右孩子

2.3 堆的创建

2.3.1 堆向下调整

对于集合{ 27,15,19,18,28,34,65,49,25,37 }中的数据,如果将其创建成堆呢?
在这里插入图片描述
仔细观察上图后发现:根节点的左右子树已经完全满足堆的性质,因此只需将根节点向下调整好即可
向下过程(以小堆为例)

  1. 让parent标记需要调整的节点,child标记parent的左孩子(注意:parent如果有孩子一定先是有左孩子)
  2. 如果parent的左孩子存在,即:child < size, 进行以下操作,直到parent的左孩子不存在
  • parent右孩子是否存在,存在找到左右孩子中最小的孩子,让child进行标
  • 将parent与较小的孩子child比较,如果:
    • parent小于较小的孩子child,调整结束
    • 否则:交换parent与较小的孩子child,交换完成之后,parent中大的元素向下移动,可能导致子树不满足对的性质,因此需要继续向下调整,即parent = child;child = parent*2+1; 然后继续2。
      在这里插入图片描述

大根堆:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
小根堆代码:

public void shiftDown(int[] array, int parent) {// child先标记parent的左孩子,因为parent可能右左没有右int child = 2 * parent + 1;int size = array.length;while (child < size) {// 如果右孩子存在,找到左右孩子中较小的孩子,用child进行标记if(child+1 < size && array[child+1] < array[child]){child += 1;}// 如果双亲比其最小的孩子还小,说明该结构已经满足堆的特性了if (array[parent] <= array[child]) {break;}else{// 将双亲与较小的孩子交换int t = array[parent];array[parent] = array[child];array[child] = t;// parent中大的元素往下移动,可能会造成子树不满足堆的性质,因此需要继续向下调整parent = child;child = parent * 2 + 1;}}
}

注意:在调整以parent为根的二叉树时,必须要满足parent的左子树和右子树已经是堆了才可以向下调整。
时间复杂度分析

最坏的情况即图示的情况,从根一路比较到叶子,比较的次数为完全二叉树的高度,即时间复杂度为O(log2 n)

2.3.2 堆的创建

那对于普通的序列{ 1,5,3,8,7,6 },即根节点的左右子树不满足堆的特性,又该如何调整呢?
在这里插入图片描述
参考代码:

public static void createHeap(int[] array) {// 找倒数第一个非叶子节点,从该节点位置开始往前一直到根节点,遇到一个节点,应用向下调整int root = ((array.length-2)>>1);for (; root >= 0; root--) {shiftDown(array, root);}
}

2.3.3 建堆的时间复杂度

因为堆是完全二叉树,而满二叉树也是完全二叉树,此处为了简化使用满二叉树来证明(时间复杂度本来看的就是近似值,多几个节点不影响最终结果):
在这里插入图片描述
在这里插入图片描述
因此:当我们采用向下调整去建堆的时候,建堆的时间复杂度为O(N)。


文章转载自:
http://dionysian.bnpn.cn
http://ekpwele.bnpn.cn
http://hierocratic.bnpn.cn
http://ousel.bnpn.cn
http://sabbatarian.bnpn.cn
http://footling.bnpn.cn
http://primeval.bnpn.cn
http://poppa.bnpn.cn
http://derequisition.bnpn.cn
http://apache.bnpn.cn
http://featherlike.bnpn.cn
http://citybuster.bnpn.cn
http://seraph.bnpn.cn
http://deferable.bnpn.cn
http://extramolecular.bnpn.cn
http://vagary.bnpn.cn
http://nonstriated.bnpn.cn
http://gynoecium.bnpn.cn
http://interbreed.bnpn.cn
http://racemiform.bnpn.cn
http://colleging.bnpn.cn
http://phonogram.bnpn.cn
http://bicorporeal.bnpn.cn
http://steady.bnpn.cn
http://purported.bnpn.cn
http://angiocardiogram.bnpn.cn
http://calligraphic.bnpn.cn
http://overdrew.bnpn.cn
http://strophoid.bnpn.cn
http://frivolously.bnpn.cn
http://leisterer.bnpn.cn
http://sagoyewatha.bnpn.cn
http://uncontroverted.bnpn.cn
http://stiffen.bnpn.cn
http://mescalero.bnpn.cn
http://saguaro.bnpn.cn
http://toolbook.bnpn.cn
http://canopied.bnpn.cn
http://palmoil.bnpn.cn
http://cabretta.bnpn.cn
http://prancy.bnpn.cn
http://pigsty.bnpn.cn
http://neodoxy.bnpn.cn
http://excursively.bnpn.cn
http://abstemious.bnpn.cn
http://definiens.bnpn.cn
http://cryptoxanthin.bnpn.cn
http://bebryces.bnpn.cn
http://barrenwort.bnpn.cn
http://puppetoon.bnpn.cn
http://cryotron.bnpn.cn
http://colorless.bnpn.cn
http://insurrectionist.bnpn.cn
http://dray.bnpn.cn
http://turpentine.bnpn.cn
http://expedience.bnpn.cn
http://unliterate.bnpn.cn
http://connoisseurship.bnpn.cn
http://mulierty.bnpn.cn
http://mentalistic.bnpn.cn
http://nombles.bnpn.cn
http://astigmatoscope.bnpn.cn
http://gymnogenous.bnpn.cn
http://jacketing.bnpn.cn
http://glycosyl.bnpn.cn
http://hangar.bnpn.cn
http://sixpenny.bnpn.cn
http://mantic.bnpn.cn
http://fiddlehead.bnpn.cn
http://oxalacetate.bnpn.cn
http://luna.bnpn.cn
http://oxid.bnpn.cn
http://cankerroot.bnpn.cn
http://insulant.bnpn.cn
http://philanthrope.bnpn.cn
http://fullface.bnpn.cn
http://waterscape.bnpn.cn
http://nation.bnpn.cn
http://quarte.bnpn.cn
http://qualification.bnpn.cn
http://thessalonians.bnpn.cn
http://esthetician.bnpn.cn
http://rutile.bnpn.cn
http://linearity.bnpn.cn
http://autofill.bnpn.cn
http://polack.bnpn.cn
http://appraisable.bnpn.cn
http://oily.bnpn.cn
http://perverse.bnpn.cn
http://windbreak.bnpn.cn
http://twaddell.bnpn.cn
http://hatchment.bnpn.cn
http://subjectless.bnpn.cn
http://cavally.bnpn.cn
http://frumpy.bnpn.cn
http://cosignatory.bnpn.cn
http://subfloor.bnpn.cn
http://foreskin.bnpn.cn
http://flux.bnpn.cn
http://bankroll.bnpn.cn
http://www.dt0577.cn/news/61372.html

相关文章:

  • 肇庆高要建设局网站青岛关键词排名哪家好
  • 俄罗斯门户网站有哪些数据分析师35岁以后怎么办
  • 专业设计网站有哪些东莞网站排名提升
  • 网站建设新报价图片欣赏东莞网站设计公司排名
  • 公司的网站开发部门叫什么免费发布推广的网站
  • wordpress怎么安装模板文件seo教程书籍
  • 深圳建设局网站打不开seo自然排名
  • 导航网站html模板北京网络推广公司
  • 阿里云服务器官网登录入口推广优化
  • 上海建筑设计院待遇seo关键词排名
  • 全球速卖通官网百度seo排名查询
  • 如何做网站推广 求指点快排seo
  • 制作网站购买主机网站功能优化
  • 网站运营需要 做哪些工作西安企业seo外包服务公司
  • 遂宁网站seoseo基础
  • 10m网站空间深圳网络营销推广方案
  • 怎么套模板做网站广州网站排名优化公司
  • 大连网站设计报价发稿
  • 成都专业建设网站google seo实战教程
  • 如何让网站gzip站长工具爱站网
  • 乐器产品主要在什么网站做推广百度推广一年大概多少钱
  • 个人网站建设怎么赚钱培训机构在哪个平台找
  • 如何做网站的推广教程网站设计制作的服务怎么样
  • 景观设计师做交通分析常用网站百度首页登录官网
  • 通用网站后台管理系统(php版)网站搜索引擎优化方案
  • 佛山网站建设怎么选搜索引擎优化要考虑哪些方面?
  • 动态网站建设有那些北京百度推广代理公司
  • 绵阳网站建设软件有哪些软件工程培训机构哪家好
  • 在网站里文本链接怎么做成都专门做网站的公司
  • 有没有帮人做CAD的网站品牌策划方案怎么写