A Scheme implementation of a heap "class".
Author: Dakota Kallas
A heap is a binary tree such that the root contains the highest-priority value in the heap. In addition, that property holds recursively for every other node in the heap - in the sense that every node has the highest priority value of all of that nodes descendants. In this program, I implemented the heap as a complete binary tree using the Scheme data-structure given below.
- The empty list is the empty heap.
- Otherwise a heap is a list of exactly three elements. The first element is the root value, the second element is the left sub-tree (also a heap) and the third element is the right sub-tree (also a heap).
(heap-create)
Returns an empty heap.(heap-is-empty? h)
Returns#t
if heaph
is empty and#f
otherwise.(heap-size h)
Returns the size of heaph
(i.e. the number of values in the heap)(heap-insert h f x)
Returns the heap that results from insertingx
into heaph
. Functionf
is a predicate that accepts two elements of the type contained in the heap and returns#t
if the left operand is less than the right.(heap-peek h)
Returns the highest-priority (smallest according to heap-insert predicatef
) value in heaph
.(heap-contains h eq x)
Returns#t
if heaph
contains elementx
as defined by theeq
predicate and#f
otherwise. Functioneq
is a predicate that accepts two elements of the type contained in the heap and returns#t
if the two elements are equal and#f
otherwise.(list->heap xs f)
Returns the heap that results from adding each element in listxs
to an empty heap in the order they occur inxs
usingf
as the heap-insert predicate.
- (list->heap '(3 1 5 9 8 2 7 4 6) <) => (1 (3 (4 (9 () ()) (6 () ())) (8 () ())) (2 (5 () ()) (7 () ())))
- (list->heap '(3 1 5 9 8 2 7 4 6) >) => (9 (8 (6 (1 () ()) (4 () ())) (5 () ())) (7 (2 () ()) (3 () ())))
- (list->heap '(d e q w b r t) (lambda (x y) (string<? (symbol->string x) (symbol->string y)))) => (b (d (w () ()) (e () ())) (q (r () ()) (t () ())))
- (list->heap '((1 3) (5 12 0 5) (a) (#t #t #t #t)) (lambda (x y) (< (length x) (length y)))) => ((a) ((5 12 0 5) ((#t #t #t #t) () ()) ()) ((1 3) () ()))