Building a tree from a list of parents