https://github.com/mshelll/maxheap
heapq library tweaked to a maxheap
https://github.com/mshelll/maxheap
competitive-programming library maxheap python3
Last synced: 6 months ago
JSON representation
heapq library tweaked to a maxheap
- Host: GitHub
- URL: https://github.com/mshelll/maxheap
- Owner: mshelll
- License: mit
- Created: 2021-12-17T07:17:51.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-05-24T06:54:15.000Z (about 1 year ago)
- Last Synced: 2025-09-22T16:53:46.781Z (9 months ago)
- Topics: competitive-programming, library, maxheap, python3
- Language: Python
- Homepage:
- Size: 40 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# maxheap
Existing [heapq](https://docs.python.org/3/library/heapq.html) library can be used as a maxheap by negating the values passed.
We need to negate back while retrieving from the heap as well. This can result in
a cumbersome interface.
Maxpheap is a implementation similar to heapq that lets you push and pop to the heap.
In a maxheap largest element has the highest priority.

# Dependencies
Python > 3.6.0
# Install
```sh
pip3 install maxheapq
```
## Verify
```sh
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> from maxheap import maxheap
```
# Usage
```sh
heap = [] # creates an empty heap
heappush(heap, item) # pushes a new item on the heap
item = heappop(heap) # pops the largest item from the heap
item = heap[0] # largest item on the heap without popping it
heapify(x) # transforms list into a max heap, in-place, in linear time
item = heapreplace(heap, item) # pops and returns largest item, and adds
# new item; the heap size is unchanged
```