Python's heapq can be confusing to beginners.
This package helps beginners better visualize heapq heaps better
Installation
pip install visualize-heapq
Quickstart
import heapq
heap = [5, 4, 3, 2, 1]
heapq.heapify(heap) # heap is now a heapq heap (using the list data type)
print(heap) # [1, 2, 3, 5, 4]
# we now want to visualize our heap
from visualize_heapq import visualize_heapq
visualize_heapq(heap)
# _____1___
# | |
# __2___ 3
# | |
# 5 4
Context
What is a heap?
- a heap is a binary tree
- a binary tree where every parent <= its children
- more specifically, this is known as a min heap
- max heaps are heaps where every parent >= its children, but max heaps are not relevant here
Where heapq comes in
heapqis a built-in Python module- it provides us with functionality to use heaps (min heaps)
- these heaps can act like priority queues
import heapq
priority_queue: list[int] = [] # heaps are stored as lists in heapq
heapq.heappush(priority_queue, 5)
heapq.heappush(priority_queue, 10)
heapq.heappush(priority_queue, 2)
heapq.heappush(priority_queue, 6)
heapq.heappush(priority_queue, 1)
print(priority_queue) # [1, 2, 5, 10, 6]
print(heapq.heappop(priority_queue)) # 1
print(heapq.heappop(priority_queue)) # 2
print(heapq.heappop(priority_queue)) # 5
print(heapq.heappop(priority_queue)) # 6
print(heapq.heappop(priority_queue)) # 10
The weird thing about heaps in heapq
- there is no Heap object.
- heaps are stored as normal lists in
heapq - which can be confusing for a beginner
As such, this package aims to make things more intuitive by allowing users to print and visualize a heapq heap (which is actually a list) directly.
Documentation Link: https://docs.python.org/3/library/heapq.html
Release files for visualize-heapq 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| visualize_heapq-0.0.3.tar.gz | 3.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| visualize_heapq-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.5 kB
Release files / visualize_heapq-0.0.3.tar.gz
| Download URL | visualize_heapq-0.0.3.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7ee64dfb59bce8bc799a4e1661c20da8897b3d1cb4c2c04ce0d0dc0dfd95f135
|
|
BLAKE2b-256 checksum How to use checksums |
da982345e556aee70f9343694bb66698241722e6fb394c53fe364eefefb9f46f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.3
|
Release files / visualize_heapq-0.0.3-py3-none-any.whl
| Download URL | visualize_heapq-0.0.3-py3-none-any.whl |
|---|---|
| Size | 3.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
36da68e9cf26113ba9c11dca811fe1b12e6d6f1c75d13b1f677bcafe1815c566
|
|
BLAKE2b-256 checksum How to use checksums |
1a0b9be00be437a721f59b3a8ac8d918c969d0b5dced9a1f08e9243720228241
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.3
|