Skip to main content

Class-based heap queue implementation

Project description

Travis CI Build Latest PyPI Version

Heap queue (priority queue) implementation.

How to


pip install heapqueue

Import and init an empty heap queue

from heapqueue import BinaryHeap

queue = BinaryHeap()

# BinaryHeap can accept two custom parameters
# comp: a function that indicates how to compare two elements
#     return -1 if a < b, 1 if a > b, and 0 if equal
# size: fixed size of heap queue to truncate results
queue = BinaryHeap(comp=lambda a, b: a['val'] - b['val'], size=3)

Insert elements

# primitive data types

# objects
# note: `comp` is necessary if you want to push objects
queue.push({'foo': 'a', 'bar': 1})
queue.push({'foo': 'b', 'bar': 3})

Get min/max elements

# delete and return the top(max/min) one

# return a sorted list according to `comp` and `size`
# note: this is a property, not a method

# BinaryHeap can handle iteration as well
for item in queue:

# you can also just have a look at top one without deleting it

Remove elements

# remove item equals to 12

# remove item using `comp` for objects
queue.remove({'foo': 'a', 'bar': 1})


Code and documentation are available according to the MIT License (see LICENSE file).

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
heapqueue-0.1b4-py2.py3-none-any.whl (4.7 kB) Copy SHA256 hash SHA256 Wheel py2.py3
heapqueue-0.1b4.tar.gz (5.5 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page