Get a tree like structure out of path string

前端 未结 1 995
独厮守ぢ
独厮守ぢ 2021-01-18 08:57

I am stuck since 2 days, as I am not to firm with pointers and recursion. I have an array of path like structures, lets say:

s:=[]string {
  \"a/b/c\",
  \"a         


        
相关标签:
1条回答
  • 2021-01-18 09:34

    https://play.golang.org/p/9pER5cwChF

    func AddToTree(root []Node, names []string) []Node {
        if len(names) > 0 {
            var i int
            for i = 0; i < len(root); i++ {
                if root[i].Name == names[0] { //already in tree
                    break
                }
            }
            if i == len(root) {
                root = append(root, Node{Name: names[0]})
            }
            root[i].Children = AddToTree(root[i].Children, names[1:])
        }
        return root
    }
    

    Example output (note that I used omitempty on the children field, because I don't like null entries in my JSONs):

    [{
        "name": "a",
        "children": [{
            "name": "b",
            "children": [{
                "name": "c"
            }, {
                "name": "g"
            }]
        }, {
            "name": "d"
        }]
    }]
    

    Notable difference from your version:

    • It operates on a list of nodes instead of the children of a single node. This is important, as your version assumes that all of the trees have the same single root node (a), when this might not be the case. The only way to handle that in your version is to have a "fake" node at the root.
    • It does NOT reuse the input node. This is one of the primary problems with your code. If len(children) > 1, you update the input node's name, append to it's children, then recurse. This means that every prior level of the slice becomes part of the children. You need to create a new node instead.
    • It actually searches the tree. You're not searching the tree to see if the item being inserted already exists, so you duplicate nodes (specifically, node b)
    0 讨论(0)
提交回复
热议问题