in DS recategorized by
34,297 views
42 votes
42 votes

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 ++$

in DS recategorized by
34.3k views

4 Comments

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

0
0

@Ahwan

Not all are left-associative.

0
0

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.

0
0

7 Answers

58 votes
58 votes
Best answer

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$

edited by

4 Comments

@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.

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

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

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

4 Comments

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.
2
2
How to find levels
1
1
0
0
@Arjun sir

Please edit the answer.
0
0
5 votes
5 votes
It will be B.

It will use a maximum 4 level stack.

3 Comments

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

Either options are wrong or it is miss typed ?
0
0
yes , this will be correct ans. Now, in GATE question such error will not be found.
2
2
5 votes
5 votes
$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$

edited by
Answer:

Related questions