Tree { {{ int sum_labels() throws Exception { SummingVisitor s = SummingVisitor.parse("0"); this.allLabels(s, new TraceVisitor()); return s.get_total();} // int sum_labels2() = allLabels2(SummingVisitor2, DisplayVisitor); int sum_labels2(){ SummingVisitor2 s = new SummingVisitor2(); DisplayVisitor d = new DisplayVisitor(); this.allLabels2(s,d); d.finish(); // to flush the printing buffer return s.get_total();} }} traversal allLabels(SummingVisitor, UniversalVisitor) { to Label;} traversal allLabels2(SummingVisitor2, UniversalVisitor) { to Label;} traversal leftTrees(UniversalVisitor) { bypassing -> *,right,* to Tree;} traversal allTrees(UniversalVisitor) { to Tree;} void print_leftTrees() = leftTrees(DisplayVisitor); // void print_leftTrees() {{ // DisplayVisitor dv = new DisplayVisitor(); // this.leftTrees(new DisplayVisitor(pw)); // dv.finish(); // }} // Doug // In your case, you need to wrap the PrintWriter around System.out. The // code would look like this: // // java.io.PrintWriter pw = new java.io.PrintWriter(System.out); // this.leftTrees(new DisplayVisitor(pw)); // pw.flush(); // //Johan void print_allTrees() = allTrees(DisplayVisitor); } SummingVisitor { before Label {{ total = total + host.get_v(); System.out.print("at a Label ----"); }} } SummingVisitor2 { init {{ total = 0; }} before Label {{ total = total + host.get_v(); System.out.print("at a Label ----"); }} return int {{ total }} } Main { {{ static public void main(String args[]) throws Exception { Tree tree = Tree.parse(System.in); int r = tree.sum_labels(); System.out.println(); int r2 = tree.sum_labels2(); System.out.println(); System.out.println("done: result = " + r + "," + r2); System.out.println(" show left trees "); tree.print_leftTrees(); System.out.println(); System.out.println(" show all trees "); tree.print_allTrees(); } }} }