5.92
7 Binary Heaps
(require data/heap) | package: data-lib |
Binary heaps are a simple implementation of priority queues.
Operations on binary heaps are not thread-safe.
Makes a new empty heap using <=? to order elements.
Examples: | |||||||||||||||||||
|
Returns #t if x is a heap, #f otherwise.
Examples: | ||||
|
procedure
h : heap?
Returns the number of elements in the heap.
Examples: | ||||||||
|
Adds each v to the heap.
Examples: | ||||||
|
Adds each element contained in v to the heap, leaving
v unchanged.
Examples: | |||||||||||||||||||||||
|
Returns the least element in the heap h, according to the
heap’s ordering. If the heap is empty, an exception is raised.
Examples: | |||||||||||||
|
procedure
(heap-remove-min! h) → void?
h : heap?
Removes the least element in the heap h. If the heap is
empty, an exception is raised.
Examples: | ||||||||||||||||
|
Builds a heap with the elements from items. The vector is not
modified.
Examples: | ||||||||||||||||
|
procedure
(heap->vector h) → vector?
h : heap?
Returns a vector containing the elements of heap h in the
heap’s order. The heap is not modified.
Examples: | ||||||||
|
Makes a copy of heap h.
Examples: | ||||||||||||||||
|
procedure
(in-heap/consume! heap) → sequence?
heap : heap?
Returns a sequence equivalent to heap, maintaining the heap’s ordering.
The heap is consumed in the process. Equivalent to repeated calling
heap-min, then heap-remove-min!.
Examples: | |||||||||||||||||||
|
Returns a sequence equivalent to heap, maintaining the heap’s ordering.
Equivalent to in-heap/consume! except the heap is copied first.
Examples: | |||||||||||||||||||
|
procedure
(heap-sort! v <=?) → void?
v : (and/c vector? (not/c immutable?)) <=? : (-> any/c any/c any/c)
Sorts vector v using the comparison function <=?.
Examples: | ||||||||
|