403616257a86302a965fbc1e5fb63302887ad2ac
[tiger.ml.git] / exercises / ch01 / tree_balanced_red_black.ml
1 module List = ListLabels
2
3 type color = R | B
4 type ('k, 'v) t =
5 | Leaf
6 | Node of color * ('k * 'v) * ('k, 'v) t * ('k, 'v) t
7
8 let empty = Leaf
9
10 let set t ~k ~v =
11 let balance = function
12 | Leaf -> assert false
13 (* LL *) | Node (B, x, Node (R, lx, Node (R, llx, lll, llr), lr ), r ) -> Node (R, lx , Node (B, llx, lll, llr), Node (B, x , lr , r ))
14 (* LR *) | Node (B, x, Node (R, lx, ll , Node (R, lrx, lrl, lrr)), r ) -> Node (R, lrx, Node (B, lx , ll , lrl), Node (B, x , lrr, r ))
15 (* RL *) | Node (B, x, l , Node (R, rx, Node (R, rlx, rll, rlr), rr )) -> Node (R, rlx, Node (B, x , l , rll), Node (B, rx , rlr, rr ))
16 (* RR *) | Node (B, x, l , Node (R, rx, rl , Node (R, rrx, rrl, rrr))) -> Node (R, rx , Node (B, x , l , rl ), Node (B, rrx, rrl, rrr))
17 (* not exhaustive - reconsider *)
18 | node -> node
19 in
20 let rec set t k v =
21 match t with
22 (* Because we recur, we cannot at this stage know if the following Node
23 * with Leaf children will end-up as root (in which case it should be
24 * black) or as the last node before leaves (in which case it should be
25 * red). We begin by assuming that it is the later and at the very end,
26 * before returning to the caller, force the actual root node (which is
27 * easy to identify at that point) to be black, regardless of what it
28 * already is.
29 *)
30 | Leaf -> Node (R, (k, v), Leaf, Leaf) (* Maybe root or last node, so R *)
31 | Node (c, ((k', _) as x), l, r) when k < k' -> balance (Node (c, x, set l k v, r))
32 | Node (c, ((k', _) as x), l, r) when k > k' -> balance (Node (c, x, l , set r k v))
33 | Node (c, _ , l, r) -> Node (c, (k, v), l, r)
34 in
35 match set t k v with
36 | Leaf -> assert false (* Can we GADT this away? *)
37 | Node (_, (k, v), l, r) -> Node (B, (k, v), l, r) (* Root is always Black *)
38
39 let rec get t ~k =
40 match t with
41 | Leaf -> None
42 | Node (_, (k', _), l, _) when k < k' -> get l ~k
43 | Node (_, (k', _), _, r) when k > k' -> get r ~k
44 | Node (_, (_ , v), _, _) -> Some v
45
46 let rec member t ~k =
47 match t with
48 | Leaf -> false
49 | Node (_, (k', _), l, _) when k < k' -> member l ~k
50 | Node (_, (k', _), _, r) when k > k' -> member r ~k
51 | Node (_, (_ , _), _, _) -> true
52
53 let to_edges t =
54 let rec to_edges_from node1 t =
55 match t with
56 | Leaf -> [(node1, `Leaf)]
57 | Node (c2, (k2, _), l, r) ->
58 let node2 = k2, c2 in
59 (node1, `Node node2) :: ((to_edges_from node2 l) @ (to_edges_from node2 r))
60 in
61 match t with
62 | Leaf ->
63 []
64 | Node (c, (k, _), l, r) ->
65 let node1 = k, c in
66 (to_edges_from node1 l) @ (to_edges_from node1 r)
67
68 let color_to_string = function
69 | B -> "black"
70 | R -> "red"
71
72 let to_dot t ~k_to_string =
73 let (dot_edges_and_nodes, _, _) =
74 List.fold_left
75 (to_edges t)
76 ~init:("", "\n", 0)
77 ~f:(fun (dot_edges_and_nodes, sep, leaves) ((k1, c1), node2_or_leaf) ->
78 let k1 = k_to_string k1 in
79 let k2, c2, leaves =
80 match node2_or_leaf with
81 | `Leaf ->
82 let leaves = succ leaves in
83 let label = Printf.sprintf "Leaf_%d" leaves in
84 (label, B, leaves)
85 | `Node (k2, c2) ->
86 let label = k_to_string k2 in
87 (label, c2, leaves)
88 in
89 let dot_edges_and_nodes =
90 Printf.sprintf
91 "%s%s %S -> %S;\n %S [color=%s,fontcolor=white,style=filled];\n %S [color=%s,fontcolor=white,style=filled];\n"
92 dot_edges_and_nodes
93 sep
94 k1 k2
95 k1 (color_to_string c1) (* Yes, it's redundant... *)
96 k2 (color_to_string c2)
97 in
98 let sep = "" in
99 (dot_edges_and_nodes, sep, leaves)
100 )
101 in
102 "digraph G {" ^ dot_edges_and_nodes ^ "}"
This page took 0.068055 seconds and 3 git commands to generate.