Assume we have hierarchy which consists from several objects, like this:

And I want to copy this tree (and maybe somehow change those objects).
A simple method to do this is just iterate between objects and create it one-by-one. But performance here is very poor. In addition, i do not like loops 😉
So question – is it possible to do with set-based logic?
Using the technique described in this question, you could do something like this:
It is assumed that you only want to copy one ‘tree’ at a time.
All this should probably be done in a single transaction.