34,297 views

The postfix expression for the infix expression $A+B*(C+D)/F+D*E$ is:

1. $AB + CD + *F/D +E*$
2. $ABCD + *F/DE* ++$

3. $A * B + CD/F *DE ++$

4. $A + *BCD/F* DE ++$

@Arjun or anyone with edit privileges, please edit the options.

@Ahwan

Not all are left-associative.

I don't know by the people here are talking about $\mathbf{By-Default}$ thing.

According to me $\underline{\text{associativity is a rule}}$ and the operators are either $\mathbf{Left\;Associative}$ or $\mathbf{Right\;Associative}$.

@Satbir

@Verma Ashish

@techbd123

@ankitgupta.1729

Can you people please verify the correctness of this statement.

Thus before considering + which has the least priority, we get  $A + (BCD+*F/) + (DE*)$

Now if we assume left associativity for $+$ (default), we get $ABCD+*F/ + DE*+$ but this is not among the options.

So, considering right associativity for $+$ we get $ABCD+*F/DE*++$

Correct Answer: $B$

@Anix7

you are getting this answer because u are giving more precedence to / than *

but remember "precedence is nothing in front of associativity" as * is coming earlier than /.

then u ll get ABCD+*F/+DE*+ as answer.

Can some one explain please how exactly (B) is answer here by drawing its tree.

reshown
Can u plz explain why are u getting extra + operator at the end .The options may be mistyped

I am getting ABCD+*F/+DE*+
can you please send the picture of the solution?

it is 5 level stak and postfix is: ABCD+*F/+DE*+

No, it will go upto a maximum level of 4 => {+, *, (, +}, then we will encounter ')', and '+' will be popped and printed and '(' will also be popped off from the stack, and the stack will never reach a level of 4 again.
How to find levels
@Arjun sir

It will be B.

It will use a maximum 4 level stack.

edited
But $+$ is left associative , So postfix should be  $$\matrix{ A & B & C & D & + & * & F & / & + & D & E & * &+}$$
Yes this should be the correct answer.

Either options are wrong or it is miss typed ?
yes , this will be correct ans. Now, in GATE question such error will not be found.
 $Top\ of\ stack$ $Next$ $Low\ priority$ $High\ priority$ $PUSH$ $High\ priority$ $Low\ priority$ $POP$ $Same\ priority\ (Left-to-Right\ associativity)$ $POP$ $Same\ priority\ (Right-to-Left\ associativity)$ $PUSH$

According to this answer should be : $ABCD+*F/+DE*+$

We are getting this answer because in the operator stack when $+$ is on the top of the stack and the next symbol is $+$ we are doing a pop operation which has to be done.

But in order to get one of the options as an answer we need to change the associativity of $+$ and when $+$ is on the top of the stack and the next symbol is $+$ we will do a push operation and hence we will get an expression :  $ABCD+*F/DE*++$  which is leading to option $B$

1
9,158 views