Construct a tree structure from list of string paths

后端 未结 6 793
名媛妹妹
名媛妹妹 2020-11-29 01:03

I have a collection of string paths like [\"x1/x2/x3\",\"x1/x2/x4\",\"x1/x5\"] in a list. I need to construct a tree-like structure from this list which can be iterated to g

6条回答
  •  青春惊慌失措
    2020-11-29 01:16

    I'd make the tree one string at a time.

    Make an empty tree (which has a root node - I assume there could be a path like "x7/x8/x9").

    Take the first string, add x1 to the root node, then x2 to x1, then x3 to x2.

    Take the second string, see that x1 and x2 are already there, add x4 to x2.

    Do this for every path you have.

提交回复
热议问题