Python实现堆数据结构
#!/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实现堆数据结构的更多相关文章
- 从一个集合中查找最大最小的N个元素——Python heapq 堆数据结构
Top N问题在搜索引擎.推荐系统领域应用很广, 如果用我们较为常见的语言,如C.C++.Java等,代码量至少也得五行,但是用Python的话,只用一个函数就能搞定,只需引入heapq(堆队列)这个 ...
- Python -- 堆数据结构 heapq - I love this game! - 博客频道 - CSDN.NET
Python -- 堆数据结构 heapq - I love this game! - 博客频道 - CSDN.NET Python -- 堆数据结构 heapq 分类: Python 2012-09 ...
- Python学习笔记——数据结构和算法(一)
1.解压序列赋值给多个变量 任何的序列(或者是可迭代对象)可以通过一个简单的赋值语句解压并赋值给多个变量. 唯一的前提就是变量的数量必须跟序列元素的数量是一样的. >>> data ...
- 堆数据结构(heapq)简单应用
## 堆数据结构(heapq)简单应用 # 堆数据结构 heapq # 常用方法:nlargest(),nsmallest(),heapify(),heappop() # 如果需要的个数较小,使用nl ...
- Python入门篇-数据结构堆排序Heap Sort
Python入门篇-数据结构堆排序Heap Sort 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.堆Heap 堆是一个完全二叉树 每个非叶子结点都要大于或者等于其左右孩子结点 ...
- Python 中的数据结构总结(一)
Python 中的数据结构 “数据结构”这个词大家肯定都不陌生,高级程序语言有两个核心,一个是算法,另一个就是数据结构.不管是c语言系列中的数组.链表.树和图,还是java中的各种map,随便抽出一个 ...
- python 下的数据结构与算法---8:哈希一下【dict与set的实现】
少年,不知道你好记不记得第三篇文章讲python内建数据结构的方法及其时间复杂度时里面关于dict与set的时间复杂度[为何访问元素为O(1)]原理我说后面讲吗?其实就是这篇文章讲啦. 目录: 一:H ...
- Python算法与数据结构--求所有子数组的和的最大值
Python算法与数据结构--求所有子数组的和的最大值 玄魂工作室-玄魂 玄魂工作室秘书 玄魂工作室 昨天 题目:输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个 ...
- Python中的数据结构
Python中的数据结构 这里总结一下Python中的内置数据结构(Built-in Data Structure):列表list.元组tuple.字典dict.集合set,涵盖的仅有部分重点,详细地 ...
随机推荐
- Rot13加密算法
Rot13是一种非常简单的替换加密算法,只能加密26个英语字母.方法是:把每个字母用其后第13个字母代替. 因为有26个字母,取其一半13. s = "xrlvf23xfqwsxsqf&qu ...
- [Spring学习笔记 7 ] Spring中的数据库支持 RowMapper,JdbcDaoSupport 和 事务处理Transaction
1.Spring中的数据库支持 把具有相同功能的代码模板抽取到一个工具类中.2.关于jdbc template的应用 jdbcTemplate模板操作类,把访问jdbc的模板抽取到template中, ...
- iOS配置SSO授权
禁止/激活SSO授权 用于控制平台是否使用SSO方式进行授权(目前只支持新浪微博.Facebook.QQ空间.腾讯微博.人人网.Pocket.默认情况下是激活SSO授权方式.),代码如下: //激活S ...
- 自由是有代价的:聊聊这几年尝试的道路 要想生活好,别看哲学书和思想书。简单看看可以,看多了问题就大了。还是要去研究研究些具体的问题。别jb坐在屋子里,嘴里念着海子的诗,脑袋里想康德想的事情,兜里屁都没有,幻想自己是大国总理,去想影帝是怎么炼成的。
自由是有代价的:聊聊这几年尝试的道路 现在不愿意写过多的技术文章了,一点是现在做的技术比较偏,写出来看的人也不多,二来是家庭事务比较繁多,没以前那么有时间写了.最近,园子里多了一些写经历的文章,我也将 ...
- Google大数据技术架构探秘
原文地址:https://blog.csdn.net/bingdata123/article/details/79927507 Google是大数据时代的奠基者,其大数据技术架构一直是互联网公司争相学 ...
- Git管理工具对比(GitBash、EGit、SourceTree)(转载)
Git管理工具对比(GitBash.EGit.SourceTree) GitBash是采用命令行的方式对版本进行管理,功能最为灵活强大,但是由于需要手动输入希望修改的文件名,所以相对繁琐. EGit是 ...
- JAX-RS annotations
@Path("resource_path"):The @Path annotation defines the path to the base URL or resource_p ...
- 【转载】抓包工具Fidder详解(主要来抓取Android中app的请求) 包括https
文章:http://blog.csdn.net/jiangwei0910410003/article/details/19806999/ Fiddler官网:http://www.telerik.co ...
- C#基础第九天-作业-储蓄账户(SavingAccount)和信用账户(CreditAccount)
要求1:完成以下两种账户类型的编码.银行的客户分为两大类:储蓄账户(SavingAccount)和信用账户(CreditAccount),两种的账户类型的区别在于:储蓄账户不允许透支,而信用账户可以透 ...
- 搭建MVC及WEB API项目框架时碰到的问题集合
前言 刚开始创建MVC与Web API的混合项目时,碰到好多问题,今天拿出来跟大家一起分享下.有朋友私信我问项目的分层及文件夹结构在我的第一篇博客中没说清楚,那么接下来我就准备从这些文件怎么分文件夹说 ...