创建最大(小)堆

二叉堆本质上是一种完全二叉树,存储方式并不是链式存储,而是顺序存储

  • 堆操作:插入(叶子节点上调),删除(堆顶元素下沉)

  • 堆创建:非叶子节点下沉(从最后一个非叶子节点开始)

    • 最小堆:

      最小堆任何一个父节点的值,都小于等于它左右孩子节点的值

      创建过程:如果非叶子节点值大于其子节点,将其下沉

    • 最大堆:

      最大堆任何一个父节点的值,都大于等于它左右孩子节点的值。

      创建过程:如果非叶子节点值小于其子节点,将其下沉

#最小堆
def upadjust(nums):
childindex = len(nums)-1
parentindex = (childindex-1)//2
temp = nums[childindex] #插入的叶子节点值
while childindex>0 and temp<nums[parentindex]:#子节点小于父节点,上调子节点
nums[childindex] = nums[parentindex]
childindex = parentindex
parentindex = (parentindex-1)//2
nums[childindex] = temp def downadjust(nums,parentindex):
temp = nums[parentindex]
childindex = 2*parentindex + 1
while childindex < len(nums):
#右孩子值小于左孩子,父节点和小的交换
if childindex +1 <len(nums) and nums[childindex+1] < nums[childindex]:
childindex += 1
if temp < nums[childindex]: #父节点小于子节点,不用调整
break
nums[parentindex] = nums[childindex]
parentindex = childindex
childindex = childindex*2+1
nums[parentindex] = temp def buildMinHeap(nums):
for i in range((len(nums)-1)//2,-1,-1):
downadjust(nums,i)
>>> nums = [5,8,6,3,9,2,1,7,0]
>>> buildMinHeap(nums)
>>> nums
[0, 5, 6, 8, 9, 2, 1, 7, 3]
#最大堆
#非叶子节点小值下沉
def downadjust(nums,parentindex):
temp = nums[parentindex]
childindex = 2*parentindex + 1
while childindex < len(nums):
if childindex +1 <len(nums) and nums[childindex+1] > nums[childindex]:#右孩子值大于左孩子,父节点和大的交换
childindex += 1
if temp > nums[childindex]: #父节点大于子节点,不用调整
break
nums[parentindex] = nums[childindex]
parentindex = childindex
childindex = childindex*2+1
nums[parentindex] = temp def buildMaxHeap(nums):
for i in range((len(nums)-1)//2,-1,-1):
downadjust(nums,i)
>>> nums = [5,8,6,3,9,2,1,7,0]
>>> buildMaxHeap(nums)
>>> nums
[9, 8, 6, 7, 5, 2, 1, 3, 0]

python自带堆模块

>>> import heapq
#默认最小堆
>>> nums = [5,8,6,3,9,2,1,7,0]
>>> heapq.heapify(nums)
>>> nums
[0, 3, 1, 5, 9, 2, 6, 7, 8]

创建堆(python)的更多相关文章

  1. 通过元类创建一个Python类

    通过元类创建一个Python类 最开始学pytohn的时候我们这样定义类 class ClassName: pass 当熟悉了元类的概念之后我们还可以这样创建 ClassName = type(&qu ...

  2. 解决vs2013下创建的python文件,到其他平台(如linux)下中文乱码(或运行时报SyntaxError: (unicode error) 'utf-8' codec can't decode byte...)

    Vs2013中创建python文件,在文件中没输入中文时,编码为utf-8的,如图 接着,在里面输入几行中文后,再次用notepad++查看其编码如下,在vs下运行也报错(用cmd运行就不会): 根据 ...

  3. linux 下创建虚拟环境 python

    virtualenv是一个可以在同一计算机中隔离多个python版本的工具.有时,两个不同的项目可能需要不同版本的python,如 python2.7 / python3.6 ,但是如果都装到一起,经 ...

  4. 创建堆 HeapCreate

    创建额外的堆的原因1.对组件进行保护2.更有效的内存管理3.局部访问4.避免线程同步开销5.快速释放 HeapCreate函数原型:HANDLE WINAPI HeapCreate( _In_ DWO ...

  5. PyCharm中创建项目时,在所创建的python虚拟环境下的pip失效

    在这篇博文里,我简单地叙述了我在使用PyCharm创建一个flask项目时遇到的问题,以及我解决这个问题的过程.其中比较值得注意的点有:①PyCharm创建新项目时的解释器配置②Python虚拟环境的 ...

  6. 创建指定python版本的虚拟环境

    使用virtualenvwrapper管理虚拟环境 鉴于virtualenv不便于对虚拟环境集中管理,所以推荐直接使用virtualenvwrapper. virtualenvwrapper提供了一系 ...

  7. ros2中创建一个python package

    完整的python package的目录结构如下: source /opt/ros/dashing/setup.bash cd ros2_ws/src && ros2 pkg crea ...

  8. 创建一个python类 ,self init相关参数的简单介绍

    一 创建 ''' 一 使用python 语法 创建一个类, 探究self 是干啥的 1 创建一个对象 car 2 写入两个行参 3 定义两个方法 ''' class Car(): ''' 二 init ...

  9. 2019-01-29 VS Code创建自定义Python代码片段

    续前文[日常]Beyond的歌里最多是"唏嘘"吗? - Python分词+词频最后的想法, 发现VS Code支持用户自定义代码片段: Creating your own snip ...

随机推荐

  1. python之 filter

    filter的语法:filter(函数名字,可迭代的变量) 其实filter就是一个“过滤器”:把[可迭代的变量]中的值,挨个地传给函数进行处理,那些使得函数的返回值为True的变量组成的迭代器对象就 ...

  2. 全球疫情统计APP图表形式展示

    全球疫情统计APP图表展示: 将该任务分解成三部分来逐个实现: ①爬取全球的疫情数据存储到云服务器的MySQL上 ②在web项目里添加一个servlet,通过参数的传递得到对应的json数据 ③设计A ...

  3. sklearn概述

    Simple and efficient tools for predictive data analysis Accessible to everybody, and reusable in var ...

  4. How to do error checking in CUDA(如何在CUDA里做错误检查)

    https://codeyarns.com/2011/03/02/how-to-do-error-checking-in-cuda/ Error checks in CUDA code can hel ...

  5. SQL Server2008执行脚本

    "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\osql.exe" -E -i C:\Users\zhiheng\Des ...

  6. SpringCloud学习系列<一>版本介绍

    SpringCloud学习踩坑记<一> SpringCloud版本迭代实在太快,学习起来也是各种坑,博主用的是"当前"的最新版本,借鉴周立老大的Spring Cloud ...

  7. python-集合,列表和元组

    a = {11,22,33} b = [11,22,33] c = (11,22,33,22) 集合:type(a) -->>set#集合内元素不可重复,花括号括起来的还有 -->& ...

  8. Angularjs优点

    数据双向绑定,前后台的更改都可以随时生效, 提供mvc开发模式模式,剥离前端各部分代码,使代码便于维护管理. 简化了你写DOM操作

  9. 五、运算符的补充与if语句

    1.可变不可变类型 指:对前面所学类型做一个可变和不可变类型的分类 可变类型:值改变,ID不变,证明改的是原值,原值是可以被改变的 不可变类型:值改变,ID也变了,证明是产生新的值,压根没有改变原值, ...

  10. 【笔记3-31】Python语言基础-元组tuple

    创建元组 my_tuple = () my_tuple1 = 1, 2, 3, 4, 5, 6 元组解包 与元组元素数量一致 a,s,d,f,g,h = my_tuple1 a, b, c, *f = ...