PriorityQueue
PriorityQueue is an implementation of the binary heap data structure
Usage
UFDS(self, arr:list = None, comparator:Callable = lambda x, y: x >= y)
Parameters:
arr : list or None
if None, an empty binary heap is created else. list should contain the elements to be included into the heap
comparator : Callable
A lambda function to relatively order the items in the heap
Methods
Method | Description |
---|---|
peek | Take a look at the root node of the heap |
extract | Returns and remove the root node of the heap |
sort | Returns a sorted list according to the rules of the comparator |
is_empty | Checks if the binary heap is empty |
search | Check if an item is present in the heap |
update | Replace an item in the binary heap |
Example
from jellybeans.structures import PriorityQueue as PQpq = PQ([None, 1, 2, 3, 4 ,5]) # Default is maxheapprint(pq) # [None, 5, 4, 3, 1, 2] (One indexed implementation)