Skip to main content

Heap implementation in python because heapq sucks!

Project description

Heap

A Python binary heap supporting both min-heap and max-heap behavior. Each node (PQNode) must be unique and hashable. The heap allows fast insert, update, remove, and pop operations.


Features

  • Min-heap or max-heap (kind="min" or "max")
  • Nodes store a key (used for ordering) and value (actual data)
  • Duplicate nodes trigger a warning but are allowed
  • O(log n) insert, update, pop, and remove

Installation

Copy the Heap class and PQNode protocol into your project. No external dependencies required.


Usage

from heap import Heap, PQNode
import warnings

class Node:
    def __init__(self, key, value):
        self.key = key       # Heap ordering
        self.value = value   # Unique data

    # Ensure uniqueness and hashability via `value`
    def __hash__(self):
        return hash(self.value)

    def __eq__(self, other):
        return isinstance(other, Node) and self.value == other.value

# Create a min-heap
heap = Heap(kind="min")

# Insert nodes
n1 = Node(5, "apple")
n2 = Node(2, "banana")
n3 = Node(8, "cherry")

heap.insert(n1)
heap.insert(n2)
heap.insert(n3)

# Pop the top element (smallest key)
top = heap.pop()
print(top.value, top.key)  # Output: banana 2

# Update a node's key
heap.update(n3, 1)
print(heap.pop().value, heap.pop().key)  # Output: cherry 1

# Remove a node
heap.remove(n1)

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

python_heap-0.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

python_heap-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_heap-0.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for python_heap-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0ac5596821db6f37dfb0098453c1c48b642134a5f7efeaa4a50a79d797a0004e
MD5 5bef359574de1145b133f42f4b961e77
BLAKE2b-256 a2ecd9a27368454d0cee36f6c5e121650b33ec44281977b04aa4d5fc5fb9ae1f

See more details on using hashes here.

File details

Details for the file python_heap-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_heap-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for python_heap-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0632d1b609d6728def8f3257cc0642841cb3cb84248d21b9f917430d2c71d57
MD5 57b719f5a5cf037d452bab7b68b076bd
BLAKE2b-256 27319f77d7b1c4b03ef30ff76372211ba822e84a09e91ea195d8cc426a11afba

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