My tree looks something like this:

So, I am trying to copy root using node as follows:
public void copy() {
List<Trees> nodes = findNodes(oldIdRoot);
Integer newIdRoot = method.add();
copyNodes(nodes, newIdRoot, oldIdRoot);
}
private void copyNodes(List<Trees> nodes, Integer newIdRoot, oldIdRoot) {
for (Trees trees : nodes) {
Integer nId= method.add(trees, newIdRoot);
}
}
private List<Trees> findNodes(Integer oldIdRoot) {
.......
List<Trees> foundedNodes = new ArrayList<Trees>();
foundedNodes.add(...));
return foundedNodes;
}
After that receives:

So, everything is right finished.
Now I'm trying to copy the whole tree. Therefore, I stand on the root - 1and press the copy button, the result:

but I expect the result:

The solution to this problem is probably a recursive copy. I see many examples of this, but I don’t understand how to do this in my solution. Can someone help me with this problem and show me an example of how a recursive copy works?
Adamo source
share