创建最大(小)堆

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

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

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

    • 最小堆:

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

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

    • 最大堆:

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

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

#最小堆
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. 「CSS」常见的清除浮动方法

    下面介绍几种清除浮动的方案,供大家参考: 使用额外的标签clear:both .parent {padding: 10px;width: 200px;background: red;} .child ...

  2. Apex_1. 解决“违反主键约束性”

    1.有创建序列号的可以把序列号调到当前记录ID的最大值+1: 2.进入系统文件system.properties,找到下面代码,把system.id.generator.type的值改为1: #系统默 ...

  3. 13. Java 获取指定字符串出现的次数

    方式一 /** * @param args */ public static void main(String[] args) { String srcText = "Hello World ...

  4. spring动态添加bean

    不知道大家想过没有,我们使用mybatis的时候只要写接口和配置上一个sql语句就可以了,单从代码的角度来看,这是不合理的. 所以我们通常在service里面注入的mapper它其实是一个代理对象 ​ ...

  5. Bisecting GlcNAc is a general suppressor of terminal modification of N-glycan (解读人:王茹凯)

    文献名:Bisecting GlcNAc is a general suppressor of terminal modification of N-glycan(平分GlcNAc是N-聚糖末端修饰的 ...

  6. Mol Cell Proteomics. | 用于鉴定新型融合转录本及其在癌细胞中的潜在翻译产物的多功能蛋白质组基因组学工具FusionPro

    期刊:Molecular & Cellular Proteomics 发表时间:June 17, 2019 DOI:10.1074/mcp.RA119.001456 分享人:任哲 内容与观点: ...

  7. Linux定时备份

    #!/bin/bash. /etc/profile. ~/.bash_profile# Shell script to backup MySql database # To backup Nysql ...

  8. Android Studio Run/Debug configuration error: Module not specified

    如下图,配置时没有module可选,因此报错error: Module not specified 解决方法: 1.打开根目录的settings.gradle,删除include ':app' 2.在 ...

  9. 深入理解JAVA字符串常量池

    初学JAVA时,在学习如何比较两个字符串是否相等,大量资料告诉我,不能用等于号( = )去比较,需要使用equals方法,理由是String是一个对象,等号此时比较的是两个字符串在java内存堆中的地 ...

  10. [AI开发]零代码公式让你明白神经网络的输入输出

    这篇文章的标题比较奇怪,网上可能很少类似专门介绍神经网络的输入输出相关文章.在我实际工作和学习过程中,发现很有必要对神经网络的输入和输出做一个比较全面地介绍.跟之前博客一样,本篇文章不会出现相关代码或 ...