Convert Flat data structure to hierarchical data