Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 递归树搜索返回错误的结果_C#_Algorithm_Recursion - Fatal编程技术网

C# 递归树搜索返回错误的结果

C# 递归树搜索返回错误的结果,c#,algorithm,recursion,C#,Algorithm,Recursion,我有一个树递归函数。它获取从A到D的所有可能路径 递归函数是: private static List<Path> GetPath(int depth, Path path) { if (depth == nodes.Length) { return new List<Path> { path }; } else { var result = new List<Path>();

我有一个树递归函数。它获取从A到D的所有可能路径

递归函数是:

private static List<Path> GetPath(int depth, Path path)
{
    if (depth == nodes.Length) 
    {
        return new List<Path> { path };
    }
    else 
    {
        var result = new List<Path>();
        foreach(var link in nodes[depth].Links)
        {
            Node node = new Node { Name = nodes[depth].Name, Links = new[] { link } };
            path.Add(node);

            result.AddRange(
                GetPath(
                    depth+1, path));
        }

        return result;
    }
}
但是,返回的路径是相同的,它们两次包含所有可能的节点

这个函数有什么问题

foreach(var link in nodes[depth].Links)
{
    Node node = new Node { Name = nodes[depth].Name, Links = new[] { link } };
    path.Add(node);

在添加下一个节点之前,您可能打算为此处找到的每个节点创建一个新路径(即
路径的副本)。

正如@moreON所建议的,我将克隆函数添加到类路径中,并修改循环,在循环中我复制到路径的新实例:

public class Path : List<Node>
{
    public override string ToString()
    {
        String s = "";

        foreach (var node in this)
        {
            s += node.Name + node.Links[0] + "->";
        }
        return s;
    }

    public Path Clone()
    {
        var newPath = new Path();
        ForEach(x => newPath.Add(new Node {Name = x.Name, Links = new int[] {x.Links[0]}}));
        return newPath;
    }
}

    private static List<Path> GetPath(int depth, Path path)
    {
        if (depth == nodes.Length)
        {
            return new List<Path> { path };
        }
        else
        {
            var result = new List<Path>();

            foreach (var link in nodes[depth].Links)
            {
                Node node = new Node { Name = nodes[depth].Name, Links = new[] { link } };
                var currentPath = path.Clone();
                currentPath.Add(node);

                result.AddRange(GetPath(depth + 1, currentPath));
            }
            return result;
        }
    }
公共类路径:列表
{
公共重写字符串ToString()
{
字符串s=“”;
foreach(本例中的var节点)
{
s+=node.Name+node.Links[0]+“->”;
}
返回s;
}
公共路径克隆()
{
var newPath=新路径();
ForEach(x=>newPath.Add(新节点{Name=x.Name,Links=newint[]{x.Links[0]}));
返回新路径;
}
}
私有静态列表GetPath(整型深度,路径)
{
if(深度==节点.长度)
{
返回新列表{path};
}
其他的
{
var result=新列表();
foreach(节点[depth].Links中的变量链接)
{
Node Node=new Node{Name=nodes[depth].Name,Links=new[]{link};
var currentPath=path.Clone();
添加(节点);
AddRange(GetPath(深度+1,currentPath));
}
返回结果;
}
}

希望有帮助。

请将代码的相关部分粘贴到此处。
public class Path : List<Node>
{
    public override string ToString()
    {
        String s = "";

        foreach (var node in this)
        {
            s += node.Name + node.Links[0] + "->";
        }
        return s;
    }

    public Path Clone()
    {
        var newPath = new Path();
        ForEach(x => newPath.Add(new Node {Name = x.Name, Links = new int[] {x.Links[0]}}));
        return newPath;
    }
}

    private static List<Path> GetPath(int depth, Path path)
    {
        if (depth == nodes.Length)
        {
            return new List<Path> { path };
        }
        else
        {
            var result = new List<Path>();

            foreach (var link in nodes[depth].Links)
            {
                Node node = new Node { Name = nodes[depth].Name, Links = new[] { link } };
                var currentPath = path.Clone();
                currentPath.Add(node);

                result.AddRange(GetPath(depth + 1, currentPath));
            }
            return result;
        }
    }