Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
In prefix notation the operator is written before its operands. Therefore, in order to print the prefix expression from an expression tree, preorder traversal is done. That is, at every non-terminal node we do the following:
While this notation may appear unfamiliar at first, consider the result obtained when we spell out the names of the operators:
Plus(Div(a,b), Times(Minus(c,d), e))This is precisely the notation used in a typical programming language to invoke user defined methods Plus, Minus, Times, and Div.