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.

Supported by

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