import util.*;
import java.util.*;
public class Search{
private TreeNode root;
private boolean []isAdded;
private boolean [][]isUsed;
private StringBuffer sb;
public Search(String [][]path){
constructTree(path);
}
public void constructTree(String [][]path){
isAdded=new boolean[path.length];
for(int i=0;i<path.length;i++){
isAdded[i]=false;
}
root=new TreeNode(path[0][0]);
ArrayList sn=new ArrayList();
ArrayList se=new ArrayList();
ArrayList de=new ArrayList();
sn.add(root);
se.add(path[0][0]);
TreeNode temp=root;
for(int i=1;sn.size()>0;i++){
temp=(TreeNode)sn.get(0);
for(int j=0;j<path.length;j++){
if(path[j][0].equals(se.get(0))&&!isAdded[j]){
temp.addChild(path[j][1]);
sn.add(temp.getChild(temp.count()-1));
se.add(path[j][1]);
isAdded[j]=true;;
}
}
de.add(se.get(0));
sn.remove(0);
se.remove(0);
}
}
public ArrayList BFS(String goal){
TreeNode current=null;
ArrayList opened=new ArrayList();
ArrayList closed=new ArrayList();
opened.add(root);
sb=new StringBuffer();
sb.append(String.format("%-10s","EN")+String.format("%-25s","Opened")+"Closed");
sb.append("\n"+String.format(" %-10s","--")+String.format("%-25s","------")+"------");
sb.append(String.format("\n %-10s",""));
sb.append(String.format("%-25s",convertToString(opened))+convertToString(closed));
while(!opened.isEmpty()){
current=(TreeNode)opened.get(0);
opened.remove(0);
if(String.valueOf(current.getData()).equals(goal)){
closed.add(current);
sb.append(String.format("\n %-10s",current.getData()));
sb.append(String.format("%-25s",convertToString(opened))+convertToString(closed));
break;
}
for(int i=0;i<current.count();i++){
opened.add(current.getChild(i));
}
closed.add(current);
sb.append(String.format("\n %-10s",current.getData()));
sb.append(String.format("%-25s",convertToString(opened))+convertToString(closed));
}
return closed;
}
public ArrayList DFS(String goal){
TreeNode current=null;
ArrayList opened=new ArrayList();
ArrayList closed=new ArrayList();
opened.add(root);
sb=new StringBuffer();
sb.append(String.format("%-10s","EN")+String.format("%-25s","Opened")+"Closed");
sb.append("\n"+String.format(" %-10s","--")+String.format("%-25s","------")+"------");
sb.append(String.format("\n %-10s",""));
sb.append(String.format("%-25s",convertToString(opened))+convertToString(closed));
while(!opened.isEmpty()){
current=(TreeNode)opened.get(0);
opened.remove(0);
if(String.valueOf(current.getData()).equals(goal)){
closed.add(current);
sb.append(String.format("\n %-10s",current.getData()));
sb.append(String.format("%-25s",convertToString(opened))+convertToString(closed));
break;
}
for(int i=current.count()-1;i>=0;i--){
opened.add(0,current.getChild(i));
}
closed.add(current);
sb.append(String.format("\n %-10s",current.getData()));
sb.append(String.format("%-25s",convertToString(opened))+convertToString(closed));
}
return closed;
}
public String getProcess(){
return sb.toString();
}
public static String convertToString(ArrayList list){
String str="[";
if(list.size()>0){
str+=((TreeNode)list.get(0)).getData();
for(int i=1;i<list.size();i++){
str+=", "+((TreeNode)list.get(i)).getData();
}
}
str+="]";
return str;
}
public TreeNode getTree(){
return root;
}
}