user3320832
user3320832

Reputation: 13

Converting terenary and boolean operators from infix to postfix

How can I convert these two examples from infix to postfix?

Example 1:
max = (a > b) ? a : b

Example 2:
(a != 0) ? ((b != 0) ? True : False) : False

For both expressions, I thought I would just have to remove the brackets. However, when I try to convert back from postfix to infix, the expression is invalid. I know how to do simple operations:

Infix:     (((a + b) * (c + d) + a) * c - 6) * b

Postfix:   a b + c d + * a + c * 6 - b *

...but I'm not sure how to convert max and boolean expressions.

Upvotes: 0

Views: 830

Answers (1)

Kode Charlie
Kode Charlie

Reputation: 1489

Instead of thinking about '+', '-', and '*' as operations apart from boolean operators or max / min functions, I think you could understand the problem better by simply saying, "All of these are operators with 2 operands -- ie, they are binary operators."

Then, the problem is setting up your expression tree so that the root is an operator ('+', '-', 'max' or whatever), and the children are operands. Producing infix or postfix is simply a question of how you traverse the expression tree.

Upvotes: 0

Related Questions