Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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#_Depth First Search - Fatal编程技术网

C# 无堆栈深度优先搜索

C# 无堆栈深度优先搜索,c#,depth-first-search,C#,Depth First Search,我开始用stack编写代码,如下所示: void Foo(TreeNode root) { Stack nodes = new Stack(); nodes.Push(root); while (nodes.Count > 0) { TreeNode node = (TreeNode) nodes.Pop(); Console.WriteLine(node.Text);

我开始用stack编写代码,如下所示:

void Foo(TreeNode root)
    {
        Stack nodes = new Stack();
        nodes.Push(root);

        while (nodes.Count > 0)
        {
            TreeNode node = (TreeNode) nodes.Pop();
            Console.WriteLine(node.Text);
            for (int i = node.Nodes.Count - 1; i >= 0; i--)
                nodes.Push(node.Nodes[i]);
        }
    }
但是,没有堆栈,我不知道我应该做什么

我试过这个。对吗? 有人能推荐我吗

void Foo(TreeNode root)
{
   if(root == null) return;

    System.out.print(root.Value + "\t");
    root.state = State.Visited;

    //for every child
    for(Node n: root.getChild())
    {
        //if childs state is not visited then recurse
        if(n.state == State.Unvisited)
        {
            dfs(n);
        }
    }
}
使用:

Console.WriteLine(node.Text);
对于(Int i=0;i
命名空间控制台应用程序3
{
使用System.Collections.Generic;
公共类树
{
公共int值{get;set;}
公共列表树节点
{
收到
设置
}
公树()
{
this.TreeNode=新列表();
}
}
公共课程
{
公共静态void Main()
{
Program pro=新程序();
树=新树();
添加(新树(){Value=1});
添加(新树(){Value=2});
添加(新树(){Value=3});
添加(新树(){Value=4});
专业深度优先搜索(2,树);
}
私有树DepthFirstSearch(int searchValue,树根)
{
if(searchValue==root.Value)
{
返回根;
}
Tree treeFound=null;
foreach(root.TreeNode中的变量树)
{
treeFound=DepthFirstSearch(searchValue,tree);
if(treeFound!=null)
{
打破
}
}
返回树基金;
}
}
}

您有什么具体问题?请参阅和寻求帮助。另外,请提醒一位老人在本文中“DFS”是什么意思?是的,您刚刚发布了一些Java代码…@JohnSaunders我很好。逻辑正确吗?我们缺少很多上下文。您正在使用的数据结构是什么?你为什么不想使用堆栈呢?你需要做一个迭代算法还是递归算法?您的数据是树还是图形?很多很多问题。。。
Console.WriteLine(node.Text);
for (Int i = 0; i < node.Nodes.Count; i++)
    Foo(root.Nodes[i]);
namespace ConsoleApplication3
{
    using System.Collections.Generic;

    public class Tree
    {
        public int Value { get; set; }

        public List<Tree> TreeNode
        {
            get;
            set;
        }
        public Tree()
        {
            this.TreeNode = new List<Tree>();
        }
    }

    public class Program
    {
        public static void Main()
        {
            Program pro = new Program();
            Tree tree = new Tree();
            tree.TreeNode.Add(new Tree() { Value = 1 });
            tree.TreeNode.Add(new Tree() { Value = 2 });
            tree.TreeNode.Add(new Tree() { Value = 3 });
            tree.TreeNode.Add(new Tree() { Value = 4 });
            pro.DepthFirstSearch(2, tree);
        }

        private Tree DepthFirstSearch(int searchValue, Tree root)
        {
            if (searchValue == root.Value)
            {
                return root;
            }

            Tree treeFound = null;
            foreach (var tree in root.TreeNode)
            {
                treeFound = DepthFirstSearch(searchValue, tree);
                if (treeFound != null)
                {
                    break;
                }
            }

            return treeFound;

        }
    }
}