Guest User

Same fringe

a guest
Apr 21st, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.86 KB | None | 0 0
  1. type 'a tree = Leaf | Branch of 'a * 'a tree * 'a tree
  2.  
  3. type 'a fringe_iterator = 'a tree list
  4.  
  5. let rec collect stack t = match t with
  6.   | Leaf -> stack
  7.   | Branch (x, Leaf, Leaf) -> t::stack
  8.   | Branch (x, left, Leaf) -> collect stack left
  9.   | Branch (x, left, right) -> collect (right::stack) left
  10.  
  11. let fringe_iter tree =
  12.   collect [] tree
  13.  
  14. let fringe_next = function
  15.   | [] -> None, []
  16.   | x::xs ->
  17.       match x with
  18.        | Branch (x, Leaf, Leaf) ->
  19.            Some x, (match xs with [] -> [] | y::ys -> collect ys y)
  20.        | _ -> failwith "bad iterator"
  21.  
  22. let same_fringe a b =
  23.   let rec cmp aiter biter =
  24.     match fringe_next aiter, fringe_next biter with
  25.      | (None, _), (None, _) -> true
  26.      | (Some x, aiter'), (Some y, biter') ->
  27.          if x <> y then false else cmp aiter' biter'
  28.      | _ -> false in
  29.   cmp (fringe_iter a) (fringe_iter b)
Advertisement
Add Comment
Please, Sign In to add comment