Support max heaps in heapq module
Hi, I understand that the default implementation of heapq.heapify is to create a min-heap. The logic for max-heapify has already been implemented inside _heapify_max and along with that for corresponding push and pop operations. But they are not exposed to the user and hence not mentioned in any of the docs. I am unable to find any explanation as to why this is so. Current methods of implementing max-heaps (according to the SO question mentioned below) involves inverting the signs of numeric values and other workarounds. Therefore, I would like to propose officially supporting max-heaps as a part of heapq module. SO Q&A regarding the issue : https://stackoverflow.com/questions/2501457/what-do-i-use-for-a-max-heap-imp... Thank you -- Abhai Kollara
It is easy to have a wrapper class around the heapq functions so that you
can use
an arbitrary comparison on the heap.
I have an answer with such an snippet, that got some
activity today - maybe exactly due to your questioning:
https://stackoverflow.com/questions/8875706/heapq-with-custom-compare-predic...
On Fri, 20 Mar 2020 at 14:30, Abhai Kollara
Hi, I understand that the default implementation of heapq.heapify is to create a min-heap. The logic for max-heapify has already been implemented inside _heapify_max and along with that for corresponding push and pop operations. But they are not exposed to the user and hence not mentioned in any of the docs. I am unable to find any explanation as to why this is so.
Current methods of implementing max-heaps (according to the SO question mentioned below) involves inverting the signs of numeric values and other workarounds. Therefore, I would like to propose officially supporting max-heaps as a part of heapq module.
SO Q&A regarding the issue : https://stackoverflow.com/questions/2501457/what-do-i-use-for-a-max-heap-imp...
Thank you -- Abhai Kollara _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/E33N24... Code of Conduct: http://python.org/psf/codeofconduct/
participants (2)
-
Abhai Kollara
-
Joao S. O. Bueno