Code Fragment: PrintExpressionTour



template <typename BinaryTree>
class PrintExpressionTour : public EulerTour<BinaryTree> {
public:
  void execute(const BinaryTree& T) {			// execute the tour
    initialize(T);
    cout << "Expression: ";
    eulerTour(T.root());
    cout << endl;
  }
protected:
  typedef EulerTour<BinaryTree>::Position	     Position;
  typedef EulerTour<BinaryTree>::TraversalResult     TraversalResult;
  virtual void visitExternal(const Position& v, TraversalResult& r) const
    { v.element().print(); }				// leaf: print value
  virtual void visitLeft(const Position& v, TraversalResult& r) const
    { cout << "("; }					// open new subexpression
  virtual void visitBelow(const Position& v, TraversalResult& r) const 
    { v.element().print(); }				// internal: print operator
  virtual void visitRight(const Position& v, TraversalResult& r) const
    { cout << ")"; }					// close subexpression
};