commit 3b45639d841d66015e077edff1e0bf321d2a8855
parent 081ad2e65d924efa31da0a02cdaa224d2daaa538
Author: Jared Tobin <jared@jtobin.ca>
Date: Thu, 20 Nov 2014 23:41:08 +1300
Polish.
Diffstat:
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/LeftistHeap.hs b/LeftistHeap.hs
@@ -56,16 +56,15 @@ fromList = foldr insert empty
-- exercise 3.2 (define insert directly rather than by merge)
altInsert :: Ord a => a -> Heap a -> Heap a
altInsert e Leaf = singleton e
-altInsert e0 (Node _ e1 l r) = create top l0 r0 where
- (l0, r0) = cascadeInsert bottom (l, r)
- (top, bottom)
+altInsert e0 (Node _ e1 l r) = create upper l0 r0 where
+ (l0, r0) = cascadeInsert (l, r)
+ (upper, lower)
| e0 <= e1 = (e0, e1)
| otherwise = (e1, e0)
-cascadeInsert :: Ord a => a -> (Heap a, Heap a) -> (Heap a, Heap a)
-cascadeInsert e (h, Leaf) = (h, singleton e)
-cascadeInsert e (Leaf, h) = (h, singleton e)
-cascadeInsert e hs@(Node _ e0 _ _, Node _ e1 _ _)
- | e0 > e1 = first (altInsert e) hs
- | otherwise = second (altInsert e) hs
+ cascadeInsert (h, Leaf) = (h, singleton lower)
+ cascadeInsert (Leaf, h) = (h, singleton lower)
+ cascadeInsert hs@(Node _ x _ _, Node _ y _ _)
+ | x > y = first (altInsert lower) hs
+ | otherwise = second (altInsert lower) hs