Skip to main content

Heap Implementation for Python

Project description

It’s like heapq (blazingly fast) but object-oriented + more features.

Why?

Less code.

What are heaps good for anyway?

They are fast when you need the smallest/biggest item of big collections - Runtime: O(log n).

How?

Suppose you have a heap, then use pop get the smallest one. Heapsort works this way.

heap = Heap(['A', 'D', 'B', 'H', 'E', 'C', 'L', 'J', 'I'])
heap.pop()  # returns A
heap.pop()  # returns B
heap.pop()  # returns C
...

Can I insert an item?

Indeed and it’s as fast as pop. Use push for insertion.

heap = Heap(['A', 'D', 'B', 'H', 'E', 'C', 'L', 'J', 'I'])
heap.push('Z')

Can I remove an item from the middle of a heap?

Yes, that’s what remove is supposed to do.

heap = Heap(['A', 'D', 'B', 'H', 'E', 'C', 'L', 'J', 'I'])
heap.remove('L')     # returns index: 6

A heap is basically a list. So, if you know the index of the item, you can use pop instead.

heap = Heap(['A', 'D', 'B', 'H', 'E', 'C', 'L', 'J', 'I'])
heap.pop(6)          # returns item: L

Max-Heap or Min-Heap?

You define the order of items. Just imagine two heaps of the very same set of items but you need different sorting for each heap. So, you define what min and max means, via cmp.

items = [date(2015, 1, 1), date(2015, 1, 2),  date(2015, 1, 3)]
order1 = Heap(items, cmp=lambda x, y: x.day <= y.day)
order2 = Heap(items, cmp=lambda x, y: x.weekday() >= y.weekday())

Checking Heap Invariant

If you tinker with a heap you can check whether the heap invariant still holds:

heap = Heap([4, 3, 7, 6, 1, 2, 9, 8, 5])
heap[3] = 10           # I know what I am doing here
heap.check_invariant() # but better check... ooops

Conclusion

Good

  • uses C implementation if available (i.e. fast)

  • object-oriented

  • can remove items from within the heap

  • can remove items with unknown index

  • sorting defined per heap (falls back to Pythonic <=)

  • works with Python2 and Python3

Bad

  • no drawbacks discovered so far ;)

  • needs fix:

    • decrease-key and increase-key seem to be another important missing use-case of heapq; so, I will dig into that as well

    • merge heaps

  • ideas are welcome :-)

Project details


Download files

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

Source Distribution

xheap-0.5.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xheap-0.5-py2.py3-none-any.whl (3.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file xheap-0.5.tar.gz.

File metadata

  • Download URL: xheap-0.5.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xheap-0.5.tar.gz
Algorithm Hash digest
SHA256 b078cf6c5d63d4b2db5e6659d51eb5fd6fcc3aec5940b1abad8dc4a38d6d956a
MD5 c1ace0d3612c3b4026e2a6d92c04d0f7
BLAKE2b-256 41781b32a5b4f9206060f8c842634ee608ba7587f9ee0fcc62c71dbf61424cd3

See more details on using hashes here.

File details

Details for the file xheap-0.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for xheap-0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 30973af298cb16ce4c7c016aed29f2c3e5910c2a693572ab77b2876fdb89fcd3
MD5 7dd0de02976df0c0e2d4631e58726bce
BLAKE2b-256 a6ceed6865a35ae918d0e3087e984bc71f3db5f78a1c3c29a5f97f327c18b9bc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page