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 堆属性。
标签:
精彩推送
今热点:甲基异丁酮MIBK商品报价动态(2023-04-05)
交易商品牌 产地交货地最新报价甲基异丁酮MIBK 含量99%芜湖睿鸿化工有限公司国产山东省 济南市16300...
第一百三十三届广交会新参展企业超九千家(权威发布)|今日快讯
第133届中国进出口商品交易会(广交会)将于今年4月15日到5月5日在广东广州举办,全面恢复线下办展,同...
焦点速看:非洲出现“致命24小时”疾病,可能属于人畜共患疾病,世卫组织发出警告
据最新报道,非洲布隆迪近期爆发一种神秘疾病,已导致3人死亡。所有病例均在症状出现24小时内死亡。此外...
超市逛得开心、商品买得放心、日子过得舒心,河北正定乡镇商超丰富乡亲生活_环球动态
数据来源:河北省商务厅、正定县整理货架、补充商品……在河北省石家庄市正定县南岗镇,瑞天超市北孙村...
江苏一家长称中学女儿被班主任猥亵,教育局通报:当事人已被刑拘,已撤销其教师资格 每日热讯
4月4日晚,江苏连云港灌云县教育局发布情况通报:2023年4月3日晚,县公安部门接群众报警,反映东王集中...
【世界快播报】blackpink五周年文案朋友圈_blackpink五周年文案
1、blackPinkInkKoreaEntertainmentCo Ltd Agirlgroupfro
新闻快讯
新闻快讯
- python实现堆(最大堆、最小堆、最小最大堆)-焦点速读
- 今热点:甲基异丁酮MIBK商品报价动态(2023-04-05)
- 第一百三十三届广交会新参展企业超九千家(权威发布)|今日快讯
- 焦点速看:非洲出现“致命24小时”疾病,可能属于人畜共患疾病,世卫组织发出警告
- 新疆军区某团后装保障演练:布阵旷野砺精兵
- 超市逛得开心、商品买得放心、日子过得舒心,河北正定乡镇商超丰富乡亲生活_环球动态
- 江苏一家长称中学女儿被班主任猥亵,教育局通报:当事人已被刑拘,已撤销其教师资格 每日热讯
- 【世界快播报】blackpink五周年文案朋友圈_blackpink五周年文案
- 泰拉瑞亚月总怎么打第二次(泰拉瑞亚月总怎么打)
- 优化营商环境青岛在行动|青岛市总工会:为新就业形态劳动者打造温馨家园-环球播报
- 微动态丨曲阜市:文旅启智廉洁润心
- 东方国信:数据安全非公司主营业务,公司有参加国内工业互联网领域的数据安全标准研究制定工作
- 2023天津五一车展(时间+门票+看点) 世界视讯
- @所有游客:查收这份清明出游提示 四月这些著名景区免费玩! 天天快播报
- 重庆税务局网上办税大厅官网查询_重庆税务局网上办税大厅官网-全球微头条
- 科源制药上市超募4亿首日破发跌12.3% 2产品产能过剩
- 天天百事通!A股大分化 3300点上方应谨慎!
- 当前速递!白酒中的“地头蛇”们,困在原地
- 朝云集团14亿元,陈丹霞打开集团快速上升通道
- 【速看料】Rivian Q1产量和交付量同比激增:产量增长268% 交付量增长548%
- 北向资金净卖出超40亿元
- 当前讯息:新华全媒+|热血铸警魂 生命写忠诚——追记浙江省瑞安市公安局民警叶永亮
- 2023四川雅安市石棉县森林资源保护林场招聘两名工作人员拟聘用人员的公示_快资讯
- 用热水澡洗去一天的疲惫吧!乐高优秀MOC作品日赏【vol.428】
- 世界最美情诗集_世界上最美的情诗古诗
- 冠的多音字组词语_冠的多音字组词
- 2022统计公报:大国崛起
- 明明是风情万种的大美人,却非要硬凹少女 即时看
- 今日最新!腰筋膜炎的症状及治疗方法(腰筋)
- 王大雷归队李源一暂未现身,李松益加盟河南,海港签下外援中锋 当前滚动
- 芦荟面膜可以敷一晚上吗?
- 万通发展股东GLP累计减持0.35%股份,减持计划时间过半
- 耶的手势图片可爱_耶的手势图片
- 天天信息:2×1000MW!国电电力大同湖东施工调试监理中标候选人公示
- 世界消息!4月3日科德数控涨5.23%,长城优化A基金重仓该股
- 望变电气(603191):盈利稳步提升 硅钢业务持续高端化
- 斗罗大陆:戴沐白邀请唐三同去星罗帝国,自己却有点愧疚 视点
- 云计算ETF、大数据ETF今年涨超50%,AI行情从主题到主线?
- 2023“皖美山水”骑行赛-全椒江淮分水岭风景道站激情开赛
- 当前快讯:国际实业:截至2023年3月31日,公司的股东总户数为39,140户
- 2023两个字独特好听名字女生网名精选网名55个(2020年两个字的名字)|世界消息
- 维宏股份(300508):第四届第八次董事会会议决议,审议《关于公司(2022年度董事会工作报告)的议案》等多项议案-天天速讯
- 世界热资讯!电视机挂悬浮楼梯上,本以为是说着玩,他家却实实在在的做出来了
- 世界热推荐:科学新发现·视频连线丨为何造林没防住今年的沙尘暴?
- 戴维斯谈球队心态 想要赢下剩余六场比赛_每日消息
- t检验 p值可以用什么来算_t检验p值怎么查表
- 德昌杉白坡山个体12
- 低保户被冒名贷款15万?银行:确有员工调查不尽职_全球消息
- 天天短讯!全国铁路4月1日实行新的货物列车运行图
- 爱仕达:公司暂未涉及乡村振兴相关的业务|环球视点