
最小-最大堆的性质是:树中偶数层的每个节点都小于它的所有后代,而树中奇数层的每个节点都大于它的所有后代。
腾讯云 2023-03-31 20:33:02
(资料图片仅供参考)
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())
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)
最小-最大堆的性质是:树中偶数层的每个节点都小于它的所有后代,而树中奇数层的每个节点都大于它的所有后代。
用途 双端优先级队列
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 堆属性。
最小-最大堆的性质是:树中偶数层的每个节点都小于它的所有后代,而树中奇数层的每个节点都大于它的所有后代。
2023网上祭英烈平台2022年中华英烈网网上祭英烈入口:http: www chinamartyrs gov cn,点击首页banner即可进入。退役军人事
双杀,三杀,四杀的英文双杀,三杀,四杀的英文双杀,三杀,四杀的英文:双杀:doublekill三杀:triplekill四杀:quadrakill双杀,三杀,四杀
导读近年来,全球发展态势并不乐观,东盟国家不免对本国的经济前景有所担忧。但根据世界银行、国际货币基金组织等机构的预测,在后疫情时代,
为烈士“寻亲”,帮亲人圆梦。近日,在苏桂两地退役军人事务部门工作人员的陪同下,江苏镇江句容籍烈士张靖华的幺弟、80岁的张映奎携孙子张博
勃林格殷格翰将将继续投资并深耕中国市场。未来5年,勃林格殷格翰计划在华追加投资超过9000万欧元,用于生产基地扩建与技术升级。
亚太股份(002284)03月31日在投资者关系平台上答复了投资者关心的问题。投资者:公司在宣布不下调转股价以来,股价及
迈阿密四强揭晓:梅总雷神演德比,辛纳再会阿尔卡!,梅总,疯狂,德比,弗里茨,迈阿密,阿尔卡拉斯,网球运动员,雷神(日本),詹尼克·辛纳
1、先打开QQ邮箱,然后选择右上角“意见反馈|帮助中心|退出”那一排的左边的那个倒三角符号,也就是在昵称右边。2、在倒三
3月31日,“中吉号杯”第33届中国围棋名人战8强战将在深圳龙华继续进行,比赛将于北京时间12:30开始,新浪将进行棋谱直播,欢迎各位棋友点击
台山社安队组织者马作林表示,奥克兰华埠在下午1时18分左右迎来大雨和冰雹,持续约20分钟;先是下大雨,之后在大雨中夹着冰雹。根据“旧金山纪
比亚迪电子(00285)早盘涨超10%,截至发稿,涨9 49%,报24 8港元,成交额1 76亿港元。
总部位于立陶宛的太阳能光伏制造商Solitek公司已将其业务扩展到意大利,在Benevento建设了一处600MW组件厂。该项目将通过欧盟委员会的补贴和So
IT之家3月31日消息,RedmiBuds4哈利・波特定制耳机将于今日10点正式开售,支持35dB
据中新网报道,2023年3月30日,傅聪大使接受《金融时报》专访,重点就中欧关系、中欧经贸合作、乌克兰危机等问题阐明中方