0
$\begingroup$

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: []
                    },
                ]
            },
        ]
    },
]
$\endgroup$
  • $\begingroup$ I don't understand what a "hierarchical list of hash maps tree" is. I don't understand how the rules specify the desired output. $\endgroup$ – D.W. Aug 14 at 1:41
  • $\begingroup$ I would suggest that you take a look at the lexicographical order of your list of paths. It reveals a quite fast algorithm. $\endgroup$ – plshelp Aug 15 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.