Advertisement
CloudRoutine

Untitled

Apr 12th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     type BTree =
  2.         | Node of   v1:     int         *
  3.                     v2:     int option  *
  4.                     parent: BTree ref   *
  5.                     left:   BTree ref   *
  6.                     mid:    BTree ref   *
  7.                     right:  BTree ref
  8.         | Leaf of   v1:     int         *
  9.                     v2:     int option  *
  10.                     parent: BTree ref
  11.         | Empty
  12.  
  13.  
  14.     let median (a:int) (b:int) (c:int) =
  15.         Array.sort [|a;b;c|]
  16.  
  17.  
  18.     let mid l r x =
  19.         (l < x) && (x < r)
  20.  
  21.  
  22.     let rec insert (tref:BTree ref) (x:int) : BTree =
  23.         let tree = !tref
  24.         match tree with
  25.         | Node(a,None,p,l,m,r)
  26.                         when !l = Empty
  27.                         &&   x < a      ->  Node(x,Some a,p,l,m,r)
  28.         | Node(a,b,p,l,m,r)      
  29.                         when x < a      ->  l := insert l x
  30.                                             Node(a,b,p,l,m,r)
  31.         | Node(a,None,p,l,m,r)
  32.                         when !r = Empty
  33.                         &&   x > a      ->  Node(a,Some x,p,l,m,r)
  34.        
  35.         | Node(a,b,p,l,m,r)      
  36.                         when x > a      ->  r := insert r x
  37.                                             Node(a,b,p,l,m,r)
  38.         | Node(a,Some b,p,l,m,r)
  39.                         when !l = Empty
  40.                         &&   !m = Empty  
  41.                         &&   !r = Empty ->  let m = median a b x
  42.                                             let o = ref Empty
  43.                                             l := Leaf(m.[0],None,o)
  44.                                             r := Leaf(m.[2],None,o)
  45.                                             o := Node(m.[1],None,p,l,ref Empty,r)
  46.                                             !o
  47.         | Node(a,Some b,p,l,m,r)
  48.                         when !l = Empty
  49.                         &&   x < a      ->  let parent = ref tree
  50.                                             l := Leaf(x,None,parent)
  51.                                             Node(a,Some b,p,l,m,r)  
  52.         | Node(a,Some b,p,l,m,r)
  53.                         when !r = Empty
  54.                         &&   x > b      ->  let parent = ref tree
  55.                                             r := Leaf(x,None,parent)
  56.                                             Node(a,Some b,p,l,m,r)
  57.         | Node(a,Some b,p,l,m,r)            
  58.                         when !m = Empty
  59.                         &&   mid a b x  ->  let parent = ref tree
  60.                                             m := Leaf(x,None,parent)
  61.                                             Node(a,Some b,p,l,m,r)
  62.         | Node(a,Some b,p,l,m,r)
  63.                         when mid a b x  ->  m := insert m x
  64.                                             Node(a,Some b,p,l,m,r)
  65.         | Leaf(a,None,p)                
  66.                         when x < a      ->  Leaf(x,Some a,p)
  67.         | Leaf(a,None,p)                
  68.                         when x > a      ->  Leaf(a,Some x,p)
  69.         | Leaf(a,Some b,p)              ->  rebuild tref x
  70.         | Empty                         ->  failwith "Cannot insert into empty"
  71.         | _                             ->  failwith "Invalid Tree"
  72.     and rebuild (tref:BTree ref) (x:int) : BTree =
  73.         let tree = !tref
  74.         match tree with
  75.         | Node(a,None,p,l,m,r)          
  76.                         when !m = Empty
  77.                         && !r = Leaf(x,)
  78.                         &&   x > a      ->  Empty                        
  79.         | Node(a,None,p,l,m,r)          
  80.                         when x > a      ->  Node(a,Some x,p,l,m,r)
  81.         | Node(a,Some b,parent,l,m,r)  
  82.                         when !m = Empty ->  let m = median a b x
  83.                                             let p = ref Empty
  84.                                             let lt = ref (Node(m.[0],None,p,l,ref Empty,ref Empty))
  85.                                             let rt = ref (Node(m.[2],None,p,ref Empty,ref Empty,r))
  86.                                             p := rebuild parent m.[1]
  87.                                             !p
  88.         | Node(a,b,p,l,m,r)        
  89.                         when !m = Empty
  90.                                 x < a   ->  m:= Leaf(x,None,tree)
  91.                                             Node(a,b,p,l,child,r)
  92.         | Leaf(a,Some(b),parent)        ->  let m = median a b x
  93.                                             let l = ref (Leaf(m.[0],None,ref Empty))    
  94.                                             let r = ref (Leaf(m.[2],None,ref Empty))
  95.                                             let p = ref (rebuild parent m.[1])
  96.                                             Node(m.[1],None,p,l,ref Empty,r)
  97.         | Node(a,b,p,l,m,Empty)        
  98.                         when x > a      ->  let child = Leaf(x,None,tree)
  99.                                             Node(a,b,p,l,m,child)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement