Search code examples
c++treebinary-treemathematical-expressions

tree expression - Node setting up


I am confused with a project that I study for a course. I have to build a tree-expression, I know how the algorithm works, but I have difficulties with setting up the nodes.

The instructor gave us this class: Expression Node Where it has 2 other subclasses: Constant Node and Operator Node?

and in the "Expression Tree class": Where am I building the tree-expression? I have a Stack with this type : Expression Node

my question is: after I assign an operator or an constant, how can I push it to the Stack, since the types are different.

i am working with C++ thank you.


Solution

  • Apply polymorphism: Push pointers ( or references ) to your nodes. Since your stack contains elements of type "ExpressionNode", it can accept all types that derive from it ( Constant and Operator).