Go to the previous, next section.

Heap Operations

A binary heap is a tree with keys and associated values that satisfies the heap property: the key of every node other than the root is greater than the key of its parent node. The main application of binary heaps are priority queues. To load the package, enter the query

| ?- use_module(library(heaps)).

@
Inserts the new Key-Datum pair into the current heap OldHeap producing the new heap NewHeap. Example:
| ?- add_to_heap(t(0,[],t),3,678,N).

N = t(1,[],t(3,678,t,t)) ? 

yes

@
Returns the Key-Datum pair in OldHeap with the smallest Key, and also a NewHeap which is the OldHeap with that pair deleted. Example:
get_from_heap(t(1,[],t(1,543,t,t)),K,D,N).

D = 543,
K = 1,
N = t(0,[1],t) ? 

yes

@
Size is the number of elements in the heap Heap.

@
Returns the current set of Key-Datum pairs in the Heap as a keysorted List.

@
Takes a list List of Key-Datum pairs and forms them into a heap Heap. Example:
| ?- list_to_heap([1-34,2-345,5-678],H).

H = t(3,[],t(1,34,t(2,345,t,t),t(5,678,t,t))) ? 

yes

@
Returns the Key-Datum pair at the top of the heap Heap without removing it. Fails if the heap is empty.

@
Returns the smallest (Key1-Datum1) and second smallest (Key2-Datum2) pairs in the Heap, without deleting them. It fails if the heap does not have at least two elements.

Go to the previous, next section.