-
-
Save sumew/aa7773ac5747073d625e670a7fbd9aee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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