expression tree

239 Views Asked by At

I'm having some trouble understanding expression trees especially with putting this expression into a tree:

S/P^Q^R

Any help with how to do these is greatly appreciated thanks

1

There are 1 best solutions below

3
On BEST ANSWER

An expression tree is a tree that visualises the evaluation order of the expression. In your case, since you have neither given any information on operator precedence or included parentheses in the expression, there is more than one possible expression tree. If we assume that / is evaluated before ^, you would get an expression tree like

           "^"
          /   \
        "^"     R
       /   \
     "/"    Q
    /  \
   S    P

Working your way back up from the leaves to the root, you can recover the expression: $$(S/P)$$ $$(S/P)^Q$$ $$((S/P)^Q)^R$$

If we assume that ^ is evaluated before /, there are two possibilities, either $$S/((P^Q)^R) \quad \text{or}$$ $$S/(P^{(Q^R)}),$$ which would correspond to the expression trees

       "/"                          "/"
      /   \                        /   \
     S    "^"                     S    "^"
         /   \                        /   \
       "^"    R                      P    "^"
      /   \                              /   \
     P     Q                            Q     R

respectively. If you have no information with regards to operator precedence, then these three expression trees are all correct expression trees for your expression.