我正在尝试执行以下结构的深层副本:
// Ternary Tree
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发行版中已弃用的一些功能
我离得很近。我应该将copyedTree分配给父属性。
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
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句