Advertisement
Guest User

Untitled

a guest
Nov 4th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.33 KB | None | 0 0
  1. let balance_left l x r =
  2.     match l, x, r with
  3.       | Red(Red(a, x, b), y, c), z, d
  4.       | Red(a, x, Red(b, y, c)), z, d ->
  5.           Red(Black(a, x, b), y, Black(c, z, d))
  6.       | l, x, r ->
  7.           Black(l, x, r)
  8.  
  9.   let balance_right l x r =
  10.     match l, x, r with
  11.       | a, x, Red(Red(b, y, c), z, d)
  12.       | a, x, Red(b, y, Red(c, z, d)) ->
  13.           Red(Black(a, x, b), y, Black(c, z, d))
  14.       | l, x, r ->
  15.           Black(l, x, r)
  16.          
  17.   let unbalanced_left = function
  18.     | Red(Black(a, x, b), y, c) -> balance_left (Red(a, x, b)) y c, false
  19.     | Black(Black(a, x, b), y, c) -> balance_left (Red(a, x, b)) y c, true
  20.     | Black(Red(a, x, Black(b, y, c)), z, d) -> Black(a, x, balance_left (Red(b, y, c)) z d), false
  21.     | _ -> assert false
  22.  
  23.   let unbalanced_right = function
  24.     | Red(a, x, Black(b, y, c)) -> balance_right a x (Red(b, y, c)), false
  25.     | Black(a, x, Black(b, y, c)) -> balance_right a x (Red(b, y, c)), true
  26.     | Black(a, x, Red(Black(b, y, c), z, d)) -> Black(balance_right a x (Red(b, y, c)), z, d), false
  27.     | _ -> assert false          
  28.    
  29.    
  30.   let rec remove_min = function
  31.     | Empty
  32.     | Black(Empty, _, Black(_)) ->
  33.         assert false
  34.     | Black(Empty, x, Empty) ->
  35.         Empty, x, true
  36.     | Black(Empty, x, Red(l, y, r)) ->
  37.         Black(l, y, r), x, false
  38.     | Red(Empty, x, r) ->
  39.         r, x, false
  40.     | Black(l, x, r) ->
  41.         let l, y, d = remove_min l in
  42.         let s = Black(l, x, r) in
  43.           if d then
  44.             let s, d = unbalanced_right s in s, y, d
  45.           else
  46.             s, y, false
  47.     | Red(l, x, r) ->
  48.         let l, y, d = remove_min l in
  49.         let s = Red(l, x, r) in
  50.           if d then
  51.             let s, d = unbalanced_right s in s, y, d
  52.           else
  53.             s, y, false    
  54.    
  55.   let remove x s =
  56.     let rec remove_aux = function
  57.       | Empty ->
  58.           Empty, false
  59.       | Black(l, y, r) ->
  60.           let c = Ord.compare x y in
  61.             if c < 0 then
  62.               let l, d = remove_aux l in
  63.               let s = Black(l, y, r) in
  64.                 if d then unbalanced_right s else s, false
  65.             else if c > 0 then
  66.               let r, d = remove_aux r in
  67.               let s = Black(l, y, r) in
  68.                 if d then unbalanced_left s else s, false
  69.             else
  70.               begin match r with
  71.                 | Empty ->
  72.                     blackify l
  73.                 | _ ->
  74.                     let r, y, d = remove_min r in
  75.                     let s = Black(l, y, r) in
  76.                       if d then unbalanced_left s else s, false
  77.               end
  78.       | Red(l, y, r) ->
  79.           let c = Ord.compare x y in
  80.             if c < 0 then
  81.               let l, d = remove_aux l in
  82.               let s = Red(l, y, r) in
  83.                 if d then unbalanced_right s else s, false
  84.             else if c > 0 then
  85.               let r, d = remove_aux r in
  86.               let s = Red(l, y, r) in
  87.                 if d then unbalanced_left s else s, false
  88.             else
  89.               begin match r with
  90.                 | Empty ->
  91.                     l, false
  92.                 | _ ->
  93.                     let r, y, d = remove_min r in
  94.                     let s = Red(l, y, r) in
  95.                       if d then unbalanced_left s else s, false
  96.               end
  97.     in fst (remove_aux s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement