Skip to main content

A small heap implementation with customizeable key

Project description

PyHeap

  • Simple implementation of a heap that takes an arbitrary key for sorting.
  • By default, the heaps are minHeaps. (can be changed by setting isMin=False) in constructor

Example Usage:

from PyHeap import Heap

# instantiate an empty heap as follows
# insert elements using the insert method
heap = Heap()
for i in range(5):
    heap.insert(i)
print(heap.heap) # [0. 0, 1, 2, 3, 4]

# instantiate a heap with a predefined array or set
a = [i for i in range(5)]
heap = Heap(a)
print(heap.heap) # [0. 0, 1, 2, 3, 4]

You can perform common heap operations such as insert, getMin/deleteMin:

minVal = heap.getMin() # value is still present in the heap
minVal = heap.deleteMin() # value no longer present

You can also delete an arbitrary value if present in the heap:

val = heap.deleteVal(3)

The heap can also be iterated over:

  • The iteration order goes from smallest to largest if minHeap or largest to smallest if maxHeap
for val in heap:
    print(val)

The heap can also be indexed:

  • In this case, heap[0] returns the smallest number and heap[k] returns the kth smallest

Finally, the heap supports adding any kind of type, as long as you provide a key that provides a method of ordering values.

  • NOTE: by default, the key is an indentity function if the object is not indexable.
  • Otherwise, it is the first element of an object. For example x[0] for a list x.
  • If the key fails on an item, it will raise a ValueError
test = [((3, 4), 2.8284271247461903), ((8, 9), 9.899494936611665), ((10, 2), 9.0), ((0.1, 2), 0.9), ((10, 5), 9.486832980505138)]
heap = Heap(test, key=lambda x:x[1]) # this will order by the second value

# you can change the heaps key as follows (this will change the heap)
heap.reconstructHeap(lambda x: 1/(x + 1))

# outside of the heap, you can get access to the heap's key as folows:
heap.applyKey(3)

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

PyHeap-0.0.4.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

PyHeap-0.0.4-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file PyHeap-0.0.4.tar.gz.

File metadata

  • Download URL: PyHeap-0.0.4.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for PyHeap-0.0.4.tar.gz
Algorithm Hash digest
SHA256 ef8d062942390001dbbd71c8b6f6dbe106cf13a96ec882377d8f50dcdc4ff305
MD5 4345bf1e4666e6a9ff1bbe2bd72a2273
BLAKE2b-256 7b7b4b37182d59c28419fa74e425a6177d4bee7e4cf4b28a799a8d3104b02880

See more details on using hashes here.

File details

Details for the file PyHeap-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: PyHeap-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for PyHeap-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a2446f87f98ec383e026bd0047f13f6b05a06506a6dc86059f08ec80dd1a2072
MD5 e4f823e5f8bd06ab4a98ed6775abc443
BLAKE2b-256 71898cad6e43fdc641d4a3cb5f3fe5c72519978407715c890e7b11698efe6c01

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