Jarkko Miettinen
12/11/2023, 2:47 PMSequenceScope
is restricted suspension scope and thus I cannot use the recursive approach defined there.
In short, I'd like to turn this recursive approach into a iterator without manually writing the stack handling for such an iterator.
Is there a way to do this?Sam
12/11/2023, 2:48 PMJarkko Miettinen
12/11/2023, 2:49 PMSam
12/11/2023, 2:50 PMSam
12/11/2023, 2:51 PMSam
12/11/2023, 2:58 PMfun <T : Comparable<T>> inOrderTraversal(root: Node<T>?): Sequence<T> = sequence {
suspend fun <T: Comparable<T>> SequenceScope<T>.walk(node: Node<T>?) {
if (node == null) return
when (node) {
is InternalNode -> {
walk(node.left)
yield(node.value)
walk(node.right)
}
is Leaf -> yield(node.value)
}
}
walk(root)
}
Jarkko Miettinen
12/11/2023, 2:58 PM