#include #include "prelude.h" using std::cout; using std::endl; template struct Tree { virtual ~Tree() {} virtual Fun1& inorder( Fun1& f ) const = 0; }; template class Node : public Tree { T data_; Tree* left_; Tree* right_; public: Node( T d, Tree* l, Tree* r ) : data_(d),left_(l),right_(r) {} Fun1& inorder( Fun1& f ) const { left_->inorder(f); f(data_); right_->inorder(f); return f; } }; template struct Empty : public Tree { Fun1& inorder( Fun1& f ) const { return f; } }; struct Dump : public CFunType { Dump() {} void operator()(int n) const { cout << n << ","; } }; int main() { // 1 // / \ - // 2 3 // / \ - // 4 5 Tree* NIL = new Empty; Tree* t = new Node(1, new Node(2,new Node(4,NIL,NIL),new Node(5,NIL,NIL)), new Node(3,NIL,NIL) ); Dump d; Fun1 dd = makeFun1(d); t->inorder(dd); return 0; }