fork download
  1. type color = R | B
  2. type 'a tree = E | T of color * 'a tree * 'a * 'a tree
  3.  
  4. let balance = function
  5. | B, T (R, T (R,a,x,b), y, c), z, d
  6. | B, T (R, a, x, T (R,b,y,c)), z, d
  7. | B, a, x, T (R, T (R,b,y,c), z, d)
  8. | 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))
  9. | c, a, x, b -> T (c, a, x, b)
  10.  
  11. let insert x s =
  12. let rec ins = function
  13. | E -> T (R,E,x,E)
  14. | T (c,a,y,b) as s ->
  15. if x < y then
  16. balance (c, ins a, y, b)
  17. else if x > y then
  18. balance (c, a, y, ins b)
  19. else
  20. s
  21. in let T (_,a,y,b) = ins s
  22. in T (B,a,y,b)
  23.  
  24. let rec mem x = function
  25. | E -> false
  26. | T (_, y, left, right) ->
  27. x = y || (x < y && mem x left) || (x > y && mem x right)
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
File "prog.ml", line 21, characters 15-26:
Warning P: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
E
File "prog.ml", line 27, characters 42-46:
This expression has type 'a but is here used with type 'a tree
stdout
Standard output is empty