6.4
7 Binary Heaps
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:
Returns the number of elements in the heap.
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:
Removes the least element in the heap h. If the heap is
empty, an exception is raised.
Examples:
Removes v from the heap h if it exists.
Examples:
| > (define a-heap (make-heap string<=? string=?)) | 
| make-heap: arity mismatch; | 
|  the expected number of arguments does not match the given | 
| number | 
|   expected: 1 | 
|   given: 2 | 
|   arguments...: | 
|    #<procedure:string<=?> | 
|    #<procedure:string=?> | 
| > (heap-add! a-heap "a" "b" "c") | 
|  | 
| > (heap-remove! a-heap "b") | 
|  | 
| > (for/list ([a (in-heap a-heap)]) a) | 
| | '("a" |  |   "bifur" |  |   "bofur" |  |   "bombur" |  |   "c" |  |   "dori" |  |   "dwalin" |  |   "fili" |  |   "fili" |  |   "gloin" |  |   "nori" |  |   "oin" |  |   "ori" |  |   "thorin") | 
 | 
Builds a heap with the elements from items. The vector is not
modified.
Examples:
| > (struct item (val frequency)) | 
|  | 
| | > (define (item<=? x y) |  | (<= (item-frequency x) (item-frequency y))) | 
 | 
|  | 
| | > (define some-sample-items |  | (vector (item #\a 17) (item #\b 12) (item #\c 19))) | 
 | 
|  | 
| > (define a-heap (vector->heap item<=? some-sample-items)) | 
|  | 
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.
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:
Sorts vector v using the comparison function <=?.
Examples:
| > (define terms (vector "batch" "deal" "flock" "good deal" "hatful" "lot")) | 
|  | 
| > (heap-sort! terms string<=?) | 
|  | 
| > terms | 
| '#("batch" "deal" "flock" "good deal" "hatful" "lot") |