麻豆一区二区-麻豆一区-麻豆一精品传媒媒短视频下载-麻豆亚洲一区-麻豆亚洲-麻豆性视频

首頁(yè) > 綜合 > 正文

python實(shí)現(xiàn)堆(最大堆、最小堆、最小最大堆)

2023-03-31 19:27:00來(lái)源:騰訊云  


(資料圖)

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. 最小-最大堆

最小-最大堆的性質(zhì)是:樹(shù)中偶數(shù)層的每個(gè)節(jié)點(diǎn)都小于它的所有后代,而樹(shù)中奇數(shù)層的每個(gè)節(jié)點(diǎn)都大于它的所有后代。

用途 雙端優(yōu)先級(jí)隊(duì)列

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

在這個(gè)實(shí)現(xiàn)中,MinMaxHeap類(lèi)代表一個(gè)min-max堆,包含一個(gè)list堆,用于存放堆中的元素。 parent、left_child 和right_child 方法分別返回節(jié)點(diǎn)的父節(jié)點(diǎn)、左子節(jié)點(diǎn)和右子節(jié)點(diǎn)的索引。 get_min 方法返回堆中的最小元素,get_max 方法返回堆中的最大元素。 insert 方法將一個(gè)元素插入到堆中并維護(hù)堆屬性。 extract_min 方法從堆中移除最小元素并保持堆屬性。 extract_max 方法從堆中移除最大元素并保持堆屬性。

_heapify_up、_heapify_up_min、_heapify_up_max、_heapify_down_min 和 _heapify_down_max 方法用于維護(hù)最小-最大堆屬性。 _heapify_up 在向堆中插入元素后調(diào)用,以確保元素位于正確的位置。 _heapify_up_min 和 _heapify_up_max 由 _heapify_up 調(diào)用以維護(hù)最小-最大堆屬性。 _heapify_down_min 和 _heapify_down_max 分別被 extract_min 和 extract_max 調(diào)用,以維護(hù) min-max 堆屬性。

標(biāo)簽:

相關(guān)閱讀

精彩推薦

相關(guān)詞

推薦閱讀

主站蜘蛛池模板: 欧美久久一区二区三区 | 动漫a级片 | 午夜一区二区福利视频在线 | 明星ai人脸替换造梦在线播放 | 国产小视频在线免费观看 | 91免费高清视频 | 好湿好紧太硬了我太爽了h 好湿好滑好硬好爽好深视频 | 全肉一女n男np高h乳 | 男男同志gaysxxx | 99久久精品国产片久人 | 人与动人物aaaa | 亚洲视频一区二区在线观看 | 武侠古典久久亚洲精品 | 日本高清色视频www 日本高清免费观看 | 性xxxxⅹhd成人 | 亚洲一区二区日韩欧美gif | 日韩aaa| 香蕉97超级碰碰碰免费公 | 天天干天天操天天碰 | 超级碰碰免费视频 | 香蕉eeww99国产精品 | 天堂在线观看中文字幕 | 国产男人搡女人免费视频 | 人人精品久久 | 图片专区亚洲欧美另类 | 色噜噜国产精品视频一区二区 | 日韩亚洲国产激情在线观看 | 激情三级做爰在线观看激情 | 国产成人愉拍精品 | 美国复古性经典xxxxx | 女人狂吮男人命根gif视频 | 亚洲免费闲人蜜桃 | 朝鲜女人free性hu | 国产经典一区 | 日韩在线视频免费不卡一区 | 精久久 | 日本视频观看 | 99在线免费观看 | 四虎影音在线 | 国产精品久久一区 | 国产a一级 |