What is the most efficient way to turn a list of directory path strings into a tree?


I’m trying to find out the most efficient way of turning a list of path strings into a hierarchical list of hash maps tree using these rules:

  • Node labels are delimited/split by ‘/’
  • Hash maps have the structure:
{     label: "Node 0",     children: [] } 
  • Node labels are also keys, so for example all nodes with the same label at the root level will be merged

So the following code:

[     "Node 0/Node 0-0",     "Node 0/Node 0-1",     "Node 1/Node 1-0/Node 1-0-0" ] 

Would turn into:

[     {         label: "Node 0",         children: [             {                 label: "Node 0-0",                 children: []             },             {                 label: "Node 0-1",                 children: []             },         ]     },     {         label: "Node 1",         children: [             {                 label: "Node 1-0",                 children: [                     {                         label: "Node 1-0-0",                         children: []                     },                 ]             },         ]     }, ]