Skip to content

Expressions as Binary Trees

Matthew McCall edited this page Mar 23, 2024 · 8 revisions

Consider the following expression: $$2x^2+x-1$$ We can represent this expression as a binary tree:

quadratic_example

Note that we strictly follow PEMDAS as the same equation can be represented with multiple different trees. As such, we try to follow PEMDAS as strictly as possible to make the tree-matching patterns as simple as possible. Operations done first in PEMDAS are deeper in the tree. Oasis offers a convenient declarative syntax for building expression. The following represents the above expression:

Oasis::Subtract {
  Oasis::Add {
    Oasis::Multiply {
      Oasis::Real { 2.0f },
      Oasis::Exponent {
        Oasis::Variable { "x" },
        Oasis::Real { 2.0f }
      }
    },
    Oasis::Variable { "x" }
  },
  Oasis::Real { 1.0f }
};
Clone this wiki locally