> For the complete documentation index, see [llms.txt](https://www.lisppad.app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.lisppad.app/libraries/lispkit/lispkit-heap.md).

# (lispkit heap)

Library `(lispkit heap)` provides an implementation of a *priority queue* in form of a *binary max heap*. A *max heap* is a tree-based data structure in which for any given node *C*, if *P* is a parent node of *C*, then the value of *P* is greater than or equal to the value of *C*. Heaps as implemented by `(lispkit heap)` are mutable objects.

***

**heap-type-tag** <img src="/files/viHiVHsCY8Wn2TeCgWJj" alt="" data-size="line">

Symbol representing the `heap` type. The `type-for` procedure of library `(lispkit type)` returns this symbol for all heap objects.

**(make-heap&#x20;*****pred\<?*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns a new empty binary max heap with *pred\<?* being the associated ordering function.

**(heap-empty?&#x20;*****hp*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if the heap *hp* is empty, otherwise `#f` is returned.

**(heap-max&#x20;*****hp*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns the largest item in heap *hp*, i.e. the item which is larger than all others according to the comparison function of *hp*. Note, `heap-max` does not remove the largest item as opposed to `heap-delete-max!`. If there are no items on the heap, an error is signaled.

**(heap-add!&#x20;*****hp e1 ...*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Inserts an item into the heap. The same item can be inserted multiple times.

**(heap-delete-max!&#x20;*****hp*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns the largest item in heap *hp*, i.e. the item which is larger than all others according to the comparison function of *hp*, and removes the item from the heap. If there are no items on the heap, an error is signaled.

**(heap-clear!&#x20;*****hp*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Removes all items from *hp*. After this procedure has been executed, the heap is empty.

**(heap-copy&#x20;*****hp*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns a copy of heap *hp*.

**(heap->vector&#x20;*****hp*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns a new vector containing all items of the heap *hp* in descending order. This procedure does not mutate *hp*.

**(heap->list&#x20;*****hp*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns a list containing all items of the heap *hp* in descending order.

**(heap->reversed-list&#x20;*****hp*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns a list containing all items of the heap *hp* in ascending order.

**(list->heap!&#x20;*****hp items*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Inserts all the items from list *items* into heap *hp*.

**(list->heap&#x20;*****items pred\<?*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Creates a new heap for the given ordering predicate *pred\<?* and inserts all the items from list *items* into it. `list-\>heap` returns the new heap.

**(vector->heap&#x20;*****vec pred\<?*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Creates and returns a new heap for the given ordering predicate *pred\<?* and inserts all the items from vector *vec* into it.
