Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-110067: Make max heap methods public and add missing ones#130725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
encukou merged 27 commits intopython:mainfromStanFromIreland:add-heapq-max
May 5, 2025
Merged
Changes from1 commit
Commits
Show all changes
27 commits
Select commitHold shift + click to select a range
eccf484
Initial addition
StanFromIrelandMar 1, 2025
beaf915
Add C imp
StanFromIrelandMar 1, 2025
c143ae2
Benedikts suggestions
StanFromIrelandMar 1, 2025
167525d
Benedikts suggestions
StanFromIrelandMar 1, 2025
1b0b6f3
Update Modules/_heapqmodule.c with Benedikts suggestion
StanFromIrelandMar 1, 2025
fc46707
Fix mistake (extra underscores)
StanFromIrelandMar 1, 2025
f4fd94a
Benedikt's requested changes
StanFromIrelandMar 1, 2025
cebbc88
Missed one of Benedikt's requested changes
StanFromIrelandMar 1, 2025
3cde6c6
Benedikts suggestion
StanFromIrelandMar 1, 2025
5d2d387
Benedikts Suggestions
StanFromIrelandMar 2, 2025
a499cd4
Fix doc warnings
StanFromIrelandMar 2, 2025
abe0a95
Improve entries
StanFromIrelandMar 2, 2025
3561206
Address some of Petr's suggestions
StanFromIrelandMar 10, 2025
8fd1a03
Clean up and add missing
StanFromIrelandMar 10, 2025
8ab97c2
Update Doc/library/heapq.rst
StanFromIrelandMar 17, 2025
623cae7
Merge branch 'main' into add-heapq-max
StanFromIrelandApr 25, 2025
81db251
Sort and add missing C implementation
StanFromIrelandMay 2, 2025
38cbf13
Petr's list suggestion
StanFromIrelandMay 2, 2025
b6f4db4
heappushpop_max fixup
StanFromIrelandMay 2, 2025
61c9285
Improve test
StanFromIrelandMay 4, 2025
ebe00dc
Switch to <
StanFromIrelandMay 5, 2025
bc0dd66
Clean up test
StanFromIrelandMay 5, 2025
988b2d3
Reword the docs
encukouMay 5, 2025
6efd70c
Add max-heap variants for the other tests
encukouMay 5, 2025
4ba533b
Apply suggestions from code review
encukouMay 5, 2025
160bc35
Merge pull request #1 from encukou/add-heapq-max
StanFromIrelandMay 5, 2025
742c46c
final touchups
StanFromIrelandMay 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Reword the docs
  • Loading branch information
@encukou
encukou committedMay 5, 2025
commit988b2d38210322c983f339719c66b5523f1bd113
78 changes: 48 additions & 30 deletionsDoc/library/heapq.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,42 +16,56 @@
This module provides an implementation of the heap queue algorithm, also known
as the priority queue algorithm.

Min-heaps (resp. max-heaps) are binary trees for which every parent node
has a value less than (resp. greater than) or equal to any of its children.
We refer to this condition as the heap invariant. Unless stated otherwise,
*heaps* refer to min-heaps.

This implementation uses arrays for which
``heap[k] <= heap[2*k+1]`` and ``heap[k] <= heap[2*k+2]`` for all *k*, counting
elements from zero. For the sake of comparison, non-existing elements are
considered to be infinite. The interesting property of a heap is that its
smallest element is always the root, ``heap[0]``.

The API below differs from textbook heap algorithms in two aspects: (a) We use
zero-based indexing. This makes the relationship between the index for a node
and the indexes for its children slightly less obvious, but is more suitable
since Python uses zero-based indexing. (b) Our pop method returns the smallest
item, not the largest (called a "min heap" in textbooks; a "max heap" is more
common in texts because of its suitability for in-place sorting).
Min-heaps are binary trees for which every parent node has a value less than
or equal to any of its children.
We refer to this condition as the heap invariant.

For min-heaps, this implementation uses lists for which
``heap[k] <= heap[2*k+1]`` and ``heap[k] <= heap[2*k+2]`` for all *k* for which
the compared elements exist. Elements are counted from zero. The interesting
property of a min-heap is that its smallest element is always the root,
``heap[0]``.

These two make it possible to view the heap as a regular Python list without
surprises: ``heap[0]`` is the smallest item, and ``heap.sort()`` maintains the
heap invariant!

Max-heaps satisfy the reverse invariant: every parent node node has a value
*greater* than any of its children. These are implemented as lists for which
``maxheap[2*k+1] <= maxheap[k]`` and ``maxheap[2*k+2] <= maxheap[k]`` for all
*k* for which the compared elements exist.
The root, ``maxheap[0]``, contains the *largest* element;
``heap.sort(reverse=True)`` maintains the max-heap invariant.

The :mod:`!heapq` API differs from textbook heap algorithms in two aspects: (a)
We use zero-based indexing. This makes the relationship between the index for
a node and the indexes for its children slightly less obvious, but is more
suitable since Python uses zero-based indexing. (b) Textbooks often focus on
max-heaps, due to their suitability for in-place sorting. Our implementation
favors min-heaps as they better correspond to Python lists: :meth:`list.sort`
maintains the *min*-heap invariant.

Like :meth:`list.sort`, this implementation uses only the ``<`` operator
for comparisons, for both min-heaps and max-heaps.

In the API below, and in this documentation, the unqalified term *heap*
generally refers to a min-heap.
API for max-heaps is named using a ``_max`` suffix.

To create a heap, use a list initialized to ``[]``, or you can transform a
populated list into a heap via function :func:`heapify`.

The following functions are provided:
The following functions are provided for min-heaps:


.. function:: heappush(heap, item)

Push the value *item* onto the *heap*, maintaining the heap invariant.
Push the value *item* onto the *heap*, maintaining themin-heap invariant.


.. function:: heappop(heap)

Pop and return the smallest item from the *heap*, maintaining the heap
Pop and return the smallest item from the *heap*, maintaining themin-heap
invariant. If the heap is empty, :exc:`IndexError` is raised. To access the
smallest item without popping it, use ``heap[0]``.

Expand All@@ -65,7 +79,7 @@ The following functions are provided:

.. function:: heapify(x)

Transform list *x* into a heap, in-place, in linear time.
Transform list *x* into amin-heap, in-place, in linear time.


.. function:: heapreplace(heap, item)
Expand DownExpand Up@@ -96,23 +110,25 @@ For max-heaps, the following functions are provided:

.. function:: heappush_max(heap, item)

Push the value *item* onto the max-heap *heap*, maintaining the heap invariant.
Push the value *item* onto the max-heap *heap*, maintaining the max-heap
invariant.

.. versionadded:: next


.. function:: heappop_max(heap)

Pop and return the largest item from the max-heap *heap*, maintaining the heap
invariant. If the max-heap is empty, :exc:`IndexError` is raised. To access the
largest item without popping it, use ``heap[0]``.
Pop and return the largest item from the max-heap *heap*, maintaining the
max-heapinvariant. If the max-heap is empty, :exc:`IndexError` is raised.
To access thelargest item without popping it, use ``maxheap[0]``.

.. versionadded:: next


.. function:: heappushpop_max(heap, item)

Push *item* on the max-heap *heap*, then pop and return the largest item from *heap*.
Push *item* on the max-heap *heap*, then pop and return the largest item
from *heap*.
The combined action runs more efficiently than :func:`heappush_max`
followed by a separate call to :func:`heappop_max`.

Expand All@@ -121,11 +137,13 @@ For max-heaps, the following functions are provided:

.. function:: heapreplace_max(heap, item)

Pop and return the largest item from the max-heap *heap* and also push the new *item*.
The max-heap size doesn't change. If the max-heap is empty, :exc:`IndexError` is raised.
Pop and return the largest item from the max-heap *heap* and also push the
new *item*.
The max-heap size doesn't change. If the max-heap is empty,
:exc:`IndexError` is raised.

The value returned may be smaller than the *item* added. Refer to the analogous
function :func:`heapreplace` for detailed usage notes.
The value returned may be smaller than the *item* added. Refer to the
analogousfunction :func:`heapreplace` for detailed usage notes.

.. versionadded:: next

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp