TSFoster/elm-heap/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Heap

Data structure for heaps.

This package exposes a data structure to implement heaps/priority queues/fast in-place sorting.

The heap is implemented as a pairing heap, as it is simple but fast, and has been shown to work well in real-world situations.

Definition

type Heap a

Heaps are exposed as an opaque union type.

Creating heaps

empty : Heap comparable

An empty heap for any comparable type (ints, floats, chars, strings, lists, or tuples).

singleton : comparable -> Heap comparable

A heap containing one comparable value (ints, floats, chars, strings, lists, or tuples).

Heap.singleton (3, 4)
isEmpty : Heap a -> Bool

True if the Heap is empty, otherwise False.

>>> Heap.isEmpty Heap.empty
True


>>> Heap.isEmpty (Heap.singleton 3)
False

Inserting values

push : a -> Heap a -> Heap a

Add a value to a heap.

>>> Heap.fromList [ 1, 6, 7 ]
...     |> Heap.push 4
...     |> Heap.peek
Just 1


>>> Heap.fromList [ 5, 6, 7 ]
...     |> Heap.push 4
...     |> Heap.peek
Just 4
merge : Heap a -> Heap a -> Heap a

Merge the second heap into the first heap.

Note This function assumes that both heaps are sorted using the same method. Strictly speaking, the merged heap has the same sorting method as the first argument.

>>> Heap.isEmpty (Heap.merge Heap.empty Heap.empty)
True


>>> Heap.merge (Heap.fromList [ 2, 4, 6, 7 ]) (Heap.fromList [ 5, 7, 9, 3 ])
...     |> Heap.size
8

Inspecting heaps

isEmpty : Heap a -> Bool

True if the Heap is empty, otherwise False.

>>> Heap.isEmpty Heap.empty
True


>>> Heap.isEmpty (Heap.singleton 3)
False
size : Heap a -> Int

Number of elements in heap.

>>> Heap.size Heap.empty
0


>>> Heap.size (Heap.fromList [ 1, 2, 3, 4, 5, 6, 7, 8 ])
8
peek : Heap a -> Maybe a

Look at smallest value in heap without applying any transformations.

>>> Heap.peek Heap.empty
Nothing


>>> Heap.peek (Heap.fromList [ 3, 56, 8, 367, 0, 4 ])
Just 0
pop : Heap a -> Maybe ( a, Heap a )

Try to remove the smallest value from the heap, returning the value and the new heap. If the heap is empty, return Nothing.

>>> Heap.pop Heap.empty
Nothing


>>> Heap.fromList [ 3, 5, 7, 7, 2, 9 ]
...     |> Heap.pop
...     |> Maybe.map (Tuple.mapSecond Heap.size)
Just (2, 5)
popBlind : Heap a -> Maybe (Heap a)

Try to remove the smallest value from the heap, returning just the new heap. If the heap is empty, return Nothing.

>>> Heap.popBlind Heap.empty
Nothing


>>> Heap.singleton 3
...     |> Heap.popBlind
...     |> Maybe.map Heap.size
Just 0
compareHeaps : Heap a -> Heap a -> Order

Compare two heaps (using compare method of first heap):

  • An empty heap is less than a non-empty heap.

  • If one heap's min-value is less than another, the heap is less than the other

  • If two heaps share the same min-value, remove the min-values are compare the resulting heaps

    Heap.compareHeaps Heap.empty Heap.empty EQ

    Heap.compareHeaps Heap.empty (Heap.singleton 3) LT

    Heap.compareHeaps (Heap.fromList [ 1, 2, 3, 4 ]) (Heap.fromList [ 1, 2, 3 ]) GT

Bulk operations

fromList : List comparable -> Heap comparable

A heap containing all values in the list of comparable types.

Heap.fromList []


Heap.fromList [ 8, 3, 8, 3, 6, 67, 23 ]
toList : Heap a -> List a

Get all values from the heap, in order.

>>> Heap.toList (Heap.fromList [ 9, 3, 6, 4, 1, 2, 8, 5, 7 ])
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
toListReverse : Heap a -> List a

Get all values from the heap, in reverse order.

>>> Heap.toListReverse (Heap.fromList [ 9, 3, 6, 4, 1, 2, 8, 5, 7 ])
[ 9, 8, 7, 6, 5, 4, 3, 2, 1 ]

Non-comparable elements

emptySortedBy : (a -> comparable) -> Heap a

An empty heap for any type, given a function to make a comprable value.

Heap.emptySortedBy .surname
singletonSortedBy : (a -> comparable) -> a -> Heap a

A heap containing one value, given a function to make a comparable value.

Heap.singletonSortedBy .surname { firstname = "Buzz", surname = "Aldrin" }
emptySortedWith : (a -> a -> Order) -> Heap a

An empty heap for any type, given a function to compare values.

Heap.emptySortedWith (\a b -> compare (List.maximum a) (List.maximum b))
singletonSortedWith : (a -> a -> Order) -> a -> Heap a

A heap containing one value, given a function to compare values.

Heap.SingletonSortedWith [ 1, 2, 3, 56 ]
fromListSortedBy : (a -> comparable) -> List a -> Heap a

A heap containing all values in the list, given a function to make a comparable value.

Heap.fromListSortedBy .surname
    [ { firstname = "Buzz", surname = "Aldrin" }
    , { firstname = "Norman", surname = "Bates" }
    , { firstname = "Bruce", surname = "Campbell" }
    ]
fromListSortedWith : (a -> a -> Order) -> List a -> Heap a

A heap containing all values in the list, given a function to compare values.

Heap.fromListSortedWith (\a b -> compare (List.maximum a) (List.maximum b))
    [ [ 1, 999 ]
    , [ 6, 4, 3, 8, 9, 347, 34, 132, 546 ]
    ]

Running times

  • peek: Θ(1)
  • pop: O(log n) (amortized)
  • push: Θ(1)
  • size: Θ(1)
  • merge: Θ(1)