dsh8009271 2012-04-25 21:01
浏览 9
已采纳

Go-具有数组和丢失值的递归结构

I created an Element struct that has a parent and children, created a helper func called SubElement, and a String method that iterates through all children for print:

package main

import "fmt"

type Element struct {
  parent *Element
  children []Element
  tag string
}

func SubElement(parent *Element, tag string) Element {
  el := Element{}
  el.parent = parent
  el.tag = tag
  parent.children = append(parent.children, el)
  return el
}

func (el Element) String() string {
  s := "<" + el.tag + ">"
  for _, child := range el.children {
    s += child.String()
  }
  s += "</" + el.tag + ">"
  return s
}

func main() {
  root := Element{}
  root.tag = "root"

  a := SubElement(&root, "a")
  b := SubElement(&a, "b")
  SubElement(&b, "c")

  fmt.Println(root) // prints: <root><a></a></root>
  fmt.Println(a) // prints: <a><b></b></a>
  // and so on
}

The problem I'm experiencing is, only the first tier of children are available from the root node I choose to print. I'm sure it's related to the use of append on parent.children, but lack the understanding of how to resolve this correctly.

To work around the issue, I changed children to map[int]Element. Then in my SubElement func, I "append" with parent.children[len(parent.children)] = el. Then to iterate in the correct order, the String method for-loop is for i:= 0; i < len(el.children); i++, accessing el.children[i].

Still, I'd like to know how to do this correctly with an array. Thanks

  • 写回答

2条回答 默认 最新

  • dongyu5482 2012-04-25 22:43
    关注

    The first clue is that SubElement doesn't compile as you have it (edit: had it) there (originally.) You could experiment with making it work, but I recommend you change the Element.children to be []*Element rather than []Element. Here's a working example:

    package main
    
    import "fmt"
    
    func main() {
        tree := &Element{tag: "head"}
        t1 := SubElement(tree, "tier-1")
        SubElement(t1, "tier-2")
        SubElement(t1, "tier-2")
        t1 = SubElement(tree, "tier-1")
        SubElement(t1, "tier-2")
        fmt.Println(tree)
    }
    
    type Element struct {
        parent   *Element
        children []*Element
        tag      string
    }
    
    func SubElement(parent *Element, tag string) *Element {
        el := &Element{parent: parent, tag: tag}
        parent.children = append(parent.children, el)
        return el
    }
    
    func (el *Element) String() string {
        s := "<" + el.tag + ">"
        for _, child := range el.children {
            s += child.String()
        }
        s += "</" + el.tag + ">"
        return s
    }
    

    Output:

    <head><tier-1><tier-2></tier-2><tier-2></tier-2></tier-1><tier-1><tier-2></tier-2></tier-1></head>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错