数据结构之优先队列:优先队列的介绍与基础操作实现,Python代码实现——14
生活随笔
收集整理的這篇文章主要介紹了
数据结构之优先队列:优先队列的介绍与基础操作实现,Python代码实现——14
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
優先隊列(Priority queue)的介紹
- 優先隊列是計算機中一種抽象的數據結構類,它有著一個類似和隊列或者堆的結構,但是其中每個元素額外有一個優先級別
- 在一個優先隊列中,一個高優先順序的元素會先執行與低優先順序的元素。在它的執行過程中,如果兩個元素擁有相同的優先順序,則會根據他們進入隊列的先后順序來確定執行先后,但是在在其他類型的數據結構中,如果兩個元素優先級別相同,則不會定義這兩個元素的先后執行順序
- 優先隊列一般使用堆來進行實現,但他們在概念上與堆是有區別的。一個優先隊列它可以是一個“列表”,也可以是一個“字典”,正如一個列表可以使用鏈表或者數組來實現一樣,優先隊列可以用堆或者其他各種不同的方法,如無序數組來實現
要實現的兩種優先隊列
兩種優先隊列:
使用堆(數組)實現最大優先隊列
定義方法
最大優先隊列Python代碼實現
class MaxPriorityQueue:def __init__(self):self.heap = [None]self.N = 0def size(self):return self.Ndef less(self, i, j):return self.heap[i] < self.heap[j]def is_empty(self):return self.N == 0def swap(self, i, j):self.heap[i], self.heap[j] = self.heap[j], self.heap[i]def append(self, item):self.heap.append(item)self.N += 1self.swim(self.N)def extract_max(self):if self.N < 1:returnfirst = self.heap[1]# print(f"first: {first}")self.swap(1, self.N)del self.heap[self.N]self.N -= 1self.sink(1, self.N)return firstdef sink(self, index, _range):while 2*index <= _range:max_index = 2*index if 2*index+1 > _range else \(2*index+1 if self.less(2*index, 2*index+1) else 2*index)if self.less(max_index, index):breakself.swap(max_index, index)index = max_indexdef swim(self, index):while index > 1:if self.less(int(index/2), index):self.swap(int(index/2), index)index = int(index/2)if __name__ == '__main__':MPQ = MaxPriorityQueue()MPQ.append('A')MPQ.append('B')MPQ.append('C')MPQ.append('D')MPQ.append('E')MPQ.append('F')MPQ.append('G')while not MPQ.is_empty():_max = MPQ.extract_max()print(_max, end=' ')Max Priority Queue運行測試結果
G F E D C B A進行尾部插入元素操作時從小到大附加到隊列,依次抽離最大元素是從大到小返回結果
使用堆(數組)實現最小優先隊列
核心方法與最大優先隊列實習的實現是差不多的,只是在父結點與子結點排序比較時,將兩者位置調換,再進行比較和位置移動操作
最小優先隊列Python代碼實現
import operatorclass MinPriorityQueue:def __init__(self):self.heap = [None]self.N = 0def size(self):return self.Ndef is_empty(self):return self.N == 0def less(self, i, j):return operator.lt(self.heap[i], self.heap[j])def swap(self, i, j):self.heap[i], self.heap[j] = self.heap[j], self.heap[i]def append(self, item):self.heap.append(item)self.N += 1self.swim(self.N)def extract_min(self):if self.N < 1:return_min = self.heap[1]self.swap(1, self.N)del self.heap[self.N]self.N -= 1self.sink(1, self.N)return _mindef swim(self, index):while index > 1:if self.less(index, int(index / 2)):self.swap(index, int(index / 2))index = int(index / 2)def sink(self, index, _range):while 2 * index <= _range:min_index = 2 * index if 2 * index + 1 > _range else \(2 * index if self.less(2 * index, 2 * index + 1) else 2 * index + 1)if self.less(index, min_index):breakself.swap(index, min_index)index = min_indexif __name__ == '__main__':MPQ = MinPriorityQueue()MPQ.append('G')MPQ.append('F')MPQ.append('E')MPQ.append('D')MPQ.append('C')MPQ.append('B')MPQ.append('A')while not MPQ.is_empty():_min = MPQ.extract_min()print(_min, end=' ')Min Priority Queue運行測試結果
A B C D E F G 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的数据结构之优先队列:优先队列的介绍与基础操作实现,Python代码实现——14的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宝塔+wordpress搭建/迁移网站
- 下一篇: 数据结构之图:图的搜索,Python代码