m3sh.heap.

Heap#

class Heap(items=None)#

Heap data structure.

A heap stores (obj, key) tuples, called heap items. Stored objects have to be hashable. Keys are compared using the < operator. Custom classes used as keys have to implement __lt__().

Parameters:
itemsiterable, optional

A sequence of (obj, key) tuples. If not provided, an empty heap is created.

See also

from_list(), push()

Examples

Given two iterables objs and keys, one holding objects the other holding keys, a heap can be initialized using zip():

>>> heap = Heap(zip(objs, keys))

Attributes

top

Access top item.

Methods

from_list(items)

Initialize heap from list.

pop()

Remove top item.

push(obj, key)

Add item.