Skip to main content

An implementation of a Min-Max Heap with a MutableMapping interface.

Project description

heap-mapping

This is a library containing an implementation of the min-max heap data structure, which allows for $O(1)$ retrieval and $O(log(n))$ removal of both the minimum and maximum elements of the heap, as well as $O(log(n))$ insertion of keys and changes to their priorities. Heap creation can be additionally performed in $O(n)$ time.

The data structure can be accessed as MutableMapping; keys are the "values" stored in the data structure, and values are their priorities.

Getting started

You can construct a MinMaxHeap from a list of values.

from heap_mapping import MinMaxHeap

heap = MinMaxHeap([5, 0, 4, 1, 2, 9, 8])
print(heap.min_value) # 0
print(heap.max_value) # 9

By default, priorities are the same as the values.

print(heap[5]) # 5

You can specify a key function in the constructor to get a priority from each element.

heap2 = MinMaxHeap([5, 0, 4, 1, 2, 9, 8], key=lambda x: -x)
print(heap2.min_value) # 9
print(heap2.max_value) # 0
print(heap2[5]) # -5

You can add elements with the add method. By default, the key function specified in the constructor is used to get the priority.

heap2.add(7)
print(heap2[7]) # -7

Alternatively, you can manually specify a key.

heap2.add(6, -3)
print(heap2[6]) # -3

You can change the priorities after they've been inserted.

heap2[7] = 1
print(heap2.max_value) # 7

You can look up and delete arbitrary elements in $O(log(n))$ time.

del heap2[6]
print(6 in heap2) # False

Iteration is performed out of order. Note that this order is also not the order in which the elements are stored in the heap!

print(list(heap2)) # [5, 0, 4, 1, 2, 9, 8, 7]

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

heap_mapping-0.2.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

heap_mapping-0.2.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file heap_mapping-0.2.0.tar.gz.

File metadata

  • Download URL: heap_mapping-0.2.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for heap_mapping-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d1d2f25688d9610b605b9ab381e3981809393d6e4ec9f153971aed6bb9175860
MD5 0b091d21fc08d91d4e0f6cd933e93e83
BLAKE2b-256 e0de71eb454e23dcf6b4ff53a952616e5f0ba61102a7f0d7fe08b37725193787

See more details on using hashes here.

Provenance

The following attestation bundles were made for heap_mapping-0.2.0.tar.gz:

Publisher: package.yml on actapia/min-max-heap-mapping

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file heap_mapping-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: heap_mapping-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for heap_mapping-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c30e19a5983736a5f0fa3a509e447d7aff17d9a3e6e032485cc77df941c628c4
MD5 e1ed356c1a7aa7b853981b3c343fdc0e
BLAKE2b-256 8804e2283f85b39e73f95e7182a591a6cc69c45121ae9855810ba78378c700f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for heap_mapping-0.2.0-py3-none-any.whl:

Publisher: package.yml on actapia/min-max-heap-mapping

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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