Skip to content

Instantly share code, notes, and snippets.

@kachayev
Created October 25, 2013 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kachayev/7161521 to your computer and use it in GitHub Desktop.
Save kachayev/7161521 to your computer and use it in GitHub Desktop.
data Pairing a = Empty | Node a [Pairing a]
singleton :: Ord a => a -> Pairing a
singleton x = Node x []
union :: Ord a => Pairing a -> Pairing a -> Pairing a
union Empty h2 = h2
union h1 Empty = h1
union t1@(Pairing h1 subs1) t2@(Pairing h2 subs)
| h1 < h2 = Pairing h1 t2:subs1
| otherwise = Pairing h2 t1:subs2
insert :: Ord a => a -> Pairing a -> Pairing a
insert x heap = union (singleton x) heap
extractMin :: Ord a => Pairing a -> Maybe (a, Pairing a)
extractMin Empty = Nothing
extractMin (Pairing x subs) = Just (x, pairing subs)
pairing [] = Empty
pairing [h1] = h1
pairing h1:h2:tail = pairing (union h1 h2):tail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment