Skip to content

Instantly share code, notes, and snippets.

@TheOpenDevProject
Created December 25, 2015 11:41
Show Gist options
  • Save TheOpenDevProject/37a9d4b4772b15c26faf to your computer and use it in GitHub Desktop.
Save TheOpenDevProject/37a9d4b4772b15c26faf to your computer and use it in GitHub Desktop.
functor BFS (structure Q: QUEUE) = (* after Okasaki, ICFP, 2000 *)
struct
datatype 'a tree
= E
| T of 'a * 'a tree * 'a tree
fun bfsQ (q : 'a tree Q.queue) : 'a list =
if Q.isEmpty q then []
else let
val (t, q') = Q.remove q
in case t
of E => bfsQ q'
| T (x, l, r) => let
val q'' = Q.insert (r, Q.insert (l, q'))
in
x :: bfsQ q''
end
end
fun bfs t = bfsQ (Q.singleton t)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment