Skip to content

Commit 4d1581a

Browse files
Docs and what's new
1 parent b8b1667 commit 4d1581a

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Doc/library/heapq.rst

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,83 @@ To create a heap, use a list initialized as ``[]``, or transform an existing lis
5050
into a min-heap or max-heap using the :func:`heapify` or :func:`heapify_max`
5151
functions, respectively.
5252

53+
Alternatively, the :class:`MinHeap` and :class:`MaxHeap` classes offer an
54+
object-oriented interface, described next; the module-level functions that they
55+
build upon are documented afterwards.
56+
57+
58+
Heap classes
59+
------------
60+
61+
The :class:`MinHeap` and :class:`MaxHeap` classes wrap the module's heap
62+
operations in an object that owns its own storage. Compared with the functions
63+
below, there is no separate list to pass to every call or to accidentally
64+
break the heap invariant with an unrelated list operation.
65+
66+
.. class:: MinHeap(iterable=None)
67+
MaxHeap(iterable=None)
68+
69+
Create a new heap. If *iterable* is given, the heap is initialized from its
70+
items in linear time; otherwise it starts empty.
71+
72+
A :class:`MinHeap` keeps its smallest item at the top, while a
73+
:class:`MaxHeap` keeps its largest item at the top. The two classes share
74+
the same interface and differ only in that ordering --- that is, in which
75+
item :meth:`~MinHeap.pop` removes and which item the other methods treat as
76+
the top. As with the functions below, only the ``<`` operator is used to
77+
compare items.
78+
79+
The methods documented below belong to both classes; a :class:`MinHeap` is
80+
used in the descriptions, with *MaxHeap* behaving the same way once
81+
"smallest" is read as "largest".
82+
83+
.. method:: push(item)
84+
85+
Push the value *item* onto the heap, maintaining the heap invariant.
86+
87+
.. method:: pop()
88+
89+
Pop and return the smallest item from the heap, maintaining the heap
90+
invariant. If the heap is empty, :exc:`IndexError` is raised.
91+
92+
.. method:: pushpop(item)
93+
94+
Push *item* on the heap, then pop and return the smallest item. The
95+
combined action runs more efficiently than :meth:`push` followed by a
96+
separate call to :meth:`pop`.
97+
98+
.. method:: replace(item)
99+
100+
Pop and return the smallest item from the heap, and also push the new
101+
*item*. The heap size doesn't change. If the heap is empty,
102+
:exc:`IndexError` is raised.
103+
104+
This one step operation is more efficient than a :meth:`pop` followed by
105+
:meth:`push` and can be more appropriate when using a fixed-size heap.
106+
The value returned may be larger than the *item* added; consider using
107+
:meth:`pushpop` instead if that is not desired.
108+
109+
.. method:: nsmallest(n, key=None)
110+
111+
Return a list with the *n* smallest items from the heap, from smallest to
112+
largest, without modifying the heap. Behaves like the module-level
113+
:func:`nsmallest` function applied to the heap's items.
114+
115+
.. method:: nlargest(n, key=None)
116+
117+
Return a list with the *n* largest items from the heap, from largest to
118+
smallest, without modifying the heap. Behaves like the module-level
119+
:func:`nlargest` function applied to the heap's items.
120+
121+
In addition, ``len(heap)`` returns the number of items on the heap, and an
122+
empty heap is falsy, so ``bool(heap)`` is ``False`` when it holds no items.
123+
124+
.. versionadded:: 3.16
125+
126+
127+
Heap functions
128+
--------------
129+
53130
The following functions are provided for min-heaps:
54131

55132

@@ -214,6 +291,15 @@ time::
214291
This is similar to ``sorted(iterable)``, but unlike :func:`sorted`, this
215292
implementation is not stable.
216293

294+
Using the object-oriented interface, the same sort becomes::
295+
296+
>>> def heapsort(iterable):
297+
... heap = MinHeap(iterable)
298+
... return [heap.pop() for _ in range(len(heap))]
299+
...
300+
>>> heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
301+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
302+
217303
Heap elements can be tuples. This is useful for assigning comparison values
218304
(such as task priorities) alongside the main record being tracked::
219305

Doc/whatsnew/3.16.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ gzip
221221
which is passed on to the constructor of the :class:`~gzip.GzipFile` class.
222222
(Contributed by Marin Misur in :gh:`91372`.)
223223

224+
heapq
225+
-----
226+
227+
* Add the :class:`~heapq.MinHeap` and :class:`~heapq.MaxHeap` classes, an
228+
object-oriented interface to the module's heap functions.
229+
(Contributed by Facundo Batista in :gh:`137200`.)
230+
224231
io
225232
--
226233

0 commit comments

Comments
 (0)