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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?