Java中的横向第一搜索

Java中的横向第一搜索,java,tree,depth,Java,Tree,Depth,我使用这种构造函数在Java中创建了一个树: public class Node { private List<Node> children = null; private String value; public Node(String value) { this.children = new ArrayList<>(); this.value = value; } public voi

我使用这种构造函数在Java中创建了一个树:

public class Node
{
    private List<Node> children = null;
    private String value;

    public Node(String value)
    {
        this.children = new ArrayList<>();
        this.value = value;
    }

    public void addChild(Node child)
    {
        children.add(child);
    }

}
但是,使用此arraylist,我无法创建*和IDA*

状态构造函数包含一个ID和一个父ID字段。
因此,在找到解决方案后,我会回溯状态直到根状态,以确定必须放置工件的位置。

如果这是深度优先:

public void depthFirst(Node node, int op){
    if (op == 1) operationsNoTrim(node); //child generator method
    else if (op == 2) operationsWithTrim(node); //child generator method
    else if (op == 3) operationsWithTrimNSecure(node); //child generator method
    for(int i = 0; i < node.getTotalChild(); i++){
        depthFirst(node.getChild(i), op);
    }
}
public void depthFirst(节点,int op){
if(op==1)operationsNoTrim(节点);//子生成器方法
else if(op==2)操作WithTrim(node);//子生成器方法
else if(op==3)带有TrimnSecure(节点)的操作;//子生成器方法
for(int i=0;i
然后这将是您的广度优先(从包含根的列表开始)

public void-breadthFirst(数组列表节点,int-op){
ArrayList子项=新的ArrayList();
//节点包含上一级(或父级)的节点
对于(int i=0;i
没有递归:

public void breadthFirst(Node root, int op) {
    ArrayList<Node> nodes = new ArrayList<>();
    ArrayList<Node> children = new ArrayList<>();
    // start
    nodes.add(root);
    // as long as unprocessed nodes are available...
    while(!nodes.isEmpty()) {
        // nodes contain the nodes on the previous (or parent) level
        for(int i = 0; i < nodes.size(); ++i) {
            Node node = nodes.get(i);
            if (op == 1) operationsNoTrim(node); //child generator method
            else if (op == 2) operationsWithTrim(node); //child generator method
            else if (op == 3) operationsWithTrimNSecure(node); //child generator method
            for(int i = 0; i < node.getTotalChild(); ++i) {
                children.add(node.getChild(i));
            }
        }
        // children contain the nodes on the current level
        // all the nodes have been created, but not processed yet
        // now process this level...
        ArrayList<Node> tmp = nodes;
        nodes = children; // children is the new "parent level"
        children = tmp;
        children.clear(); // throw away previous level
    }
}
public void breadthFirst(节点根,int op){
ArrayList节点=新的ArrayList();
ArrayList子项=新的ArrayList();
//开始
添加(根节点);
//只要未处理的节点可用。。。
而(!nodes.isEmpty()){
//节点包含上一级(或父级)的节点
对于(int i=0;i
我不明白这个问题。首先,MedthFirst方法似乎实现了深度优先搜索,而不是广度优先搜索。“子生成器方法”应该做什么?感谢您花时间!我有child生成器方法,只要找到解决方案,或者直到找不到可能的解决方案,它就会生成child。他们应该以广度优先和深度优先的方式来做。请展示子生成器方法的实现。在
breadthFirst
中,不使用其返回值。如果子生成器方法应该为以后的处理过滤子项,这可能就是问题所在。假设我有一个包含2x2个字段的空板和一个片段列表。子生成器在每个字段上放置第一个片段,生成4个子节点(另外4个节点)。然后,取第二个节点,将第二个节点放在剩余的字段上,依此类推。我不太明白你的意思。您是否正在尝试表示此处描述的游戏树?
public void breadthFirst(State state, int op){
        int i = 0;
        while (foundObjective == false){ //Generate nodes until a solution is found
            if (op == 1) noTrimOperations(state);
            else if (op == 2) operationsWithTrim(state);
            else if (op == 3) operationsWithTrimNSeg(state);
            i++;
            try{
                state = cloneState(States.get(i)); //The next node to be expanded                   
                System.out.println("State: " + i);  
                System.out.println("Nodes: "+ (States.size()));
            } catch(IndexOutOfBoundsException e) { //if no mode nodes can be generated due to empty list of pieces
                return;
            }
        }
    }
public void depthFirst(Node node, int op){
    if (op == 1) operationsNoTrim(node); //child generator method
    else if (op == 2) operationsWithTrim(node); //child generator method
    else if (op == 3) operationsWithTrimNSecure(node); //child generator method
    for(int i = 0; i < node.getTotalChild(); i++){
        depthFirst(node.getChild(i), op);
    }
}
public void breadthFirst(ArrayList<Node> nodes, int op) {
    ArrayList<Node> children = new ArrayList<>();
    // nodes contain the nodes on the previous (or parent) level
    for(int i = 0; i < nodes.size(); ++i) {
        Node node = nodes.get(i);
        if (op == 1) operationsNoTrim(node); //child generator method
        else if (op == 2) operationsWithTrim(node); //child generator method
        else if (op == 3) operationsWithTrimNSecure(node); //child generator method
        for(int i = 0; i < node.getTotalChild(); ++i) {
            children.add(node.getChild(i));
        }
    }
    // children contain the nodes on the current level
    // all the nodes have been created, but not processed yet
    // now process this level recursively...
    breadthFirst(children, op);
}
public void breadthFirst(Node root, int op) {
    ArrayList<Node> nodes = new ArrayList<>();
    ArrayList<Node> children = new ArrayList<>();
    // start
    nodes.add(root);
    // as long as unprocessed nodes are available...
    while(!nodes.isEmpty()) {
        // nodes contain the nodes on the previous (or parent) level
        for(int i = 0; i < nodes.size(); ++i) {
            Node node = nodes.get(i);
            if (op == 1) operationsNoTrim(node); //child generator method
            else if (op == 2) operationsWithTrim(node); //child generator method
            else if (op == 3) operationsWithTrimNSecure(node); //child generator method
            for(int i = 0; i < node.getTotalChild(); ++i) {
                children.add(node.getChild(i));
            }
        }
        // children contain the nodes on the current level
        // all the nodes have been created, but not processed yet
        // now process this level...
        ArrayList<Node> tmp = nodes;
        nodes = children; // children is the new "parent level"
        children = tmp;
        children.clear(); // throw away previous level
    }
}