pypdevsbbl.extra.heap module

This file is a python-implementation for a heap.

This file allows for both a min-heap and a max-heap by making use of the MIN_HEAP() and MAX_HEAP() comparator functions listed below.

pypdevsbbl.extra.heap.MIN_HEAP(x, l)

Comparator function, indicating to use a min-heap as the heap.

See also

heap

pypdevsbbl.extra.heap.MAX_HEAP(x, l)

Comparator function, indicating to use a max-heap as the heap.

See also

heap

class pypdevsbbl.extra.heap.heap(comparator=<function <lambda>>)[source]

Bases: object

The heap class is a simple heap implementation.

Note

The comparator function takes two arguments (x and l, as described below) and should return True if x should be swapped with l.

  • x: The value of a node in the heap.
  • l: The root to compare x to.

Warning

You shouldn’t need any other than MIN_HEAP() or MAX_HEAP() for the comparison function. If you do, you’re probably doing strange things and you should consider using another datastructure.

Parameters:comparator (def) – The comparison function. Defaults to MAX_HEAP().
items()[source]

Get a copy of the contents of the heap as a list.

empty()[source]

Check if the heap is empty.

Returns:True if the heap is empty, False otherwise.
_heapify(i)[source]

Fix the heap, making sure the heap condition remains satisfied.

Warning

This function should not be called from an external source.

Parameters:i (int) – Index of the root node to heapify.
insert(node)[source]

Insert a new node into the heap.

Parameters:node (object) – A comparable object to insert.
pop(i=0)[source]

Remove the ith item of the heap.

Parameters:i (int) – The index of the node to pop. Defaults to 0 (the top).
Returns:The item that was removed.
indexOf(node)[source]

Gets the index of a node.

Parameters:node (object) – The node to get the index for.
Returns:The index of that node.
peek(i=0)[source]

Look at the item at location i in the heap.

Parameters:i (int) – The index of the node to peek. Defaults to 0 (the top).
Returns:None if the heap is empty, otherwise the item at index i.
Raises:IndexError – When the index is not found.