python实现堆(最大堆、最小堆、最小最大堆)_热点评
(资料图片)
1. 最大堆
class MaxHeap: def __init__(self): self.heap = [] def parent(self, i): return (i - 1) // 2 def left_child(self, i): return 2 * i + 1 def right_child(self, i): return 2 * i + 2 def get_max(self): if not self.heap: return None return self.heap[0] def insert(self, item): self.heap.append(item) self._heapify_up(len(self.heap) - 1) def extract_max(self): if not self.heap: return None max_item = self.heap[0] last_item = self.heap.pop() if self.heap: self.heap[0] = last_item self._heapify_down(0) return max_item def _heapify_up(self, i): while i > 0 and self.heap[i] > self.heap[self.parent(i)]: self.heap[i], self.heap[self.parent(i)] = self.heap[self.parent(i)], self.heap[i] i = self.parent(i) def _heapify_down(self, i): max_index = i left = self.left_child(i) if left < len(self.heap) and self.heap[left] > self.heap[max_index]: max_index = left right = self.right_child(i) if right < len(self.heap) and self.heap[right] > self.heap[max_index]: max_index = right if i != max_index: self.heap[i], self.heap[max_index] = self.heap[max_index], self.heap[i] self._heapify_down(max_index)if __name__ == "__main__": max_heap = MaxHeap() max_heap.insert(1) max_heap.insert(2) max_heap.insert(0) max_heap.insert(8) print(max_heap.get_max())
2. 最小堆
class MinHeap: def __init__(self): self.heap = [] def parent(self, i): return (i - 1) // 2 def left_child(self, i): return 2 * i + 1 def right_child(self, i): return 2 * i + 2 def get_min(self): if not self.heap: return None return self.heap[0] def insert(self, item): self.heap.append(item) self._heapify_up(len(self.heap) - 1) def extract_min(self): if not self.heap: return None min_item = self.heap[0] last_item = self.heap.pop() if self.heap: self.heap[0] = last_item self._heapify_down(0) return min_item def _heapify_up(self, i): while i > 0 and self.heap[i] < self.heap[self.parent(i)]: self.heap[i], self.heap[self.parent(i)] = self.heap[self.parent(i)], self.heap[i] i = self.parent(i) def _heapify_down(self, i): min_index = i left = self.left_child(i) if left < len(self.heap) and self.heap[left] < self.heap[min_index]: min_index = left right = self.right_child(i) if right < len(self.heap) and self.heap[right] < self.heap[min_index]: min_index = right if i != min_index: self.heap[i], self.heap[min_index] = self.heap[min_index], self.heap[i] self._heapify_down(min_index)
3. 最小-最大堆
最小-最大堆的性质是:树中偶数层的每个节点都小于它的所有后代,而树中奇数层的每个节点都大于它的所有后代。
用途 双端优先级队列
class MinMaxHeap: def __init__(self): self.heap = [] def parent(self, i): return (i - 1) // 2 def left_child(self, i): return 2 * i + 1 def right_child(self, i): return 2 * i + 2 def get_min(self): if not self.heap: return None return self.heap[0] def get_max(self): if not self.heap: return None if len(self.heap) == 1: return self.heap[0] if len(self.heap) == 2: return self.heap[1] if self.heap[1] > self.heap[0] else self.heap[0] return self.heap[1] if self.heap[1] > self.heap[2] else self.heap[2] def insert(self, item): self.heap.append(item) self._heapify_up(len(self.heap) - 1) def extract_min(self): if not self.heap: return None min_item = self.heap[0] last_item = self.heap.pop() if self.heap: self.heap[0] = last_item self._heapify_down_min(0) return min_item def extract_max(self): if not self.heap: return None max_item = self.get_max() max_index = self.heap.index(max_item) self.heap[max_index] = self.heap[-1] self.heap.pop() if max_index < len(self.heap): self._heapify_down_max(max_index) return max_item def _heapify_up(self, i): if i == 0: return parent = self.parent(i) if self.heap[i] < self.heap[parent]: self.heap[i], self.heap[parent] = self.heap[parent], self.heap[i] self._heapify_up_max(parent) else: self._heapify_up_min(i) def _heapify_up_min(self, i): grandparent = self.parent(self.parent(i)) if i > 2 and self.heap[i] < self.heap[grandparent]: self.heap[i], self.heap[grandparent] = self.heap[grandparent], self.heap[i] self._heapify_up_min(grandparent) def _heapify_up_max(self, i): grandparent = self.parent(self.parent(i)) if i > 2 and self.heap[i] > self.heap[grandparent]: self.heap[i], self.heap[grandparent] = self.heap[grandparent], self.heap[i] self._heapify_up_max(grandparent) def _heapify_down_min(self, i): while True: min_index = i left = self.left_child(i) if left < len(self.heap) and self.heap[left] < self.heap[min_index]: min_index = left right = self.right_child(i) if right < len(self.heap) and self.heap[right] < self.heap[min_index]: min_index = right if i != min_index: self.heap[i], self.heap[min_index] = self.heap[min_index], self.heap[i] i = min_index else: break def _heapify_down_max(self, i): while True: max_index = i left = self.left_child(i) if left < len(self.heap) and self.heap[left] > self.heap[max_index]: max_index = left right = self.right_child(i) if right < len(self.heap) and self.heap[right] > self.heap[max_index]: max_index = right if i != max_index: self.heap[i], self.heap[max_index] = self.heap[max_index], self.heap[i] i = max_index else: break
在这个实现中,MinMaxHeap类代表一个min-max堆,包含一个list堆,用于存放堆中的元素。 parent、left_child 和right_child 方法分别返回节点的父节点、左子节点和右子节点的索引。 get_min 方法返回堆中的最小元素,get_max 方法返回堆中的最大元素。 insert 方法将一个元素插入到堆中并维护堆属性。 extract_min 方法从堆中移除最小元素并保持堆属性。 extract_max 方法从堆中移除最大元素并保持堆属性。
_heapify_up、_heapify_up_min、_heapify_up_max、_heapify_down_min 和 _heapify_down_max 方法用于维护最小-最大堆属性。 _heapify_up 在向堆中插入元素后调用,以确保元素位于正确的位置。 _heapify_up_min 和 _heapify_up_max 由 _heapify_up 调用以维护最小-最大堆属性。 _heapify_down_min 和 _heapify_down_max 分别被 extract_min 和 extract_max 调用,以维护 min-max 堆属性。
关键词:
责任编辑:宋璟
-
python实现堆(最大堆、最小堆、最小最大堆)_热点评
-
柠萌影视(09857)发布2022年业绩,净亏损7.32亿元
-
如何清除被子上静电?
-
天天实时:杭州地铁清明运营时间有调整,请看仔细
-
洪恩Q4同比扭亏为盈 营收同比增长9.6%创新高
-
【环球播资讯】三大"PVDF”龙头股一览(3/31)
-
报道:张保航到清涧调研指导公安工作
-
上实控股:全资附属公司SIHLFinance向合营企业提供3亿港元贷款|世界热点
-
热点聚焦:嫦娥五号月球样品研究发现月球“水库”
-
备胎能当正常胎用吗 备胎和正常轮胎有什么不同
-
星纪元兑换码(星纪元兑换码怎么得)_全球热文
-
NIO开始在德国交付ET5降低特斯拉Model3竞争对手的定价|焦点播报
-
泰国餐馆菜谱大全_泰国餐馆
-
世界热议:考研的科目和分数要求_考研的科目
-
太原周边大型墓区附近的景区汇总 新动态
-
惠誉: 今年香港经济将反弹4%-天天新动态
-
苏盐井神:3月30日融券卖出1.01万股,融资融券余额1.95亿元
-
珠光控股:2022年实现收入28.38亿港元|全球资讯
-
侄子去当兵送点什么礼物 天天新消息
-
今日热议:我发现互联网工作的性价比还在持续走低,没看到好转的迹象
-
联赢激光:融资净买入158.71万元,融资余额1.07亿元(03-30) 每日简讯
-
领钱领条的格式及范文_领钱领条范文
-
2021在家赚钱项目-2016年在家赚钱项目|焦点信息
-
最新快讯!战地2142单机怎么进入游戏_战地2142单机账号
-
花楼水塔街道: “一长三员”齐助阵,高效解决居民“揪心事”|天天快播
-
福莱特(601865):龙头厂商盈利能力坚挺 静待行业供需改善
-
【焦点热闻】挪威超23赛季前瞻(上):莫尔德博德闪耀双雄并立,罗森博格不甘寂寞
-
环球短讯!阳光保险披露2022年度业绩:价值增长稳步提升 客户思想深度落地
-
天地科技2022年营收和归母净利润均实现同比双位数增长 热议
-
储奶袋能二次使用么_储奶袋能二次用吗 环球新要闻
-
山东出版:副总经理刘东杰退休离任
-
相约安徽·向春而行|最美四月天,约惠黄山区
-
78家传统农贸市场“智慧”升级|世界今亮点
-
全球球精选!微软正探索在必应聊天回复中投放广告
-
天天热消息:人口回流:中西部7省份去年常住人口止跌回涨