import java.util.List; /** This class represents those And-Or trees whose data values may be represented as states. Each node is either an and node or an or node; this implementation places no constraints on where either type of node may appear in the tree. The trees are ordered trees; leaves are simply those nodes whose lists of children are empty (or null). Instances of this class are immutable. They may only be constructed and coverted to strings. */ public class AndOrTree { // the data for the node private State data; // whether the node is an "and" node or an "or" node private boolean isOrNode; // the list of children of the node private List children; /** A constructor @param d the data for the node @param b true for or nodes; false for and nodes @param c the list of the node's children */ public AndOrTree(State d, boolean b, List c){ data = d; isOrNode = b; children = c; } // This method simply invokes a recursive version // that keeps track of the appropriate indenting public String toString() { return toString(""); } /** This method indents each level two spaces beyond the parent level */ private String toString(String indent) { StringBuffer sb = new StringBuffer(); sb.append(indent); if (isOrNode) sb.append("OR : "); else sb.append("AND: "); sb.append(data); sb.append(System.getProperty("line.separator")); if (children != null) for (AndOrTree ao : children) sb.append(ao.toString(indent + " ")); return new String(sb); } }