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(log(n))$ retrieval or removal of both the minimum and maximum elements of the heap, as well as insertion and 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.1.0.tar.gz (7.6 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.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: heap_mapping-0.1.0.tar.gz
  • Upload date:
  • Size: 7.6 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.1.0.tar.gz
Algorithm Hash digest
SHA256 da6c2e488731acadb6c5c94910d1c12879e60cba031bb4aa936c2e65f1193489
MD5 212f1e0df44f6c2f4d5b35f368e0ebe6
BLAKE2b-256 4ca6b773caa839b97a47676e92596324861b4f4933d10eae6c9d368399ca0591

See more details on using hashes here.

Provenance

The following attestation bundles were made for heap_mapping-0.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: heap_mapping-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e89e03f10a699e9134e46629e7d4f586c1fdd0f28c99f9eb8f2e4c19c741728c
MD5 97f7221abe07177658d0ff0651516cf2
BLAKE2b-256 9ff8ddd822525a6cd60647ed27c5d9507384c915433e9ca20e2da1a49ae82aac

See more details on using hashes here.

Provenance

The following attestation bundles were made for heap_mapping-0.1.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