-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap.hs
61 lines (43 loc) · 1.71 KB
/
Heap.hs
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
module Heap(Heap,emptyHeap,heapEmpty,findHeap,insHeap,delHeap)
where
emptyHeap:: (Ord a) => Heap a
heapEmpty:: (Ord a) => Heap a -> Bool
findHeap :: (Ord a) => Heap a -> a
insHeap :: (Ord a) => a -> Heap a -> Heap a
delHeap :: (Ord a) => Heap a -> Heap a
-- IMPLEMENTATION with Leftist Heap
-- adapted from C. Okasaki Purely Functional Data Structures p 197
data {-- (Ord a) =>--} Heap a = EmptyHP
| HP a Int (Heap a) (Heap a)
deriving Show
emptyHeap = EmptyHP
heapEmpty EmptyHP = True
heapEmpty _ = False
findHeap EmptyHP = error "findHeap:empty heap"
findHeap (HP x _ a b) = x
insHeap x h = merge (HP x 1 EmptyHP EmptyHP) h
delHeap EmptyHP = error "delHeap:empty heap"
delHeap (HP x _ a b) = merge a b
-- auxiliary functions
rank :: (Ord a) => Heap a -> Int
rank EmptyHP = 0
rank (HP _ r _ _) = r
makeHP :: (Ord a) => a -> Heap a -> Heap a -> Heap a
makeHP x a b | rank a >= rank b = HP x (rank b + 1) a b
| otherwise = HP x (rank a + 1) b a
merge ::(Ord a) => Heap a -> Heap a -> Heap a
merge h EmptyHP = h
merge EmptyHP h = h
merge h1@(HP x _ a1 b1) h2@(HP y _ a2 b2)
| x <= y = makeHP x a1 (merge b1 h2)
| otherwise = makeHP y a2 (merge h1 b2)
-- examples of use of auxiliary functions
fig5_5a = insHeap 6 (insHeap 1(insHeap 4 (insHeap 8 emptyHeap)))
-- HP 1 2 (HP 4 1 (HP 8 1 EmptyHP EmptyHP) EmptyHP)
-- (HP 6 1 EmptyHP EmptyHP)
fig5_5b = insHeap 7 (insHeap 5 emptyHeap)
-- HP 5 1 (HP 7 1 EmptyHP EmptyHP) EmptyHP
{- examples of calls and results
Heap> merge fig5_5a fig5_5b
HP 1 2 (HP 5 2 (HP 7 1 EmptyHP EmptyHP) (HP 6 1 EmptyHP EmptyHP)) (HP 4 1 (HP 8 1 EmptyHP EmptyHP) EmptyHP)
-}