Module dalpy.heaps
This module holds classes and functions related to heaps and priority queues.
This module contains PriorityQueue, an implementation of a priority queue, and PriorityQueueUnderflowError, an error
raised by PriorityQueue. This module also contains the function build_min_heap() which modifies the elements of an
Array so that they construct a min heap.
Examples
Initializing, adding, and removing elements from a PriorityQueue:
q = PriorityQueue()
q.insert('a', 2)
q.insert('b', 1)
q.extract_min()
The following code will raise a PriorityQueueUnderflowError because the second extract min is done on an empty
PriorityQueue:
q.extract_min()
q.extract_min()
Functions
def build_min_heap(arr)-
Modifies an
Arrayso that its elements make up a min heap.This method does not return a copy of the provided
Arraywhose elements make up a heap, it modifies it in place. Furthermore, all the elements (starting from index 0) are in a min heap. This method runs inO(n)time wherenis the length of the inputArray.Args
arr- The input
Array. Its elements should be comparable with<,>=, etc.
Raises
TypeError- If
arris not anArray'.
Classes
class PriorityQueue-
This class represents a minimum priority queue.
One may assume that this
PriorityQueuehas no maximum capacity.Examples
To initialize a
PriorityQueue:q = PriorityQueue()To add elements to
q:q.insert('a', 2) q.insert('b', 1)To remove and return the minimum priority element of
q(in this casex = 'b'):x = q.extract_min()To see the minimum priority element of
q(in this casey = 'a'):y = q.front()To decrease the priority of an element in
q:q.decrease_key('a', 0)Initializes an empty
PriorityQueueinO(1)time.Methods
def decrease_key(self, element, new_priority)-
Decreases the priority of an element in this
PriorityQueue.This operation runs in
O(log(n))time wherenis the size of thisPriorityQueue.Args
element- The element whose priority is being updated.
new_priority- The new priority of
element. It should be<=its existing priority.
Raises
ValueError- If
elementis not in thisPriorityQueueornew_priorityis greater than the existing priority ofelement.
def extract_min(self)-
Removes the minimum priority element of this
PriorityQueue.This operation runs in
O(log(n))time wherenis the size of thisPriorityQueue.Returns
The element with the minimum priority in this
PriorityQueue.Raises
PriorityQueueUnderflowError- If this
PriorityQueueis empty.
def insert(self, element, priority)-
Inserts an element into the
PriorityQueuewith an associated priority.This operation runs in
O(log(n))time wherenis the size of thisQueue.Args
element- An element to add to this
PriorityQueue. This can be of any type. priority- The integer priority
elementshould have in thisPriorityQueue.
def is_empty(self)-
Returns whether this
PriorityQueueis empty inO(1)time w/r/t the size of thisPriorityQueue.Returns
Trueif thisPriorityQueueis empty,Falseotherwise. def minimum(self)-
Gets the minimum priority element of this
PriorityQueue.This operation runs in
O(1)time with respect to the size of thisPriorityQueue.Returns
The element with the minimum priority in this
PriorityQueue.Raises
PriorityQueueUnderflowError- If this
PriorityQueueis empty.
def size(self)-
Returns the size of this
PriorityQueueinO(1)time w/r/t the size of thisPriorityQueue.Returns
The integer number of elements in this
PriorityQueue.
class PriorityQueueUnderflowError (operation)-
This class is used by
PriorityQueueto raise errors for operations done on an emptyPriorityQueue.Initializes a
PriorityQueueUnderflowErrorthat will be raised associated with aPriorityQueueoperation.Args
operation- a string specifying the operation to raise an error on
Ancestors
- builtins.Exception
- builtins.BaseException