Priority Queue 在 Queue 的基础上,为 Queue 中的每个元素设置优先级,从而影响 dequeue 的顺序。我们知道 Queue 的 dequeue 是遵循 FIFO 策略,但现实中有很多场景的 dequeue 需要按照一定的优先级实施调度策略:
Binary Heap (以 Max Heap 为例)
# 参考
# http://interactivepython.org/courselib/static/pythonds/Trees/BinaryHeapImplementation.html
class MaxHeap:
def __init__(self):
# start from index 1, so that for node i
# parent index: i//2
# left child index: 2*i
# right child index: 2*i+1
self.h = [0]
self.n = 0
def insert(self, x):
# time complexity: O(logn)
self.h.append(x)
self.n += 1
self._bubbleUp(self.n)
def max(self):
# time complexity: O(1)
self._ensureNotEmpty()
return self.h[1]
def heapify(self, A):
# time complexity: O(n)
self.h, self.n = [0] + A, len(A)
for i in range(self.n//2, 0, -1):
self._bubbleDown(i)
def extractMax(self):
# time complexity: O(logn)
m = self.max()
if self.n == 1:
self.h, self.n = [0], 0
else:
self.h[1], self.h[self.n] = self.h[self.n], self.h[1]
del self.h[self.n]
self.n -= 1
self._bubbleDown(1)
return m
def _ensureNotEmpty(self):
if self.n <= 0:
raise Exception("empty tree")
def _bubbleUp(self, i):
while i//2 > 0:
if self.h[i] > self.h[i//2]:
self.h[i//2], self.h[i] = self.h[i], self.h[i//2]
i = i//2
def _bubbleDown(self, i):
while (i*2) <= self.n:
mci = self._maxChild(i)
if self.h[i] < self.h[mci]:
self.h[i], self.h[mci] = self.h[mci], self.h[i]
i = mci
def _maxChild(self, i):
if i*2 + 1 > self.n:
return i*2
else:
if self.h[i*2] > self.h[i*2+1]:
return i*2
else:
return i*2+1