项目作者: aayushuppal

项目描述 :
Binary Heap Priority Queue
高级语言: Python
项目地址: git://github.com/aayushuppal/bhpq.git
创建时间: 2018-11-13T04:25:21Z
项目社区:https://github.com/aayushuppal/bhpq

开源协议:MIT License

下载


HitCount
GitHub contributors
Version
License
Build Status
Downloads

BHPQ - Binary Heap Priority Queue

A binary heap priority queue implementation, thread safe

Installation

You can install bhpq from PyPI:

  1. pip install bhpq

bhpq is supported on Python 3.7

Usage

  1. from bhpq import BinaryHeapPriorityQueue
  2. # The BinaryHeapPriorityQueue constructor takes two input params:
  3. # - prefer (required param)
  4. # the preferred object is pushed to the top of the queue
  5. # the prefer input is a lambda function eg:
  6. # prefer=(lambda lhs, rhs: lhs if lhs.val >= rhs.val else rhs)
  7. # - size
  8. # The initial size allocation of the queue, default value is 10

Example

  1. class Node(object):
  2. def __init__(self, val):
  3. self.val = val
  4. A = BinaryHeapPriorityQueue(
  5. prefer=(lambda lhs, rhs: lhs if lhs.val >= rhs.val else rhs), size=5
  6. )
  7. A.add(Node(1))
  8. A.add(Node(4))
  9. A.add(Node(3))
  10. A.add(Node(5))
  11. A.add(Node(2))
  12. assert 5 == A.pop().val
  13. assert 4 == A.pop().val
  14. assert 3 == A.pop().val
  15. assert 2 == A.pop().val
  16. assert 1 == A.pop().val
  17. assert None == A.pop()

Methods

  • 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

Maintainer

Aayush Uppal