LeftistHeap

A LeftistHeap is a persistent (immutable) priority queue. The first operation is O(1), and with and withoutFirst are O(log(n)). The latter two also produce a new LeftistHeap without modifying the original.

A LeftistHeap is either a leftistLeaf having no elements, or a LeftistInternal containing the heap's minimum (the values of the heap must be Comparable), a left subtree, and a right subtree. The rank of a heap is the length of the shortest path to a leaf. The rank of a heap's right subtree is always less than or equal to the rank of the heap's left subtree. Therefore, the shortest path to a leaf can always be found along the right spine. In fact, the rank can be defined as zero for a leaf, or the right subtree's rank + 1.

Author

Mark van Gulik

Parameters

rank

The rank of the heap, which is the number of values along the right spine.

size

The number of values in the heap.

Properties

Link copied to clipboard
abstract val first: Value

The minimum value of this heap.

Link copied to clipboard

Whether this heap is empty.

Link copied to clipboard
val rank: Int
Link copied to clipboard
val size: Int
Link copied to clipboard

A heap having all but its minimum value.

Functions

Link copied to clipboard
abstract fun merge(another: LeftistHeap<Value>): LeftistHeap<Value>

Merge this heap with another.

Link copied to clipboard
fun toList(): List<Value>

Collect the heap's elements in a List in sorted order.

Link copied to clipboard
abstract fun with(newValue: Value): LeftistHeap<Value>

A heap with one more element.

Link copied to clipboard
abstract fun without(value: Value): LeftistHeap<Value>

Create a heap with a specific element removed. This is particularly efficient (O(log(N))) when the element is the minimal element, although withoutFirst is the preferred form.