Skip to content

Instantly share code, notes, and snippets.

@sumew
Created April 13, 2020 20:17
Show Gist options
  • Save sumew/aa7773ac5747073d625e670a7fbd9aee to your computer and use it in GitHub Desktop.
Save sumew/aa7773ac5747073d625e670a7fbd9aee to your computer and use it in GitHub Desktop.
case class Tree[T](value: T, children: List[Tree[T]])
/**
*
* @param tree
* @param f A function to be applied to every node in the tree
* @tparam S Output type
* @tparam T Value type of node
* @return A Queue[S] where the first element is the output of first node visited
*
* 1
* / \
* 4 2
* / / \
* 3 8 5
* / \ / \
* 10 9 6 7
*
*/
def include[S, T](value: T, acc: (Queue[S], Set[T]), f: T => S) = {
val (out, vis) = acc
if (!vis(value))
(out.enqueue(f(value)), vis + value)
else
(out, vis)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment