Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
java中迭代深化深度优先搜索的几个问题_Java_Search_Tree_Iterative Deepening - Fatal编程技术网

java中迭代深化深度优先搜索的几个问题

java中迭代深化深度优先搜索的几个问题,java,search,tree,iterative-deepening,Java,Search,Tree,Iterative Deepening,我正在查看维基百科上的伪代码,并试图用它来用java编写算法。我的问题是如何返回结果。在wikipedia上,返回一个结果,这将中断搜索。在mine中,每次找到相关节点时,都会将其添加到列表中,并在处理完树后返回该列表。我将如何检测树的结尾,以便断开并返回列表 维基百科: IDDFS(root, goal) { depth = 0 repeat { result = DLS(root, goal, depth) if (result is a solution)

我正在查看维基百科上的伪代码,并试图用它来用java编写算法。我的问题是如何返回结果。在wikipedia上,返回一个结果,这将中断搜索。在mine中,每次找到相关节点时,都会将其添加到列表中,并在处理完树后返回该列表。我将如何检测树的结尾,以便断开并返回列表

维基百科:

IDDFS(root, goal)
{
  depth = 0
  repeat
  {
    result = DLS(root, goal, depth)
    if (result is a solution)
      return result
    depth = depth + 1
  }
}

DLS(node, goal, depth) 
{
  if (depth == 0 and node == goal)
    return node
  else if (depth > 0)
    for each child in expand(node)
      DLS(child, goal, depth-1)
  else
    return null
}
地雷:

    public List<String> dfid(Tree t, String goal)
    {
        List<String> results = new ArrayList<String>();
        String result;

        int depth = 0;
        while (true) //obviously not the way to go here
        {
            result = dls(t.root, goal, depth);
            if (result.contains(goal))
                results.add(result);
            depth += 1;
        }
        return results; //unreachable return
    }

    public String dls(Node node, String goal, int depth)
    {
        if (depth == 0 && node.data.contains(goal))
        {
            return node.data;
        }
        else if (depth > 0)
        {
            for(Node child : node.children)
            {
                dls(child, goal, depth-1);
            }
        }
        return null;
    }
公共列表dfid(树t,字符串目标)
{
列表结果=新建ArrayList();
字符串结果;
int深度=0;
while(true)//显然不是这里要走的路
{
结果=dls(t.根、目标、深度);
if(result.contains(目标))
结果。添加(结果);
深度+=1;
}
返回结果;//无法到达的返回
}
公共字符串DLL(节点、字符串目标、整数深度)
{
if(深度==0&&node.data.contains(目标))
{
返回node.data;
}
否则,如果(深度>0)
{
for(节点子节点:Node.children)
{
dls(儿童、目标、深度-1);
}
}
返回null;
}
编辑:更改后:

//depth first iterative deepening
        //control variables for these methods
        boolean maxDepth = false;
    List<String> results = new ArrayList<String>();

    public List<String> dfid(Tree t, String goal)
    {
        int depth = 0;

        while (!maxDepth)
        {
            maxDepth = true;
            dls(t.root, goal, depth);
            depth += 1;
        }
        return results;
    }

    public void dls(Node node, String goal, int depth)
    {
        if (depth == 0 && node.data.contains(goal))
        {
            //set maxDepth to false if the node has children
            maxDepth = maxDepth && children.isEmpty();
            results.add(node.data);
        }
        for(Node child : node.children)
        {
            dls(child, goal, depth-1);
        }
    }
//深度优先迭代深化
//这些方法的控制变量
布尔maxDepth=false;
列表结果=新建ArrayList();
公共列表dfid(树t,字符串目标)
{
int深度=0;
而(!maxDepth)
{
maxDepth=true;
dls(t.根、目标、深度);
深度+=1;
}
返回结果;
}
公共void dls(节点、字符串目标、整数深度)
{
if(深度==0&&node.data.contains(目标))
{
//如果节点有子节点,则将maxDepth设置为false
maxDepth=maxDepth&&children.isEmpty();
结果.添加(节点.数据);
}
for(节点子节点:Node.children)
{
dls(儿童、目标、深度-1);
}
}

我认为您可以使用一个
布尔maxDepth=false
实例变量来实现这一点。在while循环的每次迭代中,如果
maxDepth==true
,则退出,否则设置
maxDepth=true
。在
dls
中,当达到
depth==0
时,设置
maxDepth=maxDepth&&children.isEmpty()
,即如果节点有任何子节点,则将maxDepth设置为false

另外,将
dls
更改为void方法。用
结果替换
返回节点.数据
。添加(节点.数据)
,其中
结果
是一个
数组列表
哈希集
,具体取决于您是否要筛选出重复项

如果您始终希望访问树中的每个节点,请按如下所示修改
dls

public void dls(ArrayList<String> results, Node node, String goal)
{
    if (node.data.contains(goal))
    {
        results.add(node.data);
    }
    for(Node child : node.children)
    {
        dls(child, goal, depth-1);
    }
}
public void dls(数组列表结果、节点、字符串目标)
{
if(node.data.contains(目标))
{
结果.添加(节点.数据);
}
for(节点子节点:Node.children)
{
dls(儿童、目标、深度-1);
}
}

在结果出现的第一个实例中,这是否仍然会中断?我想收集树中的所有结果,请参阅我的编辑-将结果放入集合中,而不是返回结果。嗯,好的。所以基本上这个搜索在技术上不会返回任何东西,它只会在其他正确的地方填充一组数据。您可以将结果集合作为实例变量,也可以通过这种方式将其作为另一个参数传递给
dls
,是否仍然需要
maxdepth