Code for building a single tree-like data structure with unique branches from an array of trees with one to many branches