(资料图)
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 堆属性。
3月23日,中石化易捷第二届出行养车节在四川成都启动。启动仪式上,中石化易捷推出出行养车卡,为消费者提供加油满减、洗车免费、安心出行
伴随着这几年新能源市场的浮沉,消费者对新能源市场从最初的观望逐步过渡到依赖,尤其是混动车型,已经成为了大部分消费者的首选,也正因如
自今年以来,长城汽车销量被各大车企所赶超,而奇瑞也是其中之一。这是因为当下SUV市场各大品牌不断涌入,致使SUV市场达到饱和状态,促使各
中新网上海5月6日电 (记者 许婧)上海市市场监管局6日发布消息称,近日,奉贤区市场监管局接到民众举报,上海品兴农家乐专业合作社在
中新网襄阳5月6日电 (胡传林 李兴会 姚敏)湖北襄阳市气膜实验室6日建成投用,除襄阳外还将承担鄂西北地区(随州、十堰、神农架)的核
中新社武汉5月6日电 (记者 张芹)记者从6日召开的长江防汛抗旱总指挥部2022年指挥长视频会议上获悉,根据当前预测,今年汛期长江流域
中新网北京5月6日电 (徐婧)北京市委宣传部对外新闻处处长田伟在6日的疫情防控工作新闻发布会上指出,北京要有序推进区域核酸筛查,进
中新网绥芬河5月6日电 (记者 王琳)绥芬河市新型冠状病毒肺炎疫情防控应急指挥部于6日发布通告,自5月7日零时起,有序恢复居民生产生
中新社杭州5月6日电 (钱晨菲)记者6日从浙江省水利厅召开的新闻发布会上获悉,该省今年汛情来得早、来得快,梅雨期和台汛期江河发生超
中新网北京5月6日电 (记者 杜燕)北京市场监管部门保持特种设备安全领域严查严管的高压态势,对市区两级发现的所有问题隐患整改情况挂
X 关闭
X 关闭