Skip to content

Commit 94ed643

Browse files
Update docs
1 parent 3dfed23 commit 94ed643

File tree

2 files changed

+12
-37
lines changed

2 files changed

+12
-37
lines changed

Doc/library/heapq-binary-tree.png

190 KB
Loading

Doc/library/heapq.rst

+12-37
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ This module provides an implementation of the heap queue algorithm, also known
1717
as the priority queue algorithm.
1818

1919
Min-heaps are binary trees for which every parent node has a value less than
20-
or equal to any of its children.
21-
We refer to this condition as the heap invariant.
20+
or equal to any of its children. We refer to this condition as the heap invariant.
2221

2322
For min-heaps, this implementation uses lists for which
2423
``heap[k] <= heap[2*k+1]`` and ``heap[k] <= heap[2*k+2]`` for all *k* for which
@@ -170,7 +169,7 @@ The module also offers three general purpose functions based on heaps.
170169
*reverse* is a boolean value. If set to ``True``, then the input elements
171170
are merged as if each comparison were reversed. To achieve behavior similar
172171
to ``sorted(itertools.chain(*iterables), reverse=True)``, all iterables must
173-
be sorted from largest to smallest.
172+
be sorted from largest to smallest, like for example, a max-heap.
174173

175174
.. versionchanged:: 3.5
176175
Added the optional *key* and *reverse* parameters.
@@ -306,23 +305,16 @@ entry as removed and add a new entry with the revised priority::
306305
Theory
307306
------
308307

309-
Heaps are arrays for which ``a[k] <= a[2*k+1]`` and ``a[k] <= a[2*k+2]`` for all
310-
*k*, counting elements from 0. For the sake of comparison, non-existing
308+
Min-heaps are arrays for which ``a[k] <= a[2*k+1]`` and ``a[k] <= a[2*k+2]`` for
309+
all *k*, counting elements from 0. For the sake of comparison, non-existing
311310
elements are considered to be infinite. The interesting property of a heap is
312-
that ``a[0]`` is always its smallest element.
311+
that ``a[0]`` is always its smallest element. Max-heaps are the reverse.
313312

314313
The strange invariant above is meant to be an efficient memory representation
315314
for a tournament. The numbers below are *k*, not ``a[k]``::
316315

317-
0
318-
319-
1 2
320-
321-
3 4 5 6
322-
323-
7 8 9 10 11 12 13 14
324-
325-
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
316+
.. figure:: heapq-binary-tree.png
317+
:alt: Example (min-heap) binary tree.
326318

327319
In the tree above, each cell *k* is topping ``2*k+1`` and ``2*k+2``. In a usual
328320
binary tournament we see in sports, each cell is the winner over the two cells
@@ -346,19 +338,18 @@ last 0'th element you extracted. This is especially useful in simulation
346338
contexts, where the tree holds all incoming events, and the "win" condition
347339
means the smallest scheduled time. When an event schedules other events for
348340
execution, they are scheduled into the future, so they can easily go into the
349-
heap. So, a heap is a good structure for implementing schedulers (this is what
350-
I used for my MIDI sequencer :-).
341+
heap. So, a heap is a suitable structure for implementing schedulers.
351342

352343
Various structures for implementing schedulers have been extensively studied,
353344
and heaps are good for this, as they are reasonably speedy, the speed is almost
354345
constant, and the worst case is not much different than the average case.
355346
However, there are other representations which are more efficient overall, yet
356347
the worst cases might be terrible.
357348

358-
Heaps are also very useful in big disk sorts. You most probably all know that a
359-
big sort implies producing "runs" (which are pre-sorted sequences, whose size is
349+
Heaps are also very useful in big disk sorts. A
350+
big sort implies producing "runs" (pre-sorted sequences, whose size is
360351
usually related to the amount of CPU memory), followed by a merging passes for
361-
these runs, which merging is often very cleverly organised [#]_. It is very
352+
these runs, which merging is often very cleverly organised. It is very
362353
important that the initial sort produces the longest runs possible. Tournaments
363354
are a good way to achieve that. If, using all the memory available to hold a
364355
tournament, you replace and percolate items that happen to fit the current run,
@@ -370,20 +361,4 @@ in the current tournament (because the value "wins" over the last output value),
370361
it cannot fit in the heap, so the size of the heap decreases. The freed memory
371362
could be cleverly reused immediately for progressively building a second heap,
372363
which grows at exactly the same rate the first heap is melting. When the first
373-
heap completely vanishes, you switch heaps and start a new run. Clever and
374-
quite effective!
375-
376-
In a word, heaps are useful memory structures to know. I use them in a few
377-
applications, and I think it is good to keep a 'heap' module around. :-)
378-
379-
.. rubric:: Footnotes
380-
381-
.. [#] The disk balancing algorithms which are current, nowadays, are more annoying
382-
than clever, and this is a consequence of the seeking capabilities of the disks.
383-
On devices which cannot seek, like big tape drives, the story was quite
384-
different, and one had to be very clever to ensure (far in advance) that each
385-
tape movement will be the most effective possible (that is, will best
386-
participate at "progressing" the merge). Some tapes were even able to read
387-
backwards, and this was also used to avoid the rewinding time. Believe me, real
388-
good tape sorts were quite spectacular to watch! From all times, sorting has
389-
always been a Great Art! :-)
364+
heap completely vanishes, you switch heaps and start a new run.

0 commit comments

Comments
 (0)