douxiji8707 2019-07-10 21:50
浏览 90

递归创建嵌套结构

I have a struct of the following format

type Node struct {
    Id string
    Children []*Node
}

Input

I have the following input

var nestedSlice = [][]string{
        {"60566", "605", "6056"},
        {"60566", "605", "6061"},
        {"60566", "605", "6065"},
        {"60653", "606", "6109"},
        {"60566", "603", "6065"},
    }

The code

package main

import (
    //"fmt"
    "github.com/davecgh/go-spew/spew"
)

type Node struct {
    Id       string
    Type     string
    Children []*Node
}

func createNode(values []string, node *Node) *Node {

}

func insert(values []string, nodes []*Node) []*Node {
    if len(nodes) == 0 {
        rootNode := createNode(values, &Node{})
        nodes = append(nodes, rootNode)
        return nodes
    } else {
        for _, node := range nodes {
            if node.Id == values[0] {
                return insert(values[1:], node.Children)
            }
        }
        anotherRoot := &Node{
            Id: values[0],
        }
        nodes = append(nodes, anotherRoot)
    }
    return nodes
}

func main() {
    nodes := make([]*Node, 0, 6)

    var nestedSlice = [][]string{
        {"60566", "605", "6056"},
        {"60566", "605", "6061"},
        {"60566", "605", "6065"},
        {"60653", "606", "6109"},
        {"60566", "603", "6065"},
    }

    for _, value := range nestedSlice {
        nodes = insert(value, nodes)
    }

    spew.Dump(nodes)
}

I am having troubles with createNode function. I am not sure how can I take the slice and create it using recursion

I want to be able to have the following struct in the end

[{
    Id: 60566,
    Children: [{
        Id: 605,
        Children: [{
            Id: 6056
        }, {
            Id: 6061
        }, {
            Id: 6065
        }]
    }, {
        Id: 603,
        Children: [{
            Id: 6065
        }]
    }]
}, {
    Id: 60653
    Children: [{
        Id: 606,
        Children: [{
            Id: 6109
        }]
    }]
}]




  • 写回答

1条回答 默认 最新

  • dongwen7283 2019-07-10 23:22
    关注

    This would be my approach, I'll create the Node and then add a separate method to add children.

    func createNode(id string) *Node{
       return &Node{
          id: id,
          children: make([]*Node, 0)
       }
    }
    

    And now add the nodes.

    func(n *Node) addChildren(nodes []*Node){
       for _, node := range nodes{
         n.children.append(node)
       }
    }
    

    But then if you wish to, you could combine the two.

    func createNode(id string, nodes []*Node) *Node{
       p= Node{
          id: id,
          children: make([]*Node, 0)
       }
       for _, node := range nodes{
         p.children.append(node)
       }
       return &p
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。