42 42 votes Assume that the operators $+, -, \times$ are left associative and $\;\hat{}\; $ is right associative. The order of precedence (from highest to lowest) is $ \ \;\hat{}\;, \times, +, -$. The postfix expression corresponding to the infix expression $a+ b \times c-d ^ \ \ \hat{}\ e^ \ \hat{} \ f$ is$abc\times+def \ \ \hat{}{} \ \ \ \hat{}-$$abc\times+de\ \ \hat{} \ \ f \ \ \hat{} \ \ -$$ab+c\times d-e \ \ \hat{} \ \ f \ \ \hat{}$$-+a\times bc \ \ \hat{} \ \ \ \hat{} \ \ def$ Data Structures gatecse-2004 stack isro2009 infix-prefix + – Kathleen 19.8k views answer comment Share Follow Print See all 3 Comments 3 3 Comments reply Suhail96 commented Apr 30, 2020 reply Follow flag can someone please help me convert the same infix expression to prefix. Im having some trouble in it. 1 1 replyShare prithatiti commented Jun 7, 2020 reply Follow flag @Suhail96 You can try using an expression tree. That would be easy. 1 1 replyShare Krishna Reddy kyp commented Sep 4, 2024 reply Follow flag Prefix: -+a*bc^d^ef 0 0 replyShare Please log in or register to add a comment.
Best answer 73 73 votes Answer is (A). Here is the procedure first : Scan Infix Expression from left to right whenever you see operand just print it. But, in case of operator if(stack is empty) then push it. if(precedence(tos) $<$ precedence(current operator) ) push it. where, tos means top of stack. else if (precedence(tos) $>$ precedence(current operator) ) pop and print. else if (precedence(tos) $==$ precedence(current operator) ) then check for associativity.In case Operators are Left to right then pop and print it otherwise push the current operator (Right to Left Associativity) And once you have scanned infix expression completely. Make sure pop all the element and print it in same order. Here, the infix expression is $a+b \times c−d ^\hat{} e ^\hat{}f$ $a$ : print it $+$ : push into the Operator Stack $b$ : print it $*$ : its having higher precedence than $+$ then push into Operator Stack $c$ : print it $'-'$ : $'-'$ is having less precedence than $'*'$ so pop from operator stack and print $'*'$.after this stack will be having $'+'$ on top.which is having same precedence as $'-'$ but both are left to right associative then just pop $+$ and print it. Now stack is empty. Push $'-'$ to it. $d$ : print it '$^\hat{}$' : top of the stack is having $'-'$.'$^\hat{}$' has higher precedence than $'-'$.so simply push '$^\hat{}$' into stack $e$ : print it. '$^\hat{}$' : Now top of the stack is '$^\hat{}$' and has same precedence so associativity will come to picture. Since '$^\hat{}$' is right associative as given in question. So '$^\hat{}$' will be pushed. $f$ : print it. Now, we have scanned entire infix expression.Now pop the stack until it becomes empty.This way you will get $abc\times+def \ \ \hat{}{} \ \ \ \hat{}-$. IgnitorSandeep answered Sep 24, 2015 • edited Oct 30, 2022 by Abhrajyoti00 IgnitorSandeep comment Share Follow See 1 comment 1 1 comment reply commenter commenter commented Oct 25, 2019 reply Follow flag ...after this stack will be having ′+′ on top.which is having same precedence as ′−′ but ... Even though the final answer remains same this is wrong according to the question. + has more precedence than -. 7 7 replyShare Please log in or register to add a comment.
12 12 votes Use Bracket according to precedance ^ - Precedance from Right to Left and others left to right ((a + (b * c )) - (d ^ (e ^ f))) then solve it Rishi yadav answered Oct 3, 2017 Rishi yadav comment Share Follow 0 reply Please log in or register to add a comment.
6 6 votes Given infix expression: $a+b\times c-d^ \ \ \hat{}\ e^ \ \ \hat{} \ f$ $\implies a+b\times c-d^ \ \ \hat{}\ (e^ \ \ \hat{} \ f)$ $\implies a+b\times c-(d^ \ \ \hat{}\ e f ^\ \hat{})$ $\implies a+(b\times c)- d\ e f ^\ \ \hat{} ^ \ \ \hat{}$ $\implies (a+bc\times)\ –\ d\ e f ^\ \ \hat{} ^ \ \ \hat{}$ $\implies (abc\times+) \ –\ (d\ e f ^\ \ \hat{} ^ \ \ \hat{})$ $\implies abc\times+ d\ e f ^\ \ \hat{} ^ \ \ \hat{} -$ Option $(A)$ is correct. Hira Thakur answered Dec 14, 2023 Hira Thakur comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote answer - A ankitrokdeonsns answered Nov 23, 2014 ankitrokdeonsns comment Share Follow See all 2 Comments 2 2 Comments reply junk_mayavi commented Jun 13, 2016 reply Follow flag One easy way is: 1. Add brackets as per precedence and order of evaluation 2. Move operators to the right of immediate paranthesis for postfix. Here, after adding brackets, ((a+(b*c))-(d^(e^f))) Now after moving operators to immediate right paranthesis, we obtain, abc*+def^^- Sorry for typos. 15 15 replyShare Kuljeet Shan commented Jun 1, 2019 reply Follow flag The easiest way @junk_mayavi Thanks ! 1 1 replyShare Please log in or register to add a comment.
0 0 votes Hence, answer is (A) KathanVakharia answered Jan 10, 2022 KathanVakharia comment Share Follow 0 reply Please log in or register to add a comment.