summaryrefslogtreecommitdiff
path: root/tree.sml
blob: 597e469c873d432d455142b811c0fd832a1cfe52 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
structure Tree: TREE = struct
  datatype ('k, 'v) t = Node of 'k * 'v * ('k, 'v) t * ('k, 'v) t | Empty

  exception Exists

  val empty = Empty

  fun insert _ Empty k v = Node (k, v, Empty, Empty)
    | insert cmp (Node (k', v', left, right)) k v =
        case cmp k k' of
          LESS => Node (k', v', insert cmp left k v, right)
        | EQUAL => raise Exists
        | GREATER => Node (k', v', left, insert cmp right k v)

  fun lookup _ Empty _ = NONE
    | lookup cmp (Node (k', v', left, right)) k =
        case cmp k k' of
          LESS => lookup cmp left k
        | EQUAL => SOME v'
        | GREATER => lookup cmp right k

  datatype ('k, 'v) arc =
    Left of 'k * 'v * ('k, 'v) t |
    Right of 'k * 'v * ('k, 'v) t

  fun assemble n buf =
  let
    fun assemble' tree (Left (k, v, right) :: tail) =
      assemble' (Node (k, v, tree, right)) tail
      | assemble' tree (Right (k, v, left) :: tail) =
      assemble' (Node (k, v, left, tree)) tail
      | assemble' tree [] = tree
  in
    assemble' n buf
  end

  fun lookup' _ _ Empty _ _ g = (g, NONE)
    | lookup' buf cmp (Node (k', v', left, right)) k f g =
        case cmp k k' of
          LESS => lookup' (Left (k', v', right) :: buf) cmp left k f g
        | GREATER => lookup' (Right (k', v', left) :: buf) cmp right k f g
        | EQUAL =>
        let
          val (newV, result) = f v'
        in
          case newV of
               NONE => (result, NONE)
             | SOME v => (result, SOME (assemble (Node (k', v, left, right)) buf))
        end

  fun lookup2 cmp t k (f, g) =
  let
    val (result, newTree) = lookup' [] cmp t k f g
  in
    (result, case newTree of
         NONE => t
       | SOME t => t)
  end

  fun print t key2str value2str =
  let
    fun Pkey z = bindWith2str key2str z
    fun Pvalue z = bindWith2str value2str z

    fun print' off Empty = printf R off `"()\n" %
      | print' off (Node (k, v, left, right)) = (
        printf R off `"(" Pkey k `", " Pvalue v `"\n";
        print' (off + 1) left;
        print' (off + 1) right;
        printf R off `")\n" %
      )
  in
    print' 0 t
  end
end