【算法——Python实现】最大堆和最小堆
来源:互联网 发布:沅有芷兮澧有兰 知乎 编辑:程序博客网 时间:2024/05/29 18:50
# _*_ encoding:utf-8 _*_"""最大堆"""class MaxHeap(object): # def __init__(self): # self.data = [] # 创建堆 # self.count = len(self.data) # 元素数量 def __init__(self, arr): self.data = copy.copy(arr) self.count = len(self.data) i = self.count / 2 while i >= 1: self.shiftDown(i) i -= 1 def size(self): return self.count def isEmpty(self): return self.count == 0 def insert(self, item): # 插入元素入堆 self.data.append(item) self.count += 1 self.shiftup(self.count) def shiftup(self, count): # 将插入的元素放到合适位置,保持最大堆 while count > 1 and self.data[(count/2)-1] < self.data[count-1]: self.data[(count/2)-1], self.data[count-1] = self.data[count-1], self.data[(count/2)-1] count /= 2 def extractMax(self): # 出堆 if self.count > 0: ret = self.data[0] self.data[0], self.data[self.count-1] = self.data[self.count-1], self.data[0] self.data.pop() self.count -= 1 self.shiftDown(1) return ret def shiftDown(self, count): # 将堆的索引位置元素向下移动到合适位置,保持最大堆 while 2 * count <= self.count : # 证明有孩子 j = 2 * count if j + 1 <= self.count: # 证明有右孩子 if self.data[j] > self.data[j-1]: j += 1 if self.data[count-1] >= self.data[j-1]: # 堆的索引位置已经大于两个孩子节点,不需要交换了 break self.data[count-1], self.data[j-1] = self.data[j-1], self.data[count-1] count = j
class MinHeap(object): """最小堆""" def __init__(self): self.data = [] # 创建堆 self.count = len(self.data) # 元素数量 # def __init__(self, arr): # self.data = copy.copy(arr) # self.count = len(self.data) # i = self.count / 2 # while i >= 1: # self.shiftDown(i) # i -= 1 def size(self): return self.count def isEmpty(self): return self.count == 0 def insert(self, item): # 插入元素入堆 self.data.append(item) self.count += 1 self.shiftup(self.count) def shiftup(self, count): # 将插入的元素放到合适位置,保持最小堆 while count > 1 and self.data[(count/2)-1] > self.data[count-1]: self.data[(count/2)-1], self.data[count-1] = self.data[count-1], self.data[(count/2)-1] count /= 2 def extractMin(self): # 出堆 if self.count > 0: ret = self.data[0] self.data[0], self.data[self.count-1] = self.data[self.count-1], self.data[0] self.data.pop() self.count -= 1 self.shiftDown(1) return ret def shiftDown(self, count): # 将堆的索引位置元素向下移动到合适位置,保持最小堆 while 2 * count <= self.count : # 证明有孩子 j = 2 * count if j + 1 <= self.count: # 证明有右孩子 if self.data[j] < self.data[j-1]: j += 1 if self.data[count-1] <= self.data[j-1]: # 堆的索引位置已经小于两个孩子节点,不需要交换了 break self.data[count-1], self.data[j-1] = self.data[j-1], self.data[count-1] count = j
阅读全文
0 0
- 【算法——Python实现】最大堆和最小堆
- python 算法实现 - 最小堆
- 【算法——Python实现】最小索引堆,最小堆的优化
- 最大堆和最小堆
- 最大堆和最小堆
- 最大堆和最小堆
- 最大堆和最小堆
- 最大堆MaxHeap和最小堆MinHeap的实现
- 用最大堆和最小堆实现中位数查找
- 使用 stl container priority_queue 实现最小堆和最大堆
- 最小堆和最大堆的JAVA实现
- 最小堆最大堆算法JAVA
- 最小堆&&最大堆的实现(c++)
- 最小堆&&最大堆的实现(c++)
- 最小堆与最大堆的实现
- 最小堆&&最大堆的实现(c++)
- 最大堆与最小堆的实现
- 最大堆、最小堆C++实现
- Unity3d游戏客户端与Photon服务器数据通信
- 扩展BaseAdapter实现储存列表项ListView
- python里使用正则表达式的groups()函数访问组匹配串
- session测试的测试点
- java.lang.OutOfMemoryError: Java heap space解决方法
- 【算法——Python实现】最大堆和最小堆
- 中国大陆省市区县三级、四级菜单数据整理
- 哪些网站使用了vue,及其seo
- 刘强东万字长文谈战略:京东要从“一体化”走向“一体化的开放”
- BZOJ 3675 [Apio2014]序列分割 动态规划+斜率优化
- 解决问题:win10下VS2013报错:此电脑没有安装ActiveX控件
- 数据结构学习——动态逆序对
- JavaScript在JSP页面加载与执行顺序
- Spring Cache扩展:注解失效时间+主动刷新缓存