-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFSearch.java
31 lines (27 loc) · 966 Bytes
/
DFSearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Dylan Wulf
//Artificial Intelligence Project 1
//Feb 12, 2017
import java.util.LinkedList;
import java.util.Stack;
//Depth-first search strategy. Implemented using a stack.
public class DFSearch implements SearchStrategy {
private Stack<ProblemNode> frontierStack;
//Constructor
public DFSearch(ProblemNode rootNode) {
frontierStack = new Stack<ProblemNode>();
frontierStack.push(rootNode);
}
//Search method returns the first node to be added to the stack which is at the goal location,
//or null if no such node is found.
public ProblemNode search() {
while (frontierStack.empty() == false) {
ProblemNode node = frontierStack.pop();
LinkedList<ProblemNode> nodeChildren = node.getChildNodes();
for (ProblemNode p : nodeChildren) {
if (p.isGoalNode()) return p;
frontierStack.push(p);
}
}
return null;
}
}