A package that visualizes heapq heaps
Project description
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
heapq
is 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
visualize_heapq-0.0.3.tar.gz
(3.5 kB
view details)
Built Distribution
File details
Details for the file visualize_heapq-0.0.3.tar.gz
.
File metadata
- Download URL: visualize_heapq-0.0.3.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ee64dfb59bce8bc799a4e1661c20da8897b3d1cb4c2c04ce0d0dc0dfd95f135 |
|
MD5 | 6ac0a5e17d08ccb04b807d459451a265 |
|
BLAKE2b-256 | da982345e556aee70f9343694bb66698241722e6fb394c53fe364eefefb9f46f |
File details
Details for the file visualize_heapq-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: visualize_heapq-0.0.3-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36da68e9cf26113ba9c11dca811fe1b12e6d6f1c75d13b1f677bcafe1815c566 |
|
MD5 | cc89c49cdea2037b777493f6349514de |
|
BLAKE2b-256 | 1a0b9be00be437a721f59b3a8ac8d918c969d0b5dced9a1f08e9243720228241 |