package adapter; import java.util.ArrayList; import java.util.List; public class StringTree { private String data; private List children = new ArrayList (); public StringTree(String data) { this.data = data; } /** * @param ch * The children to be added. * @return this so that it can be directly used after adding * children. */ public StringTree addChild(StringTree child) { this.children.add(child); return this; } public void printTo(TreePrinter out) { out.println(this.data); out.incIndent(); for(StringTree child : this.children) { child.printTo(out); } out.decIndent(); } public static void main(String[] args) { StringTree tree = new StringTree("Root") .addChild(new StringTree("A") .addChild(new StringTree("A1")) .addChild(new StringTree("A2"))) .addChild(new StringTree("B")); tree.printTo(new PrintStreamTreePrinter(System.out)); } }