Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions Lib/heapq.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ def _heappop_max(heap):
return returnitem
return lastelt

def heappush_pop(heap, item):
"""Maxheap version of a heappush."""
heap.append(item)
_siftdown_max(heap, 0, len(heap)-1)

def _heapreplace_max(heap, item):
"""Maxheap version of a heappop followed by a heappush."""
returnitem = heap[0] # raises appropriate IndexError if heap is empty
Expand Down
9 changes: 8 additions & 1 deletion Lib/test/test_heapq.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
# _heapq is imported, so check them there
func_names = ['heapify', 'heappop', 'heappush', 'heappushpop', 'heapreplace',
'_heappop_max', '_heapreplace_max', '_heapify_max']
'_heappop_max', '_heappush_max', '_heapreplace_max', '_heapify_max']

class TestModules(TestCase):
def test_py_functions(self):
Expand Down Expand Up @@ -160,6 +160,13 @@ def test_heappop_max(self):
self.assertEqual(self.module._heappop_max(h), 3)
self.assertEqual(self.module._heappop_max(h), 2)

def test_heappush_max(self):
h = [3, 2]
self.module._heappush_max(h, 3)
self.assertEqual(h[0], 3)
self.module._heappush_max(h, 5)
self.assertEqual(h[0], 5)

def test_heapsort(self):
# Exercise everything with repeated heapsort checks
for trial in range(100):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Heappush operation is missing for max heap version in heapq