அல்கோரிதம்priority queue

Priority queue

TT
Testlaa Team
May 14, 20261 min read

heapq wrapper class.

Try this in Python

import heapq

class PQ:
    def __init__(self):
        self.h = []

    def push(self, x):
        heapq.heappush(self.h, x)

    def pop(self):
        return heapq.heappop(self.h)

pq = PQ()
pq.push(2)
pq.push(1)
print(pq.pop(), pq.pop())

Key takeaways

  • Not thread-safe.
  • Lazy decrease-key.
  • queue.PriorityQueue.

Tags:

HeapsPythonமாணவர்கள்