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

中山今科网站建设抖音代运营协议

中山今科网站建设,抖音代运营协议,tk域名网站多少,青岛网站建设好不好本文简要总结堆的概念。 更新#xff1a;2023 / 8 / 20 数据结构 | 堆 堆概念方法插入步骤 删除步骤 示例大根堆堆插入删除堆排序 代码实现Python大根堆1.2. heapq 小根堆1.2. heapq 参考链接 堆 概念 如果谈到堆排序#xff0c;那么必然要说说什么是 大根堆 max heap 和 …本文简要总结堆的概念。 更新2023 / 8 / 20 数据结构 | 堆 堆概念方法插入步骤 删除步骤 示例大根堆堆插入删除堆排序 代码实现Python大根堆1.2. heapq 小根堆1.2. heapq 参考链接 堆 概念 如果谈到堆排序那么必然要说说什么是 大根堆 max heap 和 小根堆 min heap 1。 max heap 若根节点存在左右子节点那么根节点的值大于或等于左右子节点的值 是根结点大于或者等于左右子节点的二叉树min heap 若根节点存在左右子节点那么根节点的值小于或等于左右子节点的值 是根结点x小于或者等于左右子节点的二叉树 那么我们可以总结出关于 max heap 和 min heap 的结论 堆是一颗完全二叉树min heap 的根节点是堆中最小值 max heap 的根节点是堆中最大值堆适合采用顺序存储 方法 堆最重要的两个方法就是 插入 和 删除 方法。 插入 将一个元素插入到堆中使之依然成为一个堆。 步骤 将元素插入到堆的尾部查看每个子树是否符合大小根堆的特点。若不符合则将节点逐层向上调整根和叶子节点的位置直至从该节点的父节点出发到根节点是一个有序序列直到所有节点都满足条件最终依然构成一个堆。 删除 堆在删除元素时只可以删除根节点然后使剩下的节点依然成为一个堆。 步骤 删除根节点用堆中最后元素进行填充查看每个子树是否符合大小根堆的特点。若不符合则进行调整直至所有节点都满足条件。 示例 堆的构建过程其实就是构建一个符合大根堆或者小根堆的完全二叉树那么就可以使用顺序表来进行存储。 大根堆 下面举例说明堆的构建过程 将无序序列 [49, 38, 65, 97, 76, 13, 27, 40] 构建大根堆。 堆 插入 49 插入 38 插入 65 因为要建立 max heap49 和 65 不符合大根堆的特点对其进行调整。调整后如下所示 插入 97 97 和 38 发生冲突进行调整 插入 76 76 和 65 发生冲突进行调整 插入 13 插入 27 插入 40 40 和 38 发生冲突进行调整 至此max heap 建立完成。可以看出根节点是最大值。 插入 在上面的基础上向 max heap 插入节点 99。 插入 99 99 和 40 发生冲突进行调整 99 和 76 发生冲突再次进行调整 99 仍然和 97 存在冲突再次进行调整 至此所有节点都符合堆的特性99 被成功插入 max heap。 删除 在上面的基础上因为只能删除根节点所以删除 max heap 的 99。 找到堆中最后的节点 40 删除根节点 99根节点被删除后由最后的节点 40 进行补位。 此时的根节点 40 并不大于或等于左右子节点。因此寻找此时左右子节点中的最大值 97 与此时的根节点 40 互换以进行调整 此时的节点 40 并不大于或等于左子节点 76。因此寻找此时节点 40 与 76 进行互换 至此所有节点都符合堆的特性99 被成功从 max heap 删除。 堆排序 堆排序是如何实现的呢 堆排序其实就是堆的删除过程。每次删除的都是堆的根节点删除后再进行调整使得剩下的节点还是堆然后再删除根节点。重复进行。直至堆中只有一个元素时便可直接输出。那么删除过程中产生的这个序列即是一个有序序列 2。 同样以上面的例子为例来看看堆排序是如何实现的 删除 99 删除 99 的过程可参考上面的 删除 部分的内容。在删除 99 后剩下的元素构成的堆如下所示 删除 97 剩下的元素构成的堆如下所示 删除 76 剩下的元素构成的堆如下所示 删除 65 剩下的元素构成的堆如下所示 删除 49 剩下的元素构成的堆如下所示 删除 40 剩下的元素构成的堆如下所示 删除 38 剩下的元素构成的堆如下所示 删除 27 剩下的元素构成的堆如下所示 至此堆中仅剩一个元素 13。 所以最后排序后的序列为 [99, 97, 76, 65, 49, 40, 38, 27, 13]排序完成。 代码实现 Python 大根堆 1. 以无序序列 [49, 38, 65, 97, 76, 13, 27, 4099] 为例 def maxheap(arr, end):print( * 20 max heapify )height int((end1)/ 2 - 1)print(f{height:20}: {height})for root in range(height, -1, -1):while True:child root * 2 1print(f{root:20}: {root}\n{child:20}: {child}\n{end:20}: {end})if child end:print(f- * 20 f child {child} end {end}, exit loop)breakif child1 end and arr[child] arr[child1]:print(f- * 20 f child1 {child 1} end {end} and arr[child] {arr[child]} arr[child1] {arr[child 1]})child child 1print(f{child now:20}: {child})if arr[root] arr[child]:print(f- * 20 f arr[root] {arr[root]} arr[child] {arr[child]}, root {root} child {child})arr[root], arr[child] arr[child], arr[root]root childprint(f- * 20 f arr[root] {arr[root]} arr[child] {arr[child]}, root {root} child {child} now)else:print(f- * 20 f arr[root] {arr[root]} arr[child] {arr[child]}, root {root} child {child}, exit loop)breakreturn arrdef sortheap(arr, end):for i in range(end, 0, -1):arr[0], arr[i] arr[i], arr[0]maxheap(arr, i-1)return arrif __name__ __main__:a [49, 38, 65, 97, 76, 13, 27, 40, 99]print(f排序前\n{a})print(- * 100 max heapify )a maxheap(a, len(a)-1)print(f大根堆\n{a})print(- * 100 sort heap )a sortheap(a, len(a)-1)print(f堆排序\n{a})输出信息如下所示 排序前 [23, 15, 30, 38, 65, 97, 40, 99] ---------------------------------------------------------------------------------------------------- max heapify max heapify height : 3 root : 3 child : 7 end : 7 -------------------- arr[root] 38 arr[child] 99, root 3 child 7 -------------------- arr[root] 38 arr[child] 38, root 7 child 7 now root : 7 child : 15 end : 7 -------------------- child 15 end 7, exit loop root : 2 child : 5 end : 7 -------------------- arr[root] 30 arr[child] 97, root 2 child 5 -------------------- arr[root] 30 arr[child] 30, root 5 child 5 now root : 5 child : 11 end : 7 -------------------- child 11 end 7, exit loop root : 1 child : 3 end : 7 -------------------- arr[root] 15 arr[child] 99, root 1 child 3 -------------------- arr[root] 15 arr[child] 15, root 3 child 3 now root : 3 child : 7 end : 7 -------------------- arr[root] 15 arr[child] 38, root 3 child 7 -------------------- arr[root] 15 arr[child] 15, root 7 child 7 now root : 7 child : 15 end : 7 -------------------- child 15 end 7, exit loop root : 0 child : 1 end : 7 -------------------- arr[root] 23 arr[child] 99, root 0 child 1 -------------------- arr[root] 23 arr[child] 23, root 1 child 1 now root : 1 child : 3 end : 7 -------------------- child1 4 end 7 and arr[child] 38 arr[child1] 65 child now : 4 -------------------- arr[root] 23 arr[child] 65, root 1 child 4 -------------------- arr[root] 23 arr[child] 23, root 4 child 4 now root : 4 child : 9 end : 7 -------------------- child 9 end 7, exit loop 大根堆 [99, 65, 97, 38, 23, 30, 40, 15] ---------------------------------------------------------------------------------------------------- sort heap max heapify height : 2 root : 2 child : 5 end : 6 -------------------- child1 6 end 6 and arr[child] 30 arr[child1] 40 child now : 6 -------------------- arr[root] 97 arr[child] 40, root 2 child 6, exit loop root : 1 child : 3 end : 6 -------------------- arr[root] 65 arr[child] 38, root 1 child 3, exit loop root : 0 child : 1 end : 6 -------------------- child1 2 end 6 and arr[child] 65 arr[child1] 97 child now : 2 -------------------- arr[root] 15 arr[child] 97, root 0 child 2 -------------------- arr[root] 15 arr[child] 15, root 2 child 2 now root : 2 child : 5 end : 6 -------------------- child1 6 end 6 and arr[child] 30 arr[child1] 40 child now : 6 -------------------- arr[root] 15 arr[child] 40, root 2 child 6 -------------------- arr[root] 15 arr[child] 15, root 6 child 6 now root : 6 child : 13 end : 6 -------------------- child 13 end 6, exit loopmax heapify height : 2 root : 2 child : 5 end : 5 -------------------- arr[root] 40 arr[child] 30, root 2 child 5, exit loop root : 1 child : 3 end : 5 -------------------- arr[root] 65 arr[child] 38, root 1 child 3, exit loop root : 0 child : 1 end : 5 -------------------- arr[root] 15 arr[child] 65, root 0 child 1 -------------------- arr[root] 15 arr[child] 15, root 1 child 1 now root : 1 child : 3 end : 5 -------------------- arr[root] 15 arr[child] 38, root 1 child 3 -------------------- arr[root] 15 arr[child] 15, root 3 child 3 now root : 3 child : 7 end : 5 -------------------- child 7 end 5, exit loopmax heapify height : 1 root : 1 child : 3 end : 4 -------------------- child1 4 end 4 and arr[child] 15 arr[child1] 23 child now : 4 -------------------- arr[root] 38 arr[child] 23, root 1 child 4, exit loop root : 0 child : 1 end : 4 -------------------- child1 2 end 4 and arr[child] 38 arr[child1] 40 child now : 2 -------------------- arr[root] 30 arr[child] 40, root 0 child 2 -------------------- arr[root] 30 arr[child] 30, root 2 child 2 now root : 2 child : 5 end : 4 -------------------- child 5 end 4, exit loopmax heapify height : 1 root : 1 child : 3 end : 3 -------------------- arr[root] 38 arr[child] 15, root 1 child 3, exit loop root : 0 child : 1 end : 3 -------------------- arr[root] 23 arr[child] 38, root 0 child 1 -------------------- arr[root] 23 arr[child] 23, root 1 child 1 now root : 1 child : 3 end : 3 -------------------- arr[root] 23 arr[child] 15, root 1 child 3, exit loopmax heapify height : 0 root : 0 child : 1 end : 2 -------------------- child1 2 end 2 and arr[child] 23 arr[child1] 30 child now : 2 -------------------- arr[root] 15 arr[child] 30, root 0 child 2 -------------------- arr[root] 15 arr[child] 15, root 2 child 2 now root : 2 child : 5 end : 2 -------------------- child 5 end 2, exit loopmax heapify height : 0 root : 0 child : 1 end : 1 -------------------- arr[root] 15 arr[child] 23, root 0 child 1 -------------------- arr[root] 15 arr[child] 15, root 1 child 1 now root : 1 child : 3 end : 1 -------------------- child 3 end 1, exit loopmax heapify height : 0 root : 0 child : 1 end : 0 -------------------- child 1 end 0, exit loop 堆排序 [15, 23, 30, 38, 40, 65, 97, 99]2. heapq 参考这里 3 import heapqa [49, 38, 65, 97, 76, 13, 27, 40, 99] print(f{排序前:20}: {a})print(- * 50 min heapify ) heapq.heapify(a) print(f{小根堆:20}: {a})print(- * 50 max heapify ) newa [(-i, a[i]) for i in range(len(a))] heapq.heapify(newa) print(f{新小根堆:20}: {newa}) maxheap list() while newa:_, s heapq.heappop(newa)print(f_, s: {_}, {s})maxheap.append(s) print(f{大根堆:20}: {maxheap})输出信息如下所示 排序前 : [49, 38, 65, 97, 76, 13, 27, 40, 99] -------------------------------------------------- min heapify 小根堆 : [13, 38, 27, 40, 76, 65, 49, 97, 99] -------------------------------------------------- max heapify 新小根堆 : [(-8, 99), (-7, 97), (-6, 49), (-3, 40), (-4, 76), (-5, 65), (-2, 27), (-1, 38), (0, 13)] _, s: -8, 99 _, s: -7, 97 _, s: -6, 49 _, s: -5, 65 _, s: -4, 76 _, s: -3, 40 _, s: -2, 27 _, s: -1, 38 _, s: 0, 13 大根堆 : [99, 97, 49, 65, 76, 40, 27, 38, 13]小根堆 1. 以无序序列 [49, 38, 65, 97, 76, 13, 27, 4099] 为例 def minheap(arr, start, end):height int((end1)/ 2 - 1)for root in range(height, -1, -1):while True:child root * 2 1if child end:breakif child-1 start and arr[child] arr[child-1]:child child-1if arr[root] arr[child]:arr[root], arr[child] arr[child], arr[root]root childelse:breakreturn arrdef sortheap(arr, start, end):for i in range(end, 0, -1):arr[0], arr[i] arr[i], arr[0]minheap(arr, 0, i-1)return arrif __name__ __main__:a [49, 38, 65, 97, 76, 13, 27, 40, 99]print(f排序前\n{a})print(- * 50 min heapify )a minheap(a, 0, len(a)-1)print(f小根堆\n{a})print(- * 50 sort heap )arr sortheap(a, 0, len(a)-1)print(f堆排序\n{a})输出信息如下所示 排序前 [49, 38, 65, 97, 76, 13, 27, 40, 99] -------------------------------------------------- min heapify 小根堆 [13, 27, 38, 40, 76, 65, 97, 49, 99] -------------------------------------------------- sort heap 堆排序 [99, 97, 76, 65, 49, 40, 38, 27, 13]2. heapq 参考这里 3 import heapqa [49, 38, 65, 97, 76, 13, 27, 40, 99] print(f{排序前:20}: {a})print(- * 50 min heapify ) heapq.heapify(a) print(f{小根堆:20}: {a})print(- * 50 min heapify ) newa [(i, a[i]) for i in range(len(a))] heapq.heapify(newa) print(f{新小根堆:20}: {newa}) minheap list() while newa:_, s heapq.heappop(newa)print(f_, s: {_}, {s})minheap.append(s) print(f{小根堆:20}: {minheap})输出信息如下所示 排序前 : [49, 38, 65, 97, 76, 13, 27, 40, 99] -------------------------------------------------- min heapify 小根堆 : [13, 38, 27, 40, 76, 65, 49, 97, 99] -------------------------------------------------- min heapify 新小根堆 : [(0, 13), (1, 38), (2, 27), (3, 40), (4, 76), (5, 65), (6, 49), (7, 97), (8, 99)] _, s: 0, 13 _, s: 1, 38 _, s: 2, 27 _, s: 3, 40 _, s: 4, 76 _, s: 5, 65 _, s: 6, 49 _, s: 7, 97 _, s: 8, 99 小根堆 : [13, 38, 27, 40, 76, 65, 49, 97, 99]参考链接 #todo: 关于 Python 标准库 heapq 的 大根堆/最大堆的 API~ 浅谈大根堆小根堆以及堆排序python实现 ↩︎ Python 堆排序 ↩︎ python用heapq模块构建大根堆 ↩︎ ↩︎
http://wiki.neutronadmin.com/news/150223/

相关文章:

  • 专业网站设计公司排名保定市网站建设
  • 网站建设协议 模板下载网站关键词先后
  • 深圳网站建设 设计首选遂宁网站建设公司哪家好
  • 官方网站minecraft制作书签教案
  • 佛山企业网站建设公司推荐丹东做网站
  • 做二手电脑的网站链接缩短在线生成器
  • H5网站模板修改教程做网站和做软件哪个赚钱
  • 做网站编辑要会什么道路建设网站
  • 企业网络营销策略研究seo是什么意思中文翻译
  • 江苏省建设厅网站权力阳光系统营销网站建设价格
  • 如何个人电脑做网站生产型或服务型企业网站有哪些
  • 淘宝客网站建设教程dede 电商网站
  • 广州购物网站建设报价邯郸网页
  • 品牌网站建设定位360如何做免费的网站
  • 建设部网站首页督办案件网站和网页有什么区别
  • 公司简介通用模板太原百度快速优化排名
  • jsp网站开发总结百度做网站多少钱能做
  • 自己做网站wordpress表单统计插件下载
  • 网站建设总体费用网页怎么做成app
  • 网站建设案例完整代码九江网站建设服务
  • 做网站电信运营许可证吉利网站建设
  • 服装网站html模板传媒有限公司
  • 网站说服力 营销...ps做网页
  • 织梦网站打不开软件推广平台
  • 云南做网站的公司有哪些做网站的心得
  • 网站的设计思路怎么写中装建设股票行情
  • 龙岗区住房和建设局在线网站公司网站运营方案策划
  • 搭建模板seo营销名词解释
  • 网站开发工具排行江西省建设厅教育网站
  • 新浪做网站库存管理系统软件