1 (* Source code from |
1 (* Title: Pure/General/heap.ML |
2 * Purely Functional Data Structures (Chapter 3) |
|
3 * Chris Okasaki |
|
4 * Cambridge University Press, 1998 |
|
5 * |
|
6 * Copyright (c) 1998 Cambridge University Press |
|
7 ID: $Id$ |
2 ID: $Id$ |
8 *) |
3 Author: Markus Wenzel, TU Muenchen |
|
4 License: GPL (GNU GENERAL PUBLIC LICENSE) |
9 |
5 |
10 signature ORDERED = |
6 Heaps over linearly ordered types. See also Chris Okasaki: "Purely |
11 (* a totally ordered type and its comparison functions *) |
7 Functional Data Structures" (Chapter 3), Cambridge University Press, |
12 sig |
8 1998. |
13 type T |
9 *) |
14 |
|
15 val eq : T * T -> bool |
|
16 val lt : T * T -> bool |
|
17 val leq : T * T -> bool |
|
18 end; |
|
19 |
10 |
20 signature HEAP = |
11 signature HEAP = |
21 sig |
12 sig |
22 structure Elem : ORDERED |
13 type elem |
23 |
14 type T |
24 type Heap |
15 val empty: T |
25 |
16 val is_empty: T -> bool |
26 val empty : Heap |
17 val merge: T * T -> T |
27 val isEmpty : Heap -> bool |
18 val insert: elem * T -> T |
28 |
19 exception EMPTY |
29 val insert : Elem.T * Heap -> Heap |
20 val min: T -> elem (*exception EMPTY*) |
30 val merge : Heap * Heap -> Heap |
21 val delete_min: T -> T (*exception EMPTY*) |
31 |
|
32 val findMin : Heap -> Elem.T (* raises Empty if heap is empty *) |
|
33 val deleteMin : Heap -> Heap (* raises Empty if heap is empty *) |
|
34 end; |
22 end; |
35 |
23 |
36 functor LeftistHeap (Element : ORDERED) : HEAP = |
24 functor HeapFun(type elem val ord: elem * elem -> order): HEAP = |
37 struct |
25 struct |
38 structure Elem = Element |
|
39 |
26 |
40 datatype Heap = E | T of int * Elem.T * Heap * Heap |
|
41 |
27 |
42 fun rank E = 0 |
28 (* datatype heap *) |
43 | rank (T (r,_,_,_)) = r |
|
44 fun makeT (x, a, b) = if rank a >= rank b then T (rank b + 1, x, a, b) |
|
45 else T (rank a + 1, x, b, a) |
|
46 |
29 |
47 val empty = E |
30 type elem = elem; |
48 fun isEmpty E = true | isEmpty _ = false |
31 datatype T = Empty | Heap of int * elem * T * T; |
49 |
32 |
50 fun merge (h, E) = h |
|
51 | merge (E, h) = h |
|
52 | merge (h1 as T (_, x, a1, b1), h2 as T (_, y, a2, b2)) = |
|
53 if Elem.leq (x, y) then makeT (x, a1, merge (b1, h2)) |
|
54 else makeT (y, a2, merge (h1, b2)) |
|
55 |
33 |
56 fun insert (x, h) = merge (T (1, x, E, E), h) |
34 (* empty heaps *) |
57 |
35 |
58 fun findMin E = raise List.Empty |
36 val empty = Empty; |
59 | findMin (T (_, x, a, b)) = x |
37 |
60 fun deleteMin E = raise List.Empty |
38 fun is_empty Empty = true |
61 | deleteMin (T (_, x, a, b)) = merge (a, b) |
39 | is_empty (Heap _) = false; |
|
40 |
|
41 |
|
42 (* build heaps *) |
|
43 |
|
44 local |
|
45 |
|
46 fun rank Empty = 0 |
|
47 | rank (Heap (r, _, _, _)) = r; |
|
48 |
|
49 fun heap x a b = |
|
50 if rank a >= rank b then Heap (rank b + 1, x, a, b) |
|
51 else Heap (rank a + 1, x, b, a); |
|
52 |
|
53 in |
|
54 |
|
55 fun merge (h, Empty) = h |
|
56 | merge (Empty, h) = h |
|
57 | merge (h1 as Heap (_, x1, a1, b1), h2 as Heap (_, x2, a2, b2)) = |
|
58 (case ord (x1, x2) of |
|
59 Library.GREATER => heap x2 a2 (merge (h1, b2)) |
|
60 | _ => heap x1 a1 (merge (b1, h2))); |
|
61 |
|
62 fun insert (x, h) = merge (Heap (1, x, Empty, Empty), h); |
|
63 |
62 end; |
64 end; |
|
65 |
|
66 |
|
67 (* minimum element *) |
|
68 |
|
69 exception EMPTY |
|
70 |
|
71 fun min Empty = raise EMPTY |
|
72 | min (Heap (_, x, _, _)) = x; |
|
73 |
|
74 fun delete_min Empty = raise EMPTY |
|
75 | delete_min (Heap (_, _, a, b)) = merge (a, b); |
|
76 |
|
77 end; |