7 | data SplayHeap a = E | T (SplayHeap a) a (SplayHeap a)
9 | partition : Ord a => a -> SplayHeap a -> (SplayHeap a, SplayHeap a)
10 | partition pivot E = (E, E)
11 | partition pivot t@(T a x b) =
17 | let (small, big) = partition pivot $
assert_smaller t b2
18 | in (T (T a x b1) y small, big)
20 | let (small, big) = partition pivot $
assert_smaller t b1
21 | in (T a x small, T big y b2)
27 | let (small, big) = partition pivot $
assert_smaller t a2
28 | in (T a1 y small, T big x b)
30 | let (small, big) = partition pivot $
assert_smaller t a1
31 | in (small, T (T big y a2) x b)
34 | Heap SplayHeap where
39 | insert x t = let (a, b) = partition x t in T a x b
42 | merge (T a x b) t = let (ta, tb) = partition x t
43 | in T (assert_total $
merge ta a) x (assert_total $
merge tb b)
45 | findMin E = assert_total $
idris_crash "empty heap"
46 | findMin (T E x b) = x
47 | findMin (T a x b) = findMin a
49 | deleteMin E = assert_total $
idris_crash "empty heap"
50 | deleteMin (T E x b) = b
51 | deleteMin (T (T E x b) y c) = T b y c
52 | deleteMin (T (T a x b) y c) = T (deleteMin a) x (T b y c)