src/Pure/General/heap.ML
author paulson
Tue, 20 Jun 2000 11:54:22 +0200
changeset 9095 3b26cc949016
child 9413 ba209591a8d4
permissions -rw-r--r--
new module for heaps
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9095
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
     1
(* Source code from
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
     2
 *   Purely Functional Data Structures (Chapter 3)
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
     3
 *   Chris Okasaki
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
     4
 *   Cambridge University Press, 1998
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
     5
 *
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
     6
 * Copyright (c) 1998 Cambridge University Press
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
     7
    ID:         $Id$
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
     8
 *)
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
     9
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    10
signature ORDERED =
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    11
  (* a totally ordered type and its comparison functions *)
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    12
sig
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    13
  type T
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    14
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    15
  val eq  : T * T -> bool
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    16
  val lt  : T * T -> bool
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    17
  val leq : T * T -> bool
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    18
end;
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    19
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    20
signature HEAP =
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    21
sig
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    22
  structure Elem : ORDERED
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    23
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    24
  type Heap
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    25
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    26
  val empty     : Heap
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    27
  val isEmpty   : Heap -> bool
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    28
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    29
  val insert    : Elem.T * Heap -> Heap
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    30
  val merge     : Heap * Heap -> Heap
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    31
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    32
  val findMin   : Heap -> Elem.T   (* raises Empty if heap is empty *)
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    33
  val deleteMin : Heap -> Heap     (* raises Empty if heap is empty *)
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    34
end;
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    35
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    36
functor LeftistHeap (Element : ORDERED) : HEAP =
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    37
struct
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    38
  structure Elem = Element
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    39
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    40
  datatype Heap = E | T of int * Elem.T * Heap * Heap
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    41
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    42
  fun rank E = 0
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    43
    | rank (T (r,_,_,_)) = r
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    44
  fun makeT (x, a, b) = if rank a >= rank b then T (rank b + 1, x, a, b)
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    45
                        else T (rank a + 1, x, b, a)
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    46
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    47
  val empty = E
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    48
  fun isEmpty E = true | isEmpty _ = false
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    49
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    50
  fun merge (h, E) = h
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    51
    | merge (E, h) = h
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    52
    | merge (h1 as T (_, x, a1, b1), h2 as T (_, y, a2, b2)) =
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    53
        if Elem.leq (x, y) then makeT (x, a1, merge (b1, h2))
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    54
        else makeT (y, a2, merge (h1, b2))
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    55
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    56
  fun insert (x, h) = merge (T (1, x, E, E), h)
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    57
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    58
  fun findMin E = raise List.Empty
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    59
    | findMin (T (_, x, a, b)) = x
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    60
  fun deleteMin E = raise List.Empty
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    61
    | deleteMin (T (_, x, a, b)) = merge (a, b)
3b26cc949016 new module for heaps
paulson
parents:
diff changeset
    62
end;