英文:
How do you perform a deep copy of a ternary tree in Go?
问题
我正在尝试对以下结构体执行深拷贝:
// 三叉树
type Tree struct {
Left *Tree
Mid *Tree
Right *Tree
Value interface{}
Parent *Tree
Orientation string
IsTerminal bool
Type string
}
以下是我尝试的代码。看起来我在根节点创建了一个新的树,但它的子节点仍然指向相同的内存地址。
func (tree *Tree) CopyTree() *Tree {
if (tree == nil) {
return nil
} else {
copiedTree := &Tree {
tree.Left.CopyTree(),
tree.Mid.CopyTree(),
tree.Right.CopyTree(),
tree.Value,
tree.Parent.CopyTree(),
tree.Orientation,
tree.IsTerminal,
tree.Type}
return copiedTree
}
}
Go语言中是否有任何有用的构造来辅助深拷贝一个结构体?如果没有,我该如何自己执行这个深拷贝?请注意,"deepcopy"包在Go 1发布时使用了一些已被弃用的函数,因此不再可用。
英文:
I'm attempting to perform a deep copy of the following struct:
// Ternary Tree
type Tree struct {
Left *Tree
Mid *Tree
Right *Tree
Value interface{}
Parent *Tree
Orientation string
IsTerminal bool
Type string
}
The following is my sorry attempt. It looks like I'm creating a new tree at the root but it's children are still pointing to the same address in memory.
func (tree *Tree) CopyTree() *Tree {
if (tree == nil) {
return nil
} else {
copiedTree := &Tree {
tree.Left.CopyTree(),
tree.Mid.CopyTree(),
tree.Right.CopyTree(),
tree.Value,
tree.Parent.CopyTree(),
tree.Orientation,
tree.IsTerminal,
tree.Type}
return copiedTree
}
}
Are there any useful constructs in go that assist with deep copying a struct? If not, how would I perform this deep copy myself? Note, the "deepcopy" package no longer works as it uses a few functions that were deprecated with the release of Go 1
答案1
得分: 10
我离成功很近了。我应该将copiedTree赋值给parent属性。
func (tree *Tree) CopyTree() *Tree {
if (tree == nil) {
return nil
} else {
copiedTree := &Tree {
tree.Left.CopyTree(),
tree.Mid.CopyTree(),
tree.Right.CopyTree(),
tree.Value,
nil,
tree.Orientation,
tree.IsTerminal,
tree.Type,
}
if copiedTree.Left != nil {
copiedTree.Left.Parent = copiedTree
}
if copiedTree.Right != nil {
copiedTree.Right.Parent = copiedTree
}
if copiedTree.Mid != nil {
copiedTree.Mid.Parent = copiedTree
}
return copiedTree
}
}
英文:
I was close. I should have assigned the copiedTree to the parent property.
func (tree *Tree) CopyTree() *Tree {
if (tree == nil) {
return nil
} else {
copiedTree := &Tree {
tree.Left.CopyTree(),
tree.Mid.CopyTree(),
tree.Right.CopyTree(),
tree.Value,
nil,
tree.Orientation,
tree.IsTerminal,
tree.Type,
}
if copiedTree.Left != nil {
copiedTree.Left.Parent = copiedTree
}
if copiedTree.Right != nil {
copiedTree.Right.Parent = copiedTree
}
if copiedTree.Mid != nil {
copiedTree.Mid.Parent = copiedTree
}
return copiedTree
}
}
答案2
得分: 0
你可以通过encoding/gob
来进行往返转换:
package main
import (
"bytes"
"encoding/gob"
)
func copyStruct(in, out interface{}) {
buf := new(bytes.Buffer)
gob.NewEncoder(buf).Encode(in)
gob.NewDecoder(buf).Decode(out)
}
func main() {
type date struct { Month, Day int }
a := date{12, 31}
var b date
copyStruct(a, &b)
}
https://golang.org/pkg/encoding/gob
英文:
You can roundtrip it through encoding/gob
:
package main
import (
"bytes"
"encoding/gob"
)
func copyStruct(in, out interface{}) {
buf := new(bytes.Buffer)
gob.NewEncoder(buf).Encode(in)
gob.NewDecoder(buf).Decode(out)
}
func main() {
type date struct { Month, Day int }
a := date{12, 31}
var b date
copyStruct(a, &b)
}
答案3
得分: -1
json.Marshal和json.Unmarshal怎么样?如果性能很重要,我更倾向于使用protobuf。
英文:
How about json.Marshal and json.Unmarshal. If performance is critical, I perfer to use protobuf.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论