C++ Notes: Expression Trees

There are a many ways to write expressions: normal infix notation using operator precedence and parentheses, prefix (function) notation, postfix (reverse Polish, named after the Polish mathematicial Lukasiewicz) notation, and as trees. Trees are hard to represent in text-based writing systems, however, they are easy to use in a computer. In computing diagrams of trees are most often written with the root at the top.

This example has +, -, and * operators and integer operands.

Normal infix notation: (7-5) * (3+(1-2))

Tree equivalent:
          *
         / \
        /   \
       -     +
      / \   / \
     #   # #   -
     7   5 3  / \
             #   #
             1   2
Each node in the tree is represented as a struct/class, with the following fields:

Processing trees can be done in a nice, compact, natural, recursive manner.

Two examples of how to declare this: