-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.hs
61 lines (50 loc) · 2.26 KB
/
PriorityQueue.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 PriorityQueue where
data PriorityQueue k v = PQE | PriorityQueue{key :: k, val :: v, l :: PriorityQueue k v,
r :: PriorityQueue k v, sz :: Int}
instance (Show(k), Show(v), Ord(k)) => Show (PriorityQueue k v) where
show PQE = ""
show pq = show (getMin pq) ++ "[" ++ show (l pq) ++ "][" ++ show (r pq) ++ "]"
empty :: Ord(k) => PriorityQueue k v
empty = PQE
isEmpty :: Ord(k) => PriorityQueue k v -> Bool
isEmpty PQE = True
isEmpty _ = False
size :: Ord(k) => PriorityQueue k v -> Int
size PQE = 0
size pq = sz pq
insert :: Ord(k) => PriorityQueue k v -> k -> v -> PriorityQueue k v
insert PQE k' v' = PriorityQueue k' v' empty empty 1
insert pq k' v' | k'' < k' = if size lt > size rt
then pq{r = insert rt k' v', sz = size pq + 1}
else pq{l = insert lt k' v', sz = size pq + 1}
| otherwise = if size lt > size rt
then PriorityQueue k' v' lt (insert rt k'' v'') (size pq + 1)
else PriorityQueue k' v' (insert lt k'' v'') rt (size pq + 1)
where
k'' = key pq
v'' = val pq
lt = l pq
rt = r pq
insertP :: Ord(k) => PriorityQueue k v -> (k, v) -> PriorityQueue k v
insertP pq (x, y) = insert pq x y
getMinVal :: Ord(k) => PriorityQueue k v -> v
getMinVal PQE = error "empty pq"
getMinVal pq = val pq
getMinKey :: Ord(k) => PriorityQueue k v -> k
getMinKey PQE = error "empty pq"
getMinKey pq = key pq
getMin :: Ord(k) => PriorityQueue k v -> (k, v)
getMin PQE = error "empty pq"
getMin pq = (key pq, val pq)
changeMin :: Ord(k) => PriorityQueue k v -> k -> v -> PriorityQueue k v
changeMin PQE _ _ = PQE
changeMin pq x y | isEmpty $ l pq = insert empty x y
| isEmpty $ r pq = insertP (insert empty x y) $ getMin $ l pq
| x < min lx rx = pq{key = x, val = y}
| lx < rx = pq{key = lx, val = ly, l = changeMin lt x y}
| otherwise = pq{key = rx, val = ry, r = changeMin rt x y}
where
lt = l pq
rt = r pq
(lx, ly) = getMin lt
(rx, ry) = getMin rt