63 63 votes The postfix expression for the infix expression $A+B*(C+D)/F+D*E$ is: $AB + CD + *F/D +E*$ $ABCD + *F/DE* ++$ $A * B + CD/F *DE ++$ $A + *BCD/F* DE ++$ Data Structures gate1995 data-structures stack easy + – Kathleen 49.6k views answer comment Share Follow Print See all 12 Comments 12 12 Comments reply Ahwan commented Aug 12, 2017 reply Follow flag The correct ans is ABCD + * F/+DE * + No option matches... By default arithmatic operators are left associative. If it is right associative, then they should mention it. So qsn/option should be edited then only ans is B 53 53 replyShare sripo commented Nov 17, 2018 reply Follow flag @Ahwan getting same answer as yours 0 0 replyShare sripo commented Nov 17, 2018 reply Follow flag I am getting answer as ABCD+/F*+DE*+. I have given division higher priority than multiplication as per bodmas rule,plus no associativity and precedence rule is specified. 0 0 replyShare zeeshanmohnavi commented Jan 26, 2019 reply Follow flag @Arjun or anyone with edit privileges, please edit the options. 0 0 replyShare `JEET commented Dec 19, 2019 reply Follow flag @Ahwan Not all are left-associative. 0 0 replyShare `JEET commented Dec 19, 2019 reply Follow flag 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. 1 1 replyShare Srken commented Dec 9, 2025 reply Follow flag i've done mistake when division (/) operator comes then in the stack there're { +,* } where TOP is * (mul op) and i've popped multiplication (F) came and ignored when + came on top of stack it's *( mul) and as it has higher priority so popped * but here done the mistake that i've also popped Plus(+) also as same priority operator .And this is the mistake and solution is that we should not pop two operators at the same time.And if there's any mistake in my understanding then please rectify it. 0 0 replyShare Abdul_Rahman_Khan commented Jan 27 reply Follow flag guys the options are worng in my opinion. you cant just change the associativity. may be misprinted 1 1 replyShare Honey badger commented Jul 11 reply Follow flag All options are wrong (A B C D + * F / + D E * +) is the right postfix expression. Option B is misprinted version of it. 3 3 replyShare S_Sandeep commented Aug 31 reply Follow flag yes u are correct @Honey badger 1 1 replyShare duckduck commented Sep 16 reply Follow flag if asked prefix expression then ++A/*B+CDF*DE 0 0 replyShare firstgod commented 6 days ago reply Follow flag yes i agree 0 0 replyShare Please log in or register to add a comment.
Best answer 87 87 votes 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$ amarVashishth answered Nov 17, 2015 • edited Jul 2, 2019 by Arjun amarVashishth comment Share Follow See all 15 Comments 15 15 Comments reply Show 12 previous comments Srken commented Dec 9, 2025 reply Follow flag At a time how many operators can be there in stack ? The reason for this question is just take this scenario that stack contains {+,*} and top of stack is mul (*) and then again + operator came then how do we pop out both operators from stack and push + or do we only pop * operator and push + so that stack contains {+,+} 0 0 replyShare Tushar Rana commented Jan 26 reply Follow flag @Surya Pratap Singh This is because you will be arriving at A + BCD+*F/ + DE*now the associativity is being taken as right to left for "+"so A + BCD+*F/ + DE*= A + BCD+*F/DE*+= ABCD+*F/DE*++ 0 0 replyShare Neeraj_Dubey commented Mar 13 reply Follow flag yea through out que we followed left associativity, which infact is the actual associativity for infix, lets now at end try right also just to match option. 0 0 replyShare Please log in or register to add a comment.
19 19 votes it is 4 level stak and postfix is: ABCD+*F/+DE*+ Hira Thakur answered Nov 17, 2015 • edited Jan 26 by Hira Thakur Hira Thakur comment Share Follow See all 4 Comments 4 4 Comments reply ravi_ssj4 commented Aug 21, 2016 reply Follow flag 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 replyShare Rishi yadav commented Oct 3, 2017 reply Follow flag How to find levels 1 1 replyShare DEEPAK PANDEY 1 commented Mar 31, 2018 reply Follow flag This might be useful.. https://www.geeksforgeeks.org/stack-set-2-infix-to-postfix/ 0 0 replyShare Lakshman Bhaiya commented Oct 16, 2018 reply Follow flag @Arjun sir Please edit the answer. 0 0 replyShare Please log in or register to add a comment.
12 12 votes Infix to Postfix using Stack : If ( ‘(‘ ) push in stack If ( ‘)‘ ) pop until left parenthesis is popped If ( operator ) 1. Lower priority (w.r.t to Stack Top ) is in input then pop 2. Higher priority input in input then push 3. Same priority then pop If ( operand ) ignore Now using the above algorithm , let us evaluate A + B * (C + D) / F + D * E : Steps Stack Element Output Step 1 : Ignore A (as it’s operand ) A Step 2: Since Stack is empty push ‘+’ + A Step 3: Ignore B (as it’s operand ) AB Step 4 : * has Higher priority than + so push * + AB Step 5 : push ‘ ( ‘ in stack ( * + AB Step 6 : Ignore C (as it’s operand ) ( * + ABC Step 7 : push ‘+’ in stack + ( * + ABC Step 8 : Ignore D (as it’s operand ) + ( * + ABCD Step 9 : pop until left parenthesis is popped *+ ABCD+ Step 10 : ‘/’ has same priority as ‘*’ , so pop ‘*’ and push ‘/’ into stack as ‘/’ has higher priority than ‘+’ /+ ABCD+* Step 11 : Ignore F (as it’s operand ) /+ ABCD+*F Step 12 : ‘+’ has lower priority than ‘/’ , so pop ’/’ and push ’+’ ++ ABCD+*F/ Step 13 : Ignore D (as it’s operand ) ++ ABCD+*F/D Step 14 : push ‘*’ as it has higher preceedence than ‘+’ *++ ABCD+*F/D Step 15 : Ignore E (as it’s operand ) *++ ABCD+*F/DE Step 16 : We have ‘*’ , ‘+’ , ‘+’ in the stack . Just empty the stack ABCD+*F/DE*++ Correct Ans : Option ( B ) ABCD+*F/DE*++ Vishalk17 answered Sep 17, 2022 Vishalk17 comment Share Follow See all 3 Comments 3 3 Comments reply Ritika Sharma commented Sep 18, 2022 reply Follow flag Thanks! Definitely It’s the best answer. 1 1 replyShare ananya_23 commented Jul 11, 2024 reply Follow flag how in step 11, two ++ exist together? nowhere it is mentioned + operator is right associative. 3 3 replyShare Krishna Reddy kyp commented Sep 5, 2024 reply Follow flag since options are not matching,we assume it to be right associative. That's the only thing we can do. Atleast they should have mentioned it in the question to assume + to be right associative but they didn't mentioned causing ambiguity. 1 1 replyShare Please log in or register to add a comment.
9 9 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$ KUSHAGRA गुप्ता answered Dec 28, 2019 • edited Aug 12, 2020 by KUSHAGRA गुप्ता KUSHAGRA गुप्ता comment Share Follow 0 reply Please log in or register to add a comment.
6 6 votes It will be B. It will use a maximum 4 level stack. Gate Keeda answered Oct 9, 2014 Gate Keeda comment Share Follow See all 4 Comments 4 4 Comments reply Himanshu1 commented Nov 16, 2015 i edited by Pragy Agarwal Nov 17, 2015 reply Follow flag But $+$ is left associative , So postfix should be $$\matrix{ A & B & C & D & + & * & F & / & + & D & E & * &+}$$ 15 15 replyShare Kuljeet Shan commented Mar 7, 2019 reply Follow flag Yes this should be the correct answer. Either options are wrong or it is miss typed ? 0 0 replyShare srestha commented Aug 29, 2019 reply Follow flag yes , this will be correct ans. Now, in GATE question such error will not be found. 4 4 replyShare Siddiqui_Danish commented Jan 21 reply Follow flag abhi doosri galtiyaan karte hai ambigous questions dene ki 0 0 replyShare Please log in or register to add a comment.
0 0 votes B is the answer if you want to verify just push the symbols into the stack whenever you get an operator just pop last two symbols and operate them with the oprator and then check with the infix.You will get your answer. arkaprabha1012 answered Aug 22, 2020 arkaprabha1012 comment Share Follow 0 reply Please log in or register to add a comment.