Construct a tree structure from list of string paths

后端 未结 6 780
名媛妹妹
名媛妹妹 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:28

    Make your tree for every string in array. Just split path for '/' , check whether the node exists in your tree or not, if it exists then move on... otherwise create a new node and add this node in childrens of parent node.

    Iterate using recursion.

    Following is model for tree's node.

    Class Node{
        string name;
        List childrens;
    
        Node(string name){
            this.name = name;
            this.childrens = new List();
        }
    }
    

提交回复
热议问题