A recursive script would likely be needed.  If you have the source data in a flat format, then the technique in this post might help, by filtering (with where()) then generating the tree:
A recursive script would likely be needed.  If you have the source data in a flat format, then the technique in this post might help, by filtering (with where()) then generating the tree: