Skip to main content

Sophisticate Heap

Project description

heep

PyPI version License: MIT

This library was created to simplify the concept of a heap by showing that it is simply an array that is treated as a complete binary tree with some additional properties such as maximum and minimum binary heap that help us solve some programming issues such as array sorting and priority queue this library is specially designed for beginners in data structures

Installation

You can install heep via pip:

pip install heep

Usage

For Min Binary Heap

from heep import minBinaryHeap


x = minBinaryHeap([5, 4, 6, 2, 8, 1, 3, 7, 9])
print(x)

Output

[1, 2, 3, 4, 8, 6, 5, 7, 9]

You Can Visualize The Heap With All Details (As A Complete Binary Tree)

from heep import minBinaryHeap


x = minBinaryHeap([5, 4, 6, 2, 8, 1, 3, 7, 9], detail=True)
print(x)

Output

╒════════════════╤══════════════╤═══════════════╕
│   Current node  Left child    Right child   │
╞════════════════╪══════════════╪═══════════════╡
│              1  2             3             │
├────────────────┼──────────────┼───────────────┤
│              2  4             8             │
├────────────────┼──────────────┼───────────────┤
│              3  6             5             │
├────────────────┼──────────────┼───────────────┤
│              4  7             9             │
├────────────────┼──────────────┼───────────────┤
│              8  None          None          │
├────────────────┼──────────────┼───────────────┤
│              6  None          None          │
├────────────────┼──────────────┼───────────────┤
│              5  None          None          │
├────────────────┼──────────────┼───────────────┤
│              7  None          None          │
├────────────────┼──────────────┼───────────────┤
│              9  None          None          │
╘════════════════╧══════════════╧═══════════════╛

You Can Add A New Value To The Heap

from heep import minBinaryHeap


x = minBinaryHeap([5, 4, 6, 2, 8, 1, 3, 7, 9], detail=True)
print(x)
x.add(0)
print(x)

Output

╒════════════════╤══════════════╤═══════════════╕
│   Current node  Left child    Right child   │
╞════════════════╪══════════════╪═══════════════╡
│              1  2             3             │
├────────────────┼──────────────┼───────────────┤
│              2  4             8             │
├────────────────┼──────────────┼───────────────┤
│              3  6             5             │
├────────────────┼──────────────┼───────────────┤
│              4  7             9             │
├────────────────┼──────────────┼───────────────┤
│              8  None          None          │
├────────────────┼──────────────┼───────────────┤
│              6  None          None          │
├────────────────┼──────────────┼───────────────┤
│              5  None          None          │
├────────────────┼──────────────┼───────────────┤
│              7  None          None          │
├────────────────┼──────────────┼───────────────┤
│              9  None          None          │
╘════════════════╧══════════════╧═══════════════╛
╒════════════════╤══════════════╤═══════════════╕
│   Current node  Left child    Right child   │
╞════════════════╪══════════════╪═══════════════╡
│              0  1             3             │
├────────────────┼──────────────┼───────────────┤
│              1  4             2             │
├────────────────┼──────────────┼───────────────┤
│              3  6             5             │
├────────────────┼──────────────┼───────────────┤
│              4  7             9             │
├────────────────┼──────────────┼───────────────┤
│              2  8             None          │
├────────────────┼──────────────┼───────────────┤
│              6  None          None          │
├────────────────┼──────────────┼───────────────┤
│              5  None          None          │
├────────────────┼──────────────┼───────────────┤
│              7  None          None          │
├────────────────┼──────────────┼───────────────┤
│              9  None          None          │
├────────────────┼──────────────┼───────────────┤
│              8  None          None          │
╘════════════════╧══════════════╧═══════════════╛

You Can Get The Min Value In The Heap

from heep import minBinaryHeap


x = minBinaryHeap([5, 4, 6, 2, 8, 1, 3, 7, 9], detail=True)
print(x.get_min())

Output

1

You Can Extract The Min Value In The Heap (Get The Min Value And Delete It)

from heep import minBinaryHeap


x = minBinaryHeap([5, 4, 6, 2, 8, 1, 3, 7, 9], detail=True)
print(x)
print(f"Extracted Value: {x.extract_min()}")
print(x)

Output

╒════════════════╤══════════════╤═══════════════╕
│   Current node  Left child    Right child   │
╞════════════════╪══════════════╪═══════════════╡
│              1  2             3             │
├────────────────┼──────────────┼───────────────┤
│              2  4             8             │
├────────────────┼──────────────┼───────────────┤
│              3  6             5             │
├────────────────┼──────────────┼───────────────┤
│              4  7             9             │
├────────────────┼──────────────┼───────────────┤
│              8  None          None          │
├────────────────┼──────────────┼───────────────┤
│              6  None          None          │
├────────────────┼──────────────┼───────────────┤
│              5  None          None          │
├────────────────┼──────────────┼───────────────┤
│              7  None          None          │
├────────────────┼──────────────┼───────────────┤
│              9  None          None          │
╘════════════════╧══════════════╧═══════════════╛
Extracted Value: 1
╒════════════════╤══════════════╤═══════════════╕
│   Current node  Left child    Right child   │
╞════════════════╪══════════════╪═══════════════╡
│              2  4             3             │
├────────────────┼──────────────┼───────────────┤
│              4  7             8             │
├────────────────┼──────────────┼───────────────┤
│              3  6             5             │
├────────────────┼──────────────┼───────────────┤
│              7  9             None          │
├────────────────┼──────────────┼───────────────┤
│              8  None          None          │
├────────────────┼──────────────┼───────────────┤
│              6  None          None          │
├────────────────┼──────────────┼───────────────┤
│              5  None          None          │
├────────────────┼──────────────┼───────────────┤
│              9  None          None          │
╘════════════════╧══════════════╧═══════════════╛

You Can Know How Many Values In The Heap

from heep import minBinaryHeap


x = minBinaryHeap([5, 4, 6, 2, 8, 1, 3, 7, 9], detail=True)
print(len(x))

Output

9

Note

The same methods in maxBinaryHeap with get_max and extract_max.

License

This project is licensed under the MIT LICENSE - see the LICENSE for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

heep-1.0.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

heep-1.0.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file heep-1.0.2.tar.gz.

File metadata

  • Download URL: heep-1.0.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.2

File hashes

Hashes for heep-1.0.2.tar.gz
Algorithm Hash digest
SHA256 26dfb0d3f2912b9063681b6945ca234f1712fbc6a0b14336eb3ae855fed7ed8b
MD5 850b422e0f86d18106a1f71683c238d1
BLAKE2b-256 ecdb4ba480384c3be06a8ed25b7fcaac2b57e317477479b14127a5934e3fa0f9

See more details on using hashes here.

File details

Details for the file heep-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: heep-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.2

File hashes

Hashes for heep-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 66358b721592399501231e55cfa52e51d29d5d7234d0f307e261e7b6ebbb50bd
MD5 00e1926ec571c71a2ce99cbd6bfd4037
BLAKE2b-256 43861fc64f5e19facce67fe0c95b1367c1840be76d2a3db145422cb46f30d91f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page