Advertisement
malinaX

leftist Mateusz Malinowski

Nov 22nd, 2020
3,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.88 KB | None | 0 0
  1. (* Autor: Mateusz Malinowski, reviewer: Tomasz Nowak*)
  2. type 'a queue = Null | El of 'a queue * 'a * 'a queue * int;; (* int trzyma wysokosc *)
  3.  
  4. let empty = Null;;
  5.  
  6. let is_empty q = if q = Null then true else false;;
  7.  
  8. let get_height (El (_, _, _, h)) = h;;
  9.  
  10. let rec join q1 q2 =
  11.     match (q1, q2) with
  12.     | (x, Null) -> x
  13.     | (Null, x) -> x
  14.     | (El (l1, a1, r1, h1), El (_, a2, _, _)) ->
  15.         if a1 > a2 then
  16.             join q2 q1
  17.         else
  18.             let q = join r1 q2 in
  19.             let h = get_height q in (* q2 nie jest Null wiec q tez nie jest Null *)
  20.             if h > h1 - 1 then
  21.                 El (q, a1, l1, h + 1)
  22.             else
  23.                 El (l1, a1, q, h1);;
  24.  
  25. let add e q = join q (El (Null, e, Null, 1));;
  26.  
  27. exception Empty;;
  28.  
  29. let delete_min q =
  30.     match q with
  31.     | Null -> raise Empty
  32.     | El (l, a, r, _) -> (a, join l r);;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement