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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
package findexit;
import java.util.ArrayList;
public class FindExit {
public static ArrayList<Path> mapPath = new ArrayList<Path>();
public static void main(String args[]) {
System.out.println("Start ");
FindExit startPath = new FindExit();
startPath.setItem(); // set tree
ArrayList<String> root = new ArrayList<String>();
root.add("S1"); // Start Node
startPath.findExit(root);
}
public void setItem() {
mapPath.add(new Path("S0 GateZ"));
mapPath.add(new Path("S1", "S0 GateZ", "S3"));
mapPath.add(new Path("S2"));
mapPath.add(new Path("S3", "S2", "S15"));
mapPath.add(new Path("S4"));
mapPath.add(new Path("S5", "S4", "S7"));
mapPath.add(new Path("S6"));
mapPath.add(new Path("S7", "S6", "S9"));
mapPath.add(new Path("S8"));
mapPath.add(new Path("S9", "S8", "S11"));
mapPath.add(new Path("S10"));
mapPath.add(new Path("S11", "S10", "S13"));
mapPath.add(new Path("S12 GateC"));
mapPath.add(new Path("S13", "S12 GateC", "S14 GateD"));
mapPath.add(new Path("S14 GateD"));
mapPath.add(new Path("S15", "S5", "S17"));
mapPath.add(new Path("S16 GateA"));
mapPath.add(new Path("S17", "S16 GateA", "S18"));
mapPath.add(new Path("S18"));
}
public void findExit(ArrayList<String> rootNode) {
if (rootNode == null)
return;
// Get name of last node from Arraylist
String lastNode = rootNode.get(rootNode.size()-1);
// Convert String to Path Item
Path mItem = findItem(lastNode);
String root = mItem.getmPath();
String left = mItem.getLeftNode();
String right = mItem.getRightNode();
// if node is leaf and not exit node
if (left.startsWith("-") && right.startsWith("-")) {
return ;
}
ArrayList<String> leftResult = new ArrayList<String>();
ArrayList<String> rightResult = new ArrayList<String>();
for(int i = 0; i < rootNode.size(); i++) {
leftResult.add(rootNode.get(i));
rightResult.add(rootNode.get(i));
}
// travel in right side
rightResult.add(right);
if (right.contains("Gate")) {
printArrayList(rightResult);
} else {
findExit(rightResult);
}
// travel in left side
leftResult.add(left);
if (left.contains("Gate")) {
printArrayList(leftResult);
} else {
findExit(leftResult);
}
return;
}
/* Get Node Item from mapPath ArrayList */
public Path findItem(String nameNode) {
if (nameNode != null && !nameNode.equals("-") && !nameNode.equals("")) {
for(int i = 0; i < mapPath.size(); i++ ) {
if ( nameNode.equals(mapPath.get(i).getmPath())) {
return mapPath.get(i);
}
}
}
return null;
}
public void printArrayList(ArrayList<String> findExit) {
// find name of exit node
String exitPath = findExit.get(findExit.size()-1).split(" ")[1].substring("Gate".length());
System.out.print("B -> " + exitPath + ": ");
for (int i = 0; i < findExit.size(); i++) {
if (i == findExit.size()-1) {
System.out.print(findExit.get(i).split(" ")[0]);
break;
}
System.out.print(findExit.get(i) + " -> ");
}
System.out.println("");
}
private class Path {
private String mLeftPath;
private String mRightPath;
private String mRootPath;
public Path(String path) {
this(path, "-", "-");
}
public Path(String exitNode, String leftIndex, String RightPath) {
mRootPath = exitNode;
mLeftPath = leftIndex;
mRightPath = RightPath;
}
public String getLeftNode() {
return mLeftPath;
}
public String getRightNode() {
return mRightPath;
}
public String getmPath() {
return mRootPath;
}
}
}
|