Heap

Heaps are binary trees where every parent node has a value less than or equal to any of its children. In other words, this type of queue keeps track of the minimum value. Thus it helps retrieve the minimum value at all times.

insert deleteMin remove findMin
Binary Heap O(log n) O(log n) O(log n) O(1)

functions:

heapq.heappush(heap, item)

heapq.heappop(heap)

heapq.heapify(x)
    Transform list x into a heap, in-place, in linear time. (O(n))

examples:

>>> h = []
>>> heappush(h, (5, 'write code'))
>>> heappush(h, (7, 'release product'))
>>> heappush(h, (1, 'write spec'))
>>> heappush(h, (3, 'create tests'))
>>> heappop(h)
(1, 'write spec')

https://docs.python.org/2/library/heapq.html

https://www.cs.cmu.edu/~adamchik/15-121/lectures/Binary Heaps/heaps.html

https://www.geeksforgeeks.org/heap-queue-or-heapq-in-python/

Priority Queue

try:

    import Queue as Q  # ver. < 3.0
except ImportError:
    import queue as Q
from Queue import PriorityQueue

https://www.pythoncentral.io/priority-queue-beginners-guide/

https://docs.python.org/2/library/queue.html

https://discuss.leetcode.com/topic/33609/10-line-python-solution-with-priority-queue

results matching ""

    No results matching ""