// TestTree.cpp
int main()
{
NodeExpression* exp;
NodeOperand* nodeA = new NodeOperand( 2 );
NodeOperand* nodeB = new NodeOperand( -8 );
exp = new NodeAdd( nodeA, nodeB );
cout << exp->evaluate() << endl;
exp = new NodeAdd(
new NodeMultiply(
new NodeOperand( 3.0 ),
new NodeOperand( 2.0 )
),
new NodeMultiply(
new NodeOperand( 2.0 ),
new NodeSubtract(
new NodeOperand( 5.0 ),
new NodeOperand( 2.0 )
)
)
);
It may be a good idea to provide a surrogate class with which expressions can be used like simple value types.
(Users need not be aware of a hierarchy of classes, dynamically allocated objects, building the expression tree etc.)
JL, if this code had to provide simple derivatives of the polynomials. This might involve cloning the tree for certain derivatives. It seems this code is readily extensible to provide a derivate() method similar to evaluate(). What about cloning or deep cloning? What do you think?
> What about cloning or deep cloning? What do you think?
Implementing deep cloning is straight forward; however it may be quite expensive.
For instance the earlier code, modified for pure value semantics (deep clone everything except for rvalues)
creates 69 clones of various expression tree nodes.
Perhaps, cloning the tree only when creating derivatives or when required (at programmer discretion),
may be a more tenable approach.