Complete paths-to-tree algorithm