/** * This traversal specializes EulerTour to print out the * arithmetic expression stored in the tree. It assumes that the * elements stored in the nodes of the tree support a toString * method that prints the value at an external node or the * operator at an internal node. */ public class PrintExpressionTour extends EulerTour { public Object execute(BinaryTree T) { super.execute(T); System.out.print("Expression: "); eulerTour(T.root()); System.out.println(); return null; // nothing interesting to return } protected void visitExternal(Position p, TraversalResult r) { System.out.print(p.element()); } protected void visitLeft(Position p, TraversalResult r) { System.out.print("("); } protected void visitBelow(Position p, TraversalResult r) { System.out.print(p.element()); } protected void visitRight(Position p, TraversalResult r) { System.out.print(")"); } }