Skip to content

gh-137200: Add object-oriented MinHeap and MaxHeap classes to heapq#153821

Open
facundobatista wants to merge 4 commits into
python:mainfrom
facundobatista:heapq-oo
Open

gh-137200: Add object-oriented MinHeap and MaxHeap classes to heapq#153821
facundobatista wants to merge 4 commits into
python:mainfrom
facundobatista:heapq-oo

Conversation

@facundobatista

@facundobatista facundobatista commented Jul 16, 2026

Copy link
Copy Markdown
Member

Add heapq.MinHeap and heapq.MaxHeap, an object-oriented interface to the existing heapq functions.

Both classes wrap a list and expose the heap operations as methods, so you no longer have to pass the list around to the module-level functions:

>>> from heapq import MinHeap
>>> heap = MinHeap([3, 1, 2])
>>> heap.push(0)
>>> heap.pop()
0
>>> len(heap)
3

The classes provide push, pop, pushpop, replace, nsmallest and nlargest, plus __len__, __bool__ and __repr__. MaxHeap behaves as a max-heap, using the *_max variants under the hood.

The implementation is pure Python and delegates to the C-accelerated module functions, so there is no performance regression.

Includes documentation, a What's New entry and tests.

@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33624913 | 📁 Comparing 4d1581a against main (f62050d)

  🔍 Preview build  

3 files changed
± library/datatypes.html
± library/heapq.html
± whatsnew/3.16.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant