Skip to content

Instantly share code, notes, and snippets.

@kachayev
Last active July 10, 2018 16:52
Show Gist options
  • Save kachayev/6081643 to your computer and use it in GitHub Desktop.
Save kachayev/6081643 to your computer and use it in GitHub Desktop.
Skew Heap implementation in Haskell
data Skew a = Empty | Node a (Skew a) (Skew a)
singleton :: Ord a => a -> Skew a
singleton x = Node x Empty Empty
union :: Ord a => Skew a -> Skew a -> Skew a
union t1 Empty = t1
union Emtpy t2 = t2
union t1@(Node x1 l1 r1) t2@(Node x2 l2 r2)
| x1 <= x2 = Node x1 (union t2 r1) l1
| otherwise = Node x2 (union t1 r2) l2
insert :: Ord a => a -> Skew a -> Skew a
insert x heap = union (singleton x) heap
extractMin :: Ord a => Skew a -> Maybe (a, Skew a)
extractMin Empty = Nothing
extractMin (Node x l r) = Just (x, union l r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment