I am going over infix, prefix and postfix conversions and I have a doubt regarding the following expression:
Infix: a+b*d-i
Below conversions are the ones I had in my mind. However, prefix expression below doesn't match with online conversion tools
Prefix: -+a*bdi
Postfix: abd*+i-
When I put the the above infix in some online converters or even in the code I wrote, I get:
Infix: a+b*d-i
Prefix: +a-*bdi
Postfix: abd*+i-
So, my question here is, if we're going to evaluate the above prefix then we'll be doing subtraction before the addition which seems incorrect to me as per operator associativity addition should be evaluated first. How is this correct?
The online converter you point to here uses an incorrect procedure for converting infix to prefix: https://www.web4college.com/converters/infix-to-postfix-prefix.php
It says (and it's WRONG, so nobody copy this!):
The author of this is confused, because:
BUT reversing an infix expression does NOT reverse its expression tree, because of left or right associativity. The given procedure therefore produces the wrong answer.
a-b-c TREE REVERSED TREE c-b-a TREE
- - -
/ \ / \ / \
- c c - - a
/ \ / \ / \
a b b a c b