Example of Min-max heap, author(s) Aishvora/HerrAdams at Wikipedia (CC BY-SA 3.0)

Code Snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{-# LANGUAGE Safe #-}

--------------------------------------------------------------------------------

module Pairing
  ( MinHeap
  , MaxHeap
    --
  , minroot
  , mincons
  , mindrop
  , minheap
  , minlist
    --
  , maxroot
  , maxcons
  , maxdrop
  , maxheap
  , maxlist
  )
where

--------------------------------------------------------------------------------

import           Prelude    hiding
  ( drop
  )

import           Data.List
  ( foldl'
  )
import           Data.Maybe
  ( fromMaybe
  )

--------------------------------------------------------------------------------

data Tree a
  = Leaf
  | Node a [Tree a]

type Heap = Tree

newtype MinHeap a = I (Heap a)
newtype MaxHeap a = D (Heap a)

--------------------------------------------------------------------------------

minroot
  :: MinHeap a
  -> Maybe   a
minroot (I h) =
  root h

mincons
  :: Ord     a
  =>         a
  -> MinHeap a
  -> MinHeap a
mincons x (I h) =
  I $ cons LT x h

mindrop
  ::        Ord     a
  =>        MinHeap a
  -> Maybe (MinHeap a)
mindrop (I h) =
  I <$> drop LT h

minheap
  :: Ord     a
  =>        [a]
  -> MinHeap a
minheap =
  I . heap LT

minlist
  :: Ord     a
  => MinHeap a
  ->        [a]
minlist (I h) =
  list LT h

--------------------------------------------------------------------------------

maxroot
  :: MaxHeap a
  -> Maybe   a
maxroot (D h) =
  root h

maxcons
  :: Ord     a
  =>         a
  -> MaxHeap a
  -> MaxHeap a
maxcons x (D h) =
  D $ cons GT x h

maxdrop
  ::        Ord     a
  =>        MaxHeap a
  -> Maybe (MaxHeap a)
maxdrop (D h) =
  D <$> drop GT h

maxheap
  :: Ord     a
  =>        [a]
  -> MaxHeap a
maxheap =
  D . heap GT

maxlist
  :: Ord     a
  => MaxHeap a
  ->        [a]
maxlist (D h) =
  list GT h

--------------------------------------------------------------------------------

-- HELPERS

--------------------------------------------------------------------------------

-- Pairing heap
--
-- https://en.wikipedia.org/wiki/Pairing_heap

root
  :: Heap  a
  -> Maybe a
root  Leaf       = Nothing
root (Node a _ ) = Just  a

cons
  :: Ord  a
  => Ordering
  ->      a
  -> Heap a
  -> Heap a
cons o a =
  meld o n
  where
    n = Node a []

drop
  ::        Ord  a
  =>        Ordering
  ->        Heap a
  -> Maybe (Heap a)
drop _  Leaf        = Nothing
drop o (Node _ hs ) = Just ph
  where
    ph = pair o hs

--------------------------------------------------------------------------------

heap
  :: Ord  a
  => Ordering
  ->     [a]
  -> Heap a
heap o =
  foldl' f Leaf
  where
    f = flip $ cons o

list
  :: Ord  a
  => Ordering
  -> Heap a
  ->     [a]
list o =
  fromMaybe [] . app
  where
    app h =
      aux
      <$> root   h
      <*> drop o h
    aux a hs =
      a : list o hs

--------------------------------------------------------------------------------

meld
  :: Ord  a
  => Ordering
  -> Heap a
  -> Heap a
  -> Heap a
meld _     Leaf           node       = node
meld _     node           Leaf       = node
meld o t1@(Node x xs) t2@(Node y ys) =
  if o == compare x y
  then Node x (t2 : xs)
  else Node y (t1 : ys)

pair
  ::  Ord  a
  => Ordering
  -> [Heap a]
  ->  Heap a
pair _ [ ]      = Leaf
pair _ [x]      = x
pair o (x:y:hs) =
  meld o t r
  where
    t = meld o x y
    r = pair o hs

References: