#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/3/18 19:47
# @Author : baoshan
# @Site :
# @File : heap.py
# @Software: PyCharm Community Edition # 堆数据结构
class Heap(object):
def __init__(self):
self.data_list = [] def size(self):
return len(self.data_list) def left_child(self, root):
return root * 2 + 1 def right_child(self, root):
return root * 2 + 2 def father(self, node):
return (node - 1) // 2 def heapify(self, root):
if root > self.size() - 1:
return
left_node = self.left_child(root)
right_node = self.right_child(root)
largest = root
if left_node <= self.size() - 1:
if self.data_list[left_node] > self.data_list[largest]:
largest = left_node if right_node <= self.size() - 1:
if self.data_list[right_node] > self.data_list[largest]:
largest = right_node if largest != root:
self.data_list[root], self.data_list[largest] = self.data_list[largest], self.data_list[root]
self.heapify(largest) def build_heap(self, dl):
for i in range(len(dl) - 1, -1, -1):
self.insert(dl[i]) def get_max(self):
if self.size() == 0:
return None
ret = self.data_list[0]
self.data_list[0] = self.data_list[-1]
del self.data_list[-1]
self.heapify(0)
return ret def insert(self, data):
self.data_list.append(data)
now_index = self.size() - 1
pre = self.father(now_index)
while now_index != 0 and self.data_list[pre] < data:
self.data_list[pre], self.data_list[now_index] = self.data_list[now_index], self.data_list[pre]
now_index = pre
pre = now_index // 2 heap = Heap()
heap.insert(9)
heap.insert(10)
heap.insert(7)
heap.insert(12)
heap.insert(3)
heap.insert(4)
print(heap.get_max())
print(heap.get_max())
print(heap.get_max())
print(heap.get_max())
print(heap.get_max())
print(heap.get_max())
print('-' * 100)
heap.insert(10)
heap.insert(9)
heap.insert(8)
heap.insert(7)
heap.insert(7)
heap.insert(12)
print(heap.get_max())
print('-' * 100)
heap1 = Heap()
data_list = [1, 2, 3, 4, 5, 6, 7]
heap1.build_heap(data_list)
print(heap1.get_max())

输出结果:

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/baoshan/PycharmProjects/myProject/python_weixin_study/heap.py
12
10
9
7
4
3
----------------------------------------------------------------------------------------------------
12
----------------------------------------------------------------------------------------------------
7 Process finished with exit code 0

请大侠们指教!

Python实现堆数据结构的更多相关文章

  1. 从一个集合中查找最大最小的N个元素——Python heapq 堆数据结构

    Top N问题在搜索引擎.推荐系统领域应用很广, 如果用我们较为常见的语言,如C.C++.Java等,代码量至少也得五行,但是用Python的话,只用一个函数就能搞定,只需引入heapq(堆队列)这个 ...

  2. Python -- 堆数据结构 heapq - I love this game! - 博客频道 - CSDN.NET

    Python -- 堆数据结构 heapq - I love this game! - 博客频道 - CSDN.NET Python -- 堆数据结构 heapq 分类: Python 2012-09 ...

  3. Python学习笔记——数据结构和算法(一)

    1.解压序列赋值给多个变量 任何的序列(或者是可迭代对象)可以通过一个简单的赋值语句解压并赋值给多个变量. 唯一的前提就是变量的数量必须跟序列元素的数量是一样的. >>> data ...

  4. 堆数据结构(heapq)简单应用

    ## 堆数据结构(heapq)简单应用 # 堆数据结构 heapq # 常用方法:nlargest(),nsmallest(),heapify(),heappop() # 如果需要的个数较小,使用nl ...

  5. Python入门篇-数据结构堆排序Heap Sort

    Python入门篇-数据结构堆排序Heap Sort 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.堆Heap 堆是一个完全二叉树 每个非叶子结点都要大于或者等于其左右孩子结点 ...

  6. Python 中的数据结构总结(一)

    Python 中的数据结构 “数据结构”这个词大家肯定都不陌生,高级程序语言有两个核心,一个是算法,另一个就是数据结构.不管是c语言系列中的数组.链表.树和图,还是java中的各种map,随便抽出一个 ...

  7. python 下的数据结构与算法---8:哈希一下【dict与set的实现】

    少年,不知道你好记不记得第三篇文章讲python内建数据结构的方法及其时间复杂度时里面关于dict与set的时间复杂度[为何访问元素为O(1)]原理我说后面讲吗?其实就是这篇文章讲啦. 目录: 一:H ...

  8. Python算法与数据结构--求所有子数组的和的最大值

    Python算法与数据结构--求所有子数组的和的最大值 玄魂工作室-玄魂 玄魂工作室秘书 玄魂工作室 昨天 题目:输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个 ...

  9. Python中的数据结构

    Python中的数据结构 这里总结一下Python中的内置数据结构(Built-in Data Structure):列表list.元组tuple.字典dict.集合set,涵盖的仅有部分重点,详细地 ...

随机推荐

  1. 【SqlServer】SqlServer中Alter语句的使用

    在修改Sql Server表结构时,常用到Alter语句,把一些常用的alter语句列举如下. 1:向表中添加字段 Alter table [表名] add [列名] 类型 2:  删除字段 Alte ...

  2. 【Spring】Spring框架如何集成Hibernate框架

    下面个整理一下hibernate和Spring框架的结合. 首先是引入hibernate框架的包.Spring框架的包.数据库驱动包. User.java文件 package cn.shop.bean ...

  3. 【HTML】网页中如何让DIV在网页滚动到特定位置时出现

    用js或者jquery比较好实现.但你要知道,滚动到哪个特定位置,例如滚动到一个标题h3那显示这个div,那么可以用jquery算这个h3距离网页顶部的距离:$("h3").off ...

  4. 【转】我为什么离开 Cornell

    我为什么离开 Cornell 很多人都知道,我曾经在 Cornell 博士就读,两年之后转学到了 Indiana 大学.几乎所有人,包括 Indiana 大学的人都感觉奇怪,为什么会有人从 Corne ...

  5. 一段js代码

    原文地址 [].forEach.call($$("*"),function(a){ a.style.outline="1px solid #"+(~~(Math ...

  6. php把采集内容中图片地址下载并替换成本地地址

    把字符串中地址全部获取到一个数组我们利用preg_match_all函数 代码如下 复制代码 <?php$str='<p><img border="0" s ...

  7. wordxml文档格式说明

    近期需要对word xml文档进行各种操作,需要熟悉 wordxml 文档格式,搜索了一番后发现 open xml sdk 官网的文档最好.就按照官网说明来记录一番 1 word xml 文档基本格式 ...

  8. 使用Talend Open Studio将数据分步从oracle导入到hive中

    先使用Tos建立模型,将Oracle中的数据导入到本地: build job后,形成独立可以运行的程序: 将生成的zip文件,上传到hadoop集群上,有hive环境的机器上: [hive@h1 wo ...

  9. WCF 有零个操作;协定必须至少有一个操作

    转自 http://www.cnblogs.com/bdqlaccp/archive/2011/12/31/2308905.html 建立WCF服务后, 服务类中写上了相应的操作,并且方法上加上了[O ...

  10. linux 安装 Headless Chrome

    http://blog.csdn.net/goodzyw/article/details/77269875 https://chromedriver.storage.googleapis.com/in ...