Commit bd0debdd authored by Giuseppe Castagna's avatar Giuseppe Castagna

added few more operations

parent e7f93568
......@@ -38,6 +38,7 @@ let balance ( Unbalanced('a) -> Rtree('a) ; 'b & RBtree('a) -> 'b & RBtree('a) )
| x -> x
;;
let balance ( Unbalanced('a) -> Rtree('a) ; 'b\Unbalanced(Any) -> 'b\Unbalanced(Any) )
| (<black (z)>[ <red (y)>[ <red (x)>[ a b ] c ] d ]
| <black (z)>[ <red (x)>[ a <red (y)>[ b c ] ] d ]
......@@ -47,6 +48,7 @@ let balance ( Unbalanced('a) -> Rtree('a) ; 'b\Unbalanced(Any) -> 'b\Unbalanced(
| x -> x
;;
(*
type UTree = <black (Any)>[ <red (Any)>[ <red (Any)>[ Any Any ] Any ] Any ]
| <black (Any)>[ <red (Any)>[ Any <red (Any)>[ Any Any ] ] Any ]
| <black (Any)>[ Any <red (Any)>[ <red (Any)>[ Any Any ] Any ] ]
......@@ -62,6 +64,11 @@ let balance ( Unbalanced('a) -> Rtree('a) ; 'b\UTree -> 'b\UTree )
<red (y)>[ <black (x)>[ a b ] <black (z)>[ c d ] ]
| x -> x
;;
*)
(* Here we start the definition of *)
(* standard operations for red-black trees *)
let insert (x : 'a) (t : Btree('a)) : Btree('a) =
let ins_aux ( [] -> Rtree('a);
......@@ -75,3 +82,20 @@ let insert (x : 'a) (t : Btree('a)) : Btree('a) =
in match ins_aux t with
| <_ (y)>[ a b ] -> <black (y)>[ a b ]
let is_empty ( RBtree('a) -> Bool ) | [] -> `true | _ -> `false
let member (x : 'a) (t : RBtree('a)) : Bool =
match t with
| [] -> `false
| <_ elem=y>[ left right ] ->
(y = x) || ( member x (if x<<y then left else right) )
let singleton (x : 'a): Btree('a) = <black elem=x>[ [] [] ]
let cardinal ( RBtree('a) -> Int )
| [] -> 0
| <_ ..>[ l r ] -> cardinal l + cardinal r
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment