C# 如何克隆整棵树而不影响原始树?

C# 如何克隆整棵树而不影响原始树?,c#,C#,我在这个链接中使用的解决方案不起作用 下面的代码,不能复制到新的树上,变异会影响原来的树根(root) foreach(行李清单中的行李b) { 如果(b.cards.Count()==2) { 节点原始根=新节点(); 原始_root=(节点)root.Clone();//复制到新树 target\u list=选择有效的\u目标(target\u list,b.cards,b.cards.Count()-1); foreach(目标列表中的目标t) { 控制台写入线(“op:+t.op+”

我在这个链接中使用的解决方案不起作用

下面的代码,不能复制到新的树上,变异会影响原来的树根(root)

foreach(行李清单中的行李b)
{
如果(b.cards.Count()==2)
{
节点原始根=新节点();
原始_root=(节点)root.Clone();//复制到新树
target\u list=选择有效的\u目标(target\u list,b.cards,b.cards.Count()-1);
foreach(目标列表中的目标t)
{
控制台写入线(“op:+t.op+”,标签:+t.label);
Node mutated_root=mutate(原始_root,目标_列表);
结果=”;
结果=顺序(变异的_根,结果);//获取顺序
Console.WriteLine(result.Substring(1,result.Length-2));//打印
}
}
}
公共类节点:System.Object,可克隆
{
公共优先权;
/*
7字母表
6 (,)
5 *,/,%
4 +,-
3 =
2 &&,||
1 =
*/
公共字符串数据;
公共节点左;
公共节点权;
公共字符串标签;
公共对象克隆()
{
//将此作为对象返回;
返回此.MemberwiseClone();
//将新节点()作为对象返回;
}
}
公共节点mutate(节点根,列表目标)//mutate
{
if(root!=null)
{
if(!IsLeaf(root.Left.Data))
变异(根、左、目标);
foreach(目标中的目标)
{
if(root.Label==target.Label)
{
root.Data=target.op;
}
}
if(!IsLeaf(root.Right.Data))
突变(根、右、目标);
}
返回根;
}

递归克隆所有节点:

public object Clone()
{
    Node clone = (Node)MemberwiseClone();
    if (Left != null)
        clone.Left = (Node)Left.Clone();
    if (Right != null)
        clone.Right = (Node)Right.Clone();
    return clone;
} 

递归克隆所有节点:

public object Clone()
{
    Node clone = (Node)MemberwiseClone();
    if (Left != null)
        clone.Left = (Node)Left.Clone();
    if (Right != null)
        clone.Right = (Node)Right.Clone();
    return clone;
} 

clone.Left=(节点)Left.clone()处的空引用;clone.Left=(节点)Left.clone()处的空引用;