我正在尝试执行以下结构的深层副本:
// Ternary Tree
type Tree struct {
Left *Tree
Mid *Tree
Right *Tree
Value interface{}
Parent *Tree
Orientation string
IsTerminal bool
Type string
}
Run Code Online (Sandbox Code Playgroud)
以下是我的抱歉尝试.看起来我在根处创建了一个新树,但它的子节点仍指向内存中的相同地址.
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
}
}
Run Code Online (Sandbox Code Playgroud)
是否有任何有用的结构可以帮助深度复制结构?如果没有,我将如何自己执行此深层复制?请注意," deepcopy "包不再有效,因为它使用了Go 1版本弃用的一些函数