I'm looking to convert a tree using one type to node to another type, and I've been stuck on this for longer than I wish to admit.
So lets say the original tree looks like this:
Node: String, Children (Node)
I want to clone this tree but instead have:
Node2: String, Children (Node2), String
I'm stuck on how I can recursively go through the initial tree and then clone all the nodes to be using Node2 parameters.
Any help would be greatly appreciated!
Update: Used the answer provided by @tgdavies and ended up with:
private fun cloneTree(
nodeA: Node_1,
createB: BiFunction<Node_1, List<Node_2>?, Node_2>
): Node_2 {
val children: List<Node_2> = nodeA.children
.stream()
.map { anA -> cloneTree(anA, createB) }
.collect(Collectors.toList())
return createB.apply(nodeA, children)
}