# 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: []
},
]
},
]
},
]

• I don't understand what a "hierarchical list of hash maps tree" is. I don't understand how the rules specify the desired output. – D.W. Aug 14 at 1:41
• I would suggest that you take a look at the lexicographical order of your list of paths. It reveals a quite fast algorithm. – plshelp Aug 15 at 0:31