HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleheaps-0.4.1Haskell2010

Data.Heap

An efficient, asymptotically optimal, implementation of a priority queues extended with support for efficient size, and Data.Foldable

Note: Since many function names (but not the type name) clash with Prelude names, this module is usually imported qualified, e.g.

 import Data.Heap (Heap)
 import qualified Data.Heap as Heap

The implementation of Heap is based on bootstrapped skew binomial heaps as described by:

All time bounds are worst-case.

  • 2 types
  • 35 values
  • Packageheaps-0.4.1
  • Exports37
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceHeap.hs

Heap Type

1 declaration
datadata Heap a
#

A min-heap of values of type a.

Instances8Foldable, Eq, Data, Ord, Read, Show, …

Entry type

1 declaration
datadata Entry p a
#

Explicit priority/payload tuples. Useful to build a priority queue using a Heap, since the payload is ignored in the Eq/Ord instances.

myHeap = fromList [Entry 2 "World", Entry 1 "Hello", Entry 3 "!"]

==> foldMap payload myHeap ≡ "HelloWorld!"

Constructors

Instances9Bifunctor, Functor, Foldable, Traversable, Eq, Data, …

Basic functions

11 declarations
valuenull :: Heap a -> Bool
#

O(1). Is the heap empty?

Example1 expression
null emptyTrue
Example1 expression
null (singleton "hello")False
valuesize :: Heap a -> Int
#

O(1). The number of elements in the heap.

Example3 expressions
size empty0size (singleton "hello")1size (fromList [4,1,2])3
valueminimum :: Heap a -> a
#

O(1). Assumes the argument is a non-null heap.

Example1 expression
minimum (fromList [3,1,2])1
valuedeleteMin :: Heap a -> Heap a
#

O(log n). Delete the minimum key from the heap and return the resulting heap.

Example1 expression
deleteMin (fromList [3,1,2])fromList [2,3]
valueadjustMin :: (a -> a) -> Heap a -> Heap a
#

O(log n). Adjust the minimum key in the heap and return the resulting heap.

Example1 expression
adjustMin (+1) (fromList [1,2,3])fromList [2,2,3]
valueunion :: Heap a -> Heap a -> Heap a
#

O(1). Meld the values from two heaps into one heap.

Example2 expressions
union (fromList [1,3,5]) (fromList [6,4,2])fromList [1,2,6,4,3,5]union (fromList [1,1,1]) (fromList [1,2,1])fromList [1,1,1,2,1,1]
valueuncons :: Heap a -> Maybe (a, Heap a)
#

Provides both O(1) access to the minimum element and O(log n) access to the remainder of the heap. This is the same operation as viewMin

Example1 expression
uncons (fromList [2,1,3])Just (1,fromList [2,3])

Transformations

2 declarations
valuemapMonotonic :: Ord b => (a -> b) -> Heap a -> Heap b
#

O(n). Map a monotone increasing function over the heap. Provides a better constant factor for performance than map, but no checking is performed that the function provided is monotone increasing. Misuse of this function can cause a Heap to violate the heap property.

Example2 expressions
mapMonotonic (+1) (fromList [1,2,3])fromList [2,3,4]mapMonotonic (*2) (fromList [1,2,3])fromList [2,4,6]
valuemap :: Ord b => (a -> b) -> Heap a -> Heap b
#

O(n). Map a function over the heap, returning a new heap ordered appropriately for its fresh contents

Example1 expression
map negate (fromList [3,1,2])fromList [-3,-1,-2]

To/From Lists

6 declarations
valuetoUnsortedList :: Heap a -> [a]
#

O(n). Returns the elements in the heap in some arbitrary, very likely unsorted, order.

Example1 expression
toUnsortedList (fromList [3,1,2])[1,3,2]
fromList . toUnsortedList ≡ id
valuesort :: Ord a => [a] -> [a]
#

O(n log n). Perform a heap sort

valuemapM :: (Monad m, Ord b) => (a -> m b) -> Heap a -> m (Heap b)
#

O(n log n). Traverse the elements of the heap in sorted order and produce a new heap using Monadic side-effects.

valueconcatMap :: (a -> Heap b) -> Heap a -> Heap b
#

O(n). Construct heaps from each element in another heap, and union them together.

Example1 expression
concatMap (\a -> fromList [a,a+1]) (fromList [1,4])fromList [1,4,5,2]

Filtering

10 declarations
valuefilter :: (a -> Bool) -> Heap a -> Heap a
#

O(n). Filter the heap, retaining only values that satisfy the predicate.

Example3 expressions
filter (>'a') (fromList "ab")fromList "b"filter (>'x') (fromList "ab")fromList []filter (<'a') (fromList "ab")fromList []
valuepartition :: (a -> Bool) -> Heap a -> (Heap a, Heap a)
#

O(n). Partition the heap according to a predicate. The first heap contains all elements that satisfy the predicate, the second all elements that fail the predicate. See also split.

Example1 expression
partition (>'a') (fromList "ab")(fromList "b",fromList "a")
valuesplit :: a -> Heap a -> (Heap a, Heap a, Heap a)
#

O(n). Partition the heap into heaps of the elements that are less than, equal to, and greater than a given value.

Example1 expression
split 'h' (fromList "hello")(fromList "e",fromList "h",fromList "llo")
valuebreak :: (a -> Bool) -> Heap a -> (Heap a, Heap a)
#

O(n log n). break applied to a predicate p and a heap xs returns a tuple where the first element is a heap consisting of the longest prefix the least elements of xs that do not satisfy p and the second element is the remainder of the elements in the heap.

Example1 expression
break (\x -> x `mod` 4 == 0) (fromList [3,5,7,12,13,16])(fromList [3,5,7],fromList [12,13,16])

break p is equivalent to span (not . p).

valuespan :: (a -> Bool) -> Heap a -> (Heap a, Heap a)
#

O(n log n). span applied to a predicate p and a heap xs returns a tuple where the first element is a heap consisting of the longest prefix the least elements of xs that satisfy p and the second element is the remainder of the elements in the heap.

Example1 expression
span (\x -> x `mod` 4 == 0) (fromList [4,8,12,14,16])(fromList [4,8,12],fromList [14,16])

span p xs is equivalent to (takeWhile p xs, dropWhile p xs)

valuetake :: Int -> Heap a -> Heap a
#

O(n log n). Return a heap consisting of the least n elements of a given heap.

Example1 expression
take 3 (fromList [10,2,4,1,9,8,2])fromList [1,2,2]
valuedrop :: Int -> Heap a -> Heap a
#

O(n log n). Return a heap consisting of all members of given heap except for the n least elements.

valuesplitAt :: Int -> Heap a -> (Heap a, Heap a)
#

O(n log n). Split a heap into two heaps, the first containing the n least elements, the latter consisting of all members of the heap except for those elements.

valuetakeWhile :: (a -> Bool) -> Heap a -> Heap a
#

O(n log n). takeWhile applied to a predicate p and a heap xs returns a heap consisting of the longest prefix the least elements of xs that satisfy p.

Example1 expression
takeWhile (\x -> x `mod` 4 == 0) (fromList [4,8,12,14,16])fromList [4,8,12]
valuedropWhile :: (a -> Bool) -> Heap a -> Heap a
#

O(n log n). dropWhile p xs returns the suffix of the heap remaining after takeWhile p xs.

Example1 expression
dropWhile (\x -> x `mod` 4 == 0) (fromList [4,8,12,14,16])fromList [14,16]

Grouping

3 declarations
valuegroup :: Heap a -> Heap (Heap a)
#

O(n log n). Group a heap into a heap of heaps, by unioning together duplicates.

Example1 expression
group (fromList "hello")fromList [fromList "e",fromList "h",fromList "ll",fromList "o"]
valuenub :: Heap a -> Heap a
#

O(n log n). Remove duplicate entries from the heap.

Example1 expression
nub (fromList [1,1,2,6,6])fromList [1,2,6]

Intersection

2 declarations
valueintersect :: Heap a -> Heap a -> Heap a
#

O(n log n + m log m). Intersect the values in two heaps, returning the value in the left heap that compares as equal

valueintersectWith :: Ord b => (a -> a -> b) -> Heap a -> Heap a -> Heap b
#

O(n log n + m log m). Intersect the values in two heaps using a function to generate the elements in the right heap.

Duplication

1 declaration
valuereplicate :: Ord a => a -> Int -> Heap a
#

O(log n). Create a heap consisting of multiple copies of the same value.

Example1 expression
replicate 'a' 10fromList "aaaaaaaaaa"