【问题标题】:How to merge two trees in golang ?如何在golang中合并两棵树?
【发布时间】:2016-07-04 17:13:56
【问题描述】:

树结构如下:

 type TreeData struct {
    Name          string      `json:"name"`
    Depth         int         `json:"depth"`
    Children      []TreeData  `json:"children"`
}

我有两棵树,我想把它们合并成一棵树。 我该怎么做?
如果有人可以向我展示您的代码,我将不胜感激!
不知道能不能用递归的方式来完成合并?

树一:

{
     "name": ".",
     "depth": 1,
     "children": [
         {
             "name": "com",
             "depth": 2,
             "children": [
                 {
                     "name": "didi"
                     "depth": 3,
                     "children": [
                         {
                             "name": "dev",
                             "depth": 4,
                             "children": null
                         }
                     ]
                 }
             ]
         }
     ]
}

树二:

{
     "name": ".",
     "depth": 1,
     "children": [
         {
             "name": "com",
             "depth": 2,
             "children": [
                 {
                     "name": "didi"
                     "depth": 3,
                     "children": [
                         {
                             "name": "influxdb",
                             "depth": 4,
                             "children": [
                                 {
                                     "name": "cluster-one"
                                     "depth": 5
                                     "children": null
                                 }
                             ]    
                         } 
                     ]
                 }
             ]
         }
     ]
}

合并:

{
     "name": ".",
     "depth": 1,
     "children": [
         {
             "name": "com",
             "depth": 2,
             "children": [
                 {
                     "name": "didi"
                     "depth": 3,
                     "children": [
                         {
                             "name": "influxdb",
                             "depth": 4,
                             "children": [
                                 {
                                     "name": "cluster-one"
                                     "depth": 5
                                     "children": null
                                 }
                             ]
                         },
                         {
                            "name": "dev",
                            "depth": 4,
                            "children": null
                         }
                     ]
                 }
             ]
         }
     ]
}

【问题讨论】:

  • 先给我们看一些代码。
  • 我不知道该怎么做......

标签: go tree


【解决方案1】:

我为 golang 创建树找到了一个很好的解决方案!! http://blog.csdn.net/xtxy/article/details/50812392

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 2014-12-06
    • 2021-12-26
    • 2021-09-08
    • 2014-06-28
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    相关资源
    最近更新 更多