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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
functor Tree (Key: sig
type key
val compare: key * key -> order
end) :> sig
type key = Key.key
type 'datum tree
val size: 'datum tree -> int
val empty: 'datum tree
val single: key * 'datum -> 'datum tree
val add: 'datum tree * key * 'datum -> 'datum tree
val find: 'datum tree * key -> 'datum option
val exists: 'datum tree * key -> bool
val up: 'datum tree -> (key * 'datum) Stream.stream
val down: 'datum tree -> (key * 'datum) Stream.stream
val upFrom: 'datum tree * key -> (key * 'datum) Stream.stream
val downFrom: 'datum tree * key -> (key * 'datum) Stream.stream
val fromList: (key * 'datum) list -> 'datum tree
val fromStream: (key * 'datum) Stream.stream -> 'datum tree
(*
val remove: 'datum tree * key -> 'datum tree
*)
end = struct
type key = Key.key
structure Height = Int8
datatype 'datum tree =
Leaf
| Branch of {
height: Height.int
, less: 'datum tree
, key: key
, datum: 'datum
, greater: 'datum tree
}
fun size tree = case tree of
Leaf => 0
| Branch {less, greater, ...} =>
1 + size less + size greater
val empty = Leaf
fun single (key, datum) = Branch {
height = 1
, less = Leaf
, key = key
, datum = datum
, greater = Leaf
}
fun height tree = case tree of
Leaf => 0
| Branch {height, ...} => height
fun calculateHeight {key, datum, less, greater} = Branch {
key = key
, datum = datum
, less = less
, greater = greater
, height = 1 + Height.max (height less, height greater)
}
fun rotateLess branch = case branch of
(*
b d
a d => b e
c e a c
*)
{
less = a
, key = bKey
, datum = bDatum
, greater = Branch {
less = c
, key = dKey
, datum = dDatum
, greater = e
, height = _
}
} => calculateHeight {
less = calculateHeight {
less = a
, key = bKey
, datum = bDatum
, greater = c
}, key = dKey
, datum = dDatum
, greater = e
} | _ => raise Fail "rotateLess"
fun rotateGreater branch = case branch of
(*
d b
b e => a d
a c c e
*)
{
less = Branch {
less = a
, key = bKey
, datum = bDatum
, greater = c
, height = _
}, key = dKey
, datum = dDatum
, greater = e
} => calculateHeight {
less = a
, key = bKey
, datum = bDatum
, greater = calculateHeight {
less = c
, key = dKey
, datum = dDatum
, greater = e
}
} | _ => raise Fail "rotateGreater"
fun balance (branch as {key, datum, less, greater}) =
let
val heightLess = height less
val heightGreater = height greater
in
if heightLess < heightGreater - 2 then
rotateLess branch
else if heightLess > heightGreater + 2 then
rotateGreater branch
else calculateHeight branch
end
fun add (tree, newKey, newDatum) = case tree of
Leaf => single (newKey, newDatum)
| Branch {height, less, key, datum, greater} =>
case Key.compare (newKey, key) of
EQUAL => Branch {
height = height
, less = less
, key = newKey
, datum = newDatum
, greater = greater
} | LESS => balance {
less = add (less, newKey, newDatum)
, key = key
, datum = datum
, greater = greater
} | GREATER => balance {
less = less
, key = key
, datum = datum
, greater = add (greater, newKey, newDatum)
}
fun find (tree, desiredKey) = case tree of
Leaf => NONE
| Branch {less, key, datum, greater, ...} =>
case Key.compare (desiredKey, key) of
EQUAL => SOME datum
| LESS => find (less, desiredKey)
| GREATER => find (greater, desiredKey)
fun exists (tree, desiredKey) = case find (tree, desiredKey) of
NONE => false
| SOME _ => true
fun up tree = Stream.create (fn () =>
case tree of
Leaf => NONE
| Branch {less, key, datum, greater, ...} =>
Stream.getItem (
Stream.append (
up less
, Stream.cons (
(key, datum)
, up greater
)
)
)
)
fun down tree = Stream.create (fn () =>
case tree of
Leaf => NONE
| Branch {greater, key, datum, less, ...} =>
Stream.getItem (
Stream.append (
down greater
, Stream.cons (
(key, datum)
, down less
)
)
)
)
fun upFrom (tree, firstKey) = Stream.create (fn () =>
case tree of
Leaf => NONE
| Branch {less, key, datum, greater, ...} =>
case Key.compare (firstKey, key) of
LESS => Stream.getItem (
Stream.append (
upFrom (less, firstKey)
, Stream.cons (
(key, datum)
, up greater
)
)
) | EQUAL => SOME (
(key, datum)
, up greater
) | GREATER => Stream.getItem (
upFrom (greater, firstKey)
)
)
fun downFrom (tree, firstKey) = Stream.create (fn () =>
case tree of
Leaf => NONE
| Branch {greater, key, datum, less, ...} =>
case Key.compare (firstKey, key) of
LESS => Stream.getItem (
downFrom (less, firstKey)
) | EQUAL => SOME (
(key, datum)
, down less
) | GREATER => Stream.getItem (
Stream.append (
downFrom (greater, firstKey)
, Stream.cons (
(key, datum)
, down less
)
)
)
)
fun fromStream stream =
Stream.fold (fn ((key, datum), tree) =>
add (tree, key, datum)
) empty stream
fun fromList list = fromStream (Stream.fromList list)
end
|