raedr7n

Breadth-first traversal of a binary tree

Nov 7th, 2021 (edited)
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.40 KB | None | 0 0
  1. type 'a bin_tree =
  2.   Node of 'a bin_tree * 'a * 'a bin_tree | Nil ;;
  3.  
  4. let bf_iter f = function
  5.     Nil -> ()
  6.   | root ->
  7.       let q = ref [root] in
  8.       while !q <> [] do
  9.         let this = match !q with h :: _ -> h | _ -> Nil
  10.         in let _ = q := match !q with _ :: t -> t | _ -> []
  11.         in match this with
  12.           Node (l, e, r) -> f e; q := !q @ [l; r]
  13.         | _ -> ()
  14.       done ;;
Add Comment
Please, Sign In to add comment