diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-04-30 09:16:03 +0200 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-05-06 16:45:25 +0200 |
| commit | 960d5b8f2728f71dd9ce038d527844d82ca2f0f2 (patch) | |
| tree | 9a15dc43f91a0b58a3aaa10223bba35abf7ef5f2 /lib/splay | |
| parent | a5efd7d15b4d049a4ed539ed5d9c2f1037d00973 (diff) | |
Allow updates for existing contents
Diffstat (limited to 'lib/splay')
| -rw-r--r-- | lib/splay/splay.ml | 44 | ||||
| -rw-r--r-- | lib/splay/tests/splay.t | 24 |
2 files changed, 39 insertions, 29 deletions
diff --git a/lib/splay/splay.ml b/lib/splay/splay.ml index bf0a2ac..979d017 100644 --- a/lib/splay/splay.ml +++ b/lib/splay/splay.ml @@ -28,40 +28,40 @@ module Make (El : KEY) = struct let (Node (l, y, r)) = t in begin match El.compare x (fst y) with | 0 -> t - | v when v < 0 -> begin - match l with + | v when v < 0 -> + begin match l with | Leaf -> t - | Node (ll, z, rr) -> begin - match El.compare x (fst z) with + | Node (ll, z, rr) -> + begin match El.compare x (fst z) with | 0 -> Node (ll, z, Node (rr, y, r)) - | v when v < 0 -> begin - match ll with + | v when v < 0 -> + begin match ll with | Leaf -> Node (ll, z, Node (rr, y, r)) | Node _ as ll -> let (Node (newL, newV, newR)) = splay x ll in Node (newL, newV, Node (newR, z, Node (rr, y, r))) - end + end | _ -> ( match rr with | Leaf -> Node (ll, z, Node (rr, y, r)) | Node _ as rr -> let (Node (newL, newV, newR)) = splay x rr in Node (Node (ll, z, newL), newV, Node (newR, y, r))) - end - end + end + end | _ -> ( match r with | Leaf -> t | Node (ll, z, rr) -> ( match El.compare x (fst z) with | 0 -> Node (Node (l, y, ll), z, rr) - | v when v < 0 -> begin - match ll with + | v when v < 0 -> + begin match ll with | Leaf -> Node (Node (l, y, ll), z, rr) | Node _ as ll -> let (Node (newL, newV, newR)) = splay x ll in Node (Node (l, y, newL), newV, Node (newR, z, rr)) - end + end | _ -> ( match rr with | Leaf -> Node (Node (l, y, ll), z, rr) @@ -128,8 +128,8 @@ module Make (El : KEY) = struct let root' = splay key root in let (Node (l, c', r)) = root' in begin match El.compare (fst c') key with - | 0 -> begin - match _subtree_maximum l with + | 0 -> + begin match _subtree_maximum l with | Node (l, c, Leaf) -> T (ref (Node (l, c, r))) | Node (_, _, _) -> raise Not_found | Leaf -> ( @@ -137,7 +137,7 @@ module Make (El : KEY) = struct | Leaf -> empty () | Node (Leaf, c, r) -> T (ref (Node (l, c, r))) | Node (_, _, _) -> raise Not_found) - end + end (* The key is not present, return the splayed tree *) | _ -> T (ref root') end @@ -155,11 +155,11 @@ module Make (El : KEY) = struct let c = (key, value) in (_fold [@tailcall]) ((c, Branch right) :: acc) v left (* We have nothing left, we process the values delayed *) - | Leaf -> begin - match acc with + | Leaf -> + begin match acc with | [] -> v | (c, Branch right) :: tl -> (_fold [@tailcall]) tl (f v c) right - end + end in _fold [] init !t @@ -174,14 +174,14 @@ module Make (El : KEY) = struct let pp formatter (T t) = let leaf_id = ref 0 in let repr_edge from formatter dest = - Format.fprintf formatter "\"%a\" -> \"%a\"@;" El.pp from El.pp dest + Format.fprintf formatter "@,\"%a\" -> \"%a\"" El.pp from El.pp dest in let rec repr' : type b. El.t -> Format.formatter -> (b, _) branch -> unit = fun parent formatter -> function | Leaf -> - Format.fprintf formatter "leaf_%d [shape=point]@;" !leaf_id; - Format.fprintf formatter "\"%a\" -> leaf_%d@;" El.pp parent !leaf_id; + Format.fprintf formatter "@,leaf_%d [shape=point]" !leaf_id; + Format.fprintf formatter "@,\"%a\" -> leaf_%d" El.pp parent !leaf_id; incr leaf_id | Node (l, c, r) -> let key = fst c in @@ -193,6 +193,6 @@ module Make (El : KEY) = struct | Leaf -> Format.fprintf formatter "digraph G {}" | Node (l, c, r) -> let key = fst c in - Format.fprintf formatter "@[<v 2>digraph G {@;%a%a@]}" (repr' key) l + Format.fprintf formatter "@[<v 2>digraph G {%a%a@]@,}" (repr' key) l (repr' key) r end diff --git a/lib/splay/tests/splay.t b/lib/splay/tests/splay.t index 64f77a6..7b49803 100644 --- a/lib/splay/tests/splay.t +++ b/lib/splay/tests/splay.t @@ -1,8 +1,18 @@ -Tests for the splay tree. The tree representation used in the tests follow the -dot syntax +Tests for the splay tree. See https://en.wikipedia.org/wiki/Splay_tree +The test build a splay tree with the node given as argument, then display the +representation using the dot syntax. + + $ tests --splay_tree \ + > --help + Start the tests + --add Add an element in the tree + --del Remove an element from the tree + --get Get an element from the tree + -help Display this list of options + --help Display this list of options The root is the latest inserted node: @@ -17,7 +27,7 @@ The root is the latest inserted node: "5" -> leaf_1 leaf_2 [shape=point] "5" -> leaf_2 - } + } $ tests --splay_tree \ > --add 3 \ @@ -30,7 +40,7 @@ The root is the latest inserted node: "3" -> leaf_1 leaf_2 [shape=point] "5" -> leaf_2 - } + } Collecting an element in the tree will push this node at the root: @@ -50,7 +60,7 @@ Collecting an element in the tree will push this node at the root: "3" -> leaf_2 leaf_3 [shape=point] "3" -> leaf_3 - } + } $ tests --splay_tree \ > --add 1 \ @@ -72,7 +82,7 @@ Collecting an element in the tree will push this node at the root: "4" -> leaf_3 leaf_4 [shape=point] "4" -> leaf_4 - } + } Removing an element from the tree modify the structure @@ -89,5 +99,5 @@ Removing an element from the tree modify the structure "3" -> leaf_1 leaf_2 [shape=point] "3" -> leaf_2 - } + } |
