From 0861080da77667a667c94d552fd47ed3d7337241 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Wed, 7 May 2025 21:45:16 +0100 Subject: [PATCH 1/3] min/max heaps --- Doc/library/heapq.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/library/heapq.rst b/Doc/library/heapq.rst index 922ba0c8aa4214..6c0acff93dd1ef 100644 --- a/Doc/library/heapq.rst +++ b/Doc/library/heapq.rst @@ -170,7 +170,7 @@ The module also offers three general purpose functions based on heaps. *reverse* is a boolean value. If set to ``True``, then the input elements are merged as if each comparison were reversed. To achieve behavior similar to ``sorted(itertools.chain(*iterables), reverse=True)``, all iterables must - be sorted from largest to smallest. + be sorted from largest to smallest, like for example, a max-heap. .. versionchanged:: 3.5 Added the optional *key* and *reverse* parameters. @@ -306,12 +306,12 @@ entry as removed and add a new entry with the revised priority:: Theory ------ -Heaps are arrays for which ``a[k] <= a[2*k+1]`` and ``a[k] <= a[2*k+2]`` for all +Min-heaps are arrays for which ``a[k] <= a[2*k+1]`` and ``a[k] <= a[2*k+2]`` for all *k*, counting elements from 0. For the sake of comparison, non-existing -elements are considered to be infinite. The interesting property of a heap is -that ``a[0]`` is always its smallest element. +elements are considered to be infinite. The interesting property of a min-heap is +that ``a[0]`` is always its smallest element. Max-heaps satisfy the reverse invariant. -The strange invariant above is meant to be an efficient memory representation +The strange min-heap invariant is meant to be an efficient memory representation for a tournament. The numbers below are *k*, not ``a[k]``:: 0 From ede66b983f6588ed96adf490926388cdddb7ff02 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Date: Thu, 8 May 2025 15:42:45 +0100 Subject: [PATCH 2/3] Fixup --- Doc/library/heapq.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/heapq.rst b/Doc/library/heapq.rst index 6c0acff93dd1ef..c547746cc1101a 100644 --- a/Doc/library/heapq.rst +++ b/Doc/library/heapq.rst @@ -170,7 +170,7 @@ The module also offers three general purpose functions based on heaps. *reverse* is a boolean value. If set to ``True``, then the input elements are merged as if each comparison were reversed. To achieve behavior similar to ``sorted(itertools.chain(*iterables), reverse=True)``, all iterables must - be sorted from largest to smallest, like for example, a max-heap. + be sorted from largest to smallest. .. versionchanged:: 3.5 Added the optional *key* and *reverse* parameters. From 01348b4c9a118902175b824c263223309fe0f280 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Sun, 13 Jul 2025 07:02:49 +0100 Subject: [PATCH 3/3] Fixup merge --- Doc/library/heapq.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/library/heapq.rst b/Doc/library/heapq.rst index 0e271a0836474e..52d96600c087c0 100644 --- a/Doc/library/heapq.rst +++ b/Doc/library/heapq.rst @@ -312,8 +312,7 @@ elements are considered to be infinite. The interesting property of a min-heap that ``a[0]`` is always its smallest element. Max-heaps satisfy the reverse invariant. The strange min-heap invariant is meant to be an efficient memory representation -for a tournament. The numbers below are *k*, not ``a[k]``:: - +for a tournament. The numbers below are *k*, not ``a[k]``: .. figure:: heapq-binary-tree.svg :class: invert-in-dark-mode