Skip to main content

Binary Heap Priority Queue

Project description

HitCount GitHub contributors Version License Build Status Downloads

BHPQ - Binary Heap Priority Queue

A binary heap priority queue implementation, thread safe


You can install bhpq from PyPI:

pip install bhpq

bhpq is supported on Python 3.7


from bhpq import BinaryHeapPriorityQueue

# The BinaryHeapPriorityQueue constructor takes two input params:

# - prefer (required param)
#    the preferred object is pushed to the top of the queue
# the prefer input is a lambda function eg:
# prefer=(lambda lhs, rhs: lhs if lhs.val >= rhs.val else rhs)

# - size
#    The initial size allocation of the queue, default value is 10


class Node(object):
def __init__(self, val):
    self.val = val

A = BinaryHeapPriorityQueue(
        prefer=(lambda lhs, rhs: lhs if lhs.val >= rhs.val else rhs), size=5


assert 5 == A.pop().val
assert 4 == A.pop().val
assert 3 == A.pop().val
assert 2 == A.pop().val
assert 1 == A.pop().val
assert None == A.pop()


  • size()

returns the current size of the priority queue

  • peek()

returns the object at the topof the priority queue if it exists else returns None

  • pop()

removes and returns the object at the top of the priority queue if it exists else returns None

  • add(val)

adds an element to the priority queue


Aayush Uppal

Project details

Download files

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

Files for bhpq, version 2.0.1
Filename, size File type Python version Upload date Hashes
Filename, size bhpq-2.0.1-py3-none-any.whl (4.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size bhpq-2.0.1.tar.gz (3.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page