-
Notifications
You must be signed in to change notification settings - Fork 1
/
red_black_tree_optimized.ml
63 lines (52 loc) · 1.73 KB
/
red_black_tree_optimized.ml
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
(* Exercise 2.2 - less calls to compare function in `member`, useful if compare is expensive
Exercise 3.10 - faster balancing *)
module type SET =
sig
type elem
type set
val empty : set
val insert : elem -> set -> set
val member : elem -> set -> bool
end
module type ORDERED =
(* a totally ordered type and it's comparison function *)
sig
type t
val compare : t -> t -> int
end
module RedBlackTree2 (Element:ORDERED) : (SET with type elem = Element.t) =
struct
type elem = Element.t
type color = R | B
type tree = E | T of color * tree * elem * tree
type set = tree
let empty = E
let lbalance = function
| (B, T (R, T (R, a, x, b), y, c), z, d)
| (B, T (R, a, x, T (R, b, y, c)), z, d) -> T (R, T (B, a, x, b), y, T (B, c, z, d))
| color, a, x, b -> T (color, a, x, b)
let rbalance = function
| (B, a, x, T (R, T (R, b, y, c), z, d))
| (B, a, x, T (R, b, y, T (R, c, z, d))) -> T (R, T (B, a, x, b), y, T (B, c, z, d))
| color, a, x, b -> T (color, a, x, b)
let rec insert x t =
let rec ins = function
| E -> T (R, E, x, E)
| T (color, a, y, b) as s ->
if Element.compare x y < 0 then lbalance (color, ins a, y, b)
else if Element.compare x y > 0 then rbalance (color, a, y, ins b)
else s
in
let T (_, a, y, b) = ins t in
T (B, a, y, b)
let member x s =
let rec go x s last =
match s, last with
| E, None -> false
| E, Some v -> (Element.compare x v) = 0
| T (_, a, y, b), last ->
if Element.compare x y < 0 then go x a last
else go x b (Some y)
in
go x s None
end