'Transform the following infix expression to postfix form (using stacks)

guys please its due asapp

Transform the following infix expression to postfix form (using stacks). (A + 2) * (B - C + D * E) + F



Solution 1:[1]

The idea with postfix is that you put the operand(s) to the left of the operator rather than on either side. This is nice because it removes ambiguity about the order of operations without the need for parentheses. When starting with an infix expression we want to convert to postfix, always fully parenthesize the infix expression first. Your expression is this:

(A + 2) * (B - C + D * E) + F

According to standard rules of order of operations, a corresponding fully-parenthesized expression is this:

(((A + 2) * ((B - C) + (D * E))) + F)

This makes it clear at what level each infix operator exists. The rightmost postfix operator should be the infix operator that is enclosed by the smallest number of parentheses: in our case, +. That means our postfix expression must end with + and have two subexpressions to the left of it:

r s +

The expression for s is easy: it is F. The expression for r should be the postfix expression corresponding to the other side of the infix + operator, ((A + 2) * ((B - C) + (D * E))). So, we can apply the same procedure recursively to get an expression for r. We find that * is the outermost operator here, so we get

r' s' *

r' will be a postfix expression for (A + 2); this one's easy, it's A 2 +. The other one, ((B - C) + (D * E)), is a little trickier. Its outermost operation is +, so

r'' s'' +

r'' is the postfix expression for B - C, which is B C -, and s'' is the postfix expression for D * E, which is D E *. So, our expression for s' is

B C - D E * +

Substituting back, we get an expression for r:

A 2 + B C - D E * + *

Finally, the postfix version of the original infix expression:

A 2 + B C - D E * + * F +

All this said, I am not sure exactly how to interpret "using stacks"... perhaps this suggests what you want is a piece of code that does this procedure automatically for any infix expression. That's not conceptually much harder than what we've done, but writing out all the code for it is a bit of a chore. Maybe a high level description would suffice:

  1. Tokenize the expression into a series of subexpressions separated by operators at the top level (not inside parentheses)
  2. Generate the postfix expression for the tokenized infix expression recursively by applying order of operations, using simple symbols as they are, and calling the method recursively on subexpressions

Note that as an alternative to applying order of operations, or perhaps as an implementation of it, you could first produce the fully-parenthesized infix expression given the order of operations, and then simply transform the infix (r # s) to r s #, simplifying the conversion considerably.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Patrick87