亚洲第一天堂无码专区_一区二区欧美久久_精品剧情v国产在线观看在线_久久久精品综合

首頁 > 綜合 > 正文

python實現堆(最大堆、最小堆、最小最大堆)

2023-03-31 19:27:00來源:騰訊云  


(資料圖)

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 堆屬性。

標簽:

相關閱讀

精彩推薦

相關詞

推薦閱讀

主站蜘蛛池模板: 日韩在线视频免费观看| 日韩av观看网址V| 欧日韩免费视频| 欧美精品中文字幕一区二区| 久久视频中文字幕| 日韩中文字幕亚洲精品欧美| 91精品国产99久久久久久| 懂色av一区二区三区四区五区| 国产精品自产拍在线观看| 欧美久久在线| 日韩精品一区二区三区外面| 欧美精品在线免费| 欧美日韩国产第一页| 精品网站在线看| 久久久国产精品亚洲一区| 欧美成人午夜剧场免费观看| 色播亚洲婷婷| 日韩专区中文字幕| 久久这里只有精品视频首页| 久久精品一区二区三区不卡免费视频 | 欧美精品中文字幕一区二区| 青青青国产在线观看| 日韩亚洲精品电影| 亚洲午夜精品久久久中文影院av| 久久久久久久久久久久久久久久久久av| 欧美,日韩,国产在线| 国产精品第一页在线| 午夜免费日韩视频| 在线视频不卡一区二区| 日韩av一区二区三区在线观看| 久久人人爽人人爽人人av| 国产成人综合一区二区三区| 欧美日韩国产一二| 91精品在线观| 国产精品久久精品国产| 日韩一区视频在线| 日本不卡在线观看| 97色在线观看免费视频| 日韩欧美视频一区二区三区四区| 97成人精品视频在线观看| 国产精品激情av电影在线观看|