import java.util.*; public class GeneralNode { private Object item; private LinkedList nodeList; public GeneralNode() { nodeList = new LinkedList(); } public GeneralNode(Object item) { this.item=item; if (nodeList==null) nodeList = new LinkedList(); } public void addNode(GeneralNode node) { nodeList.add(node); } // using pre-order for clarity: public String toString() { String s = ""+item; Iterator it = nodeList.iterator(); if (it.hasNext()) s += "("; while(it.hasNext()) { s += it.next(); if (it.hasNext()) s += ","; if (!it.hasNext()) s += ")"; } return s; } // handier way to view tree, esp for debugging: public String toTree() { return toTree("| ","|___"); } public String toTree(String blank, String spacing) { String t = item.toString() + "\n"; for ( Iterator it = nodeList.iterator(); it.hasNext(); ) { if (it.hasNext()) { GeneralNode next = (GeneralNode) it.next(); t += spacing + (next.toTree(blank, blank+spacing)); } } return t; } }