2,182 views
2 votes
2 votes

The maximum size of operator stack, when converting the following infix expression to postfix expression? Assume that  has the highest precedence and follows right associativity is _______.
 

Infix: 

1 Answer

Best answer
5 votes
5 votes

According to the algo given here

Let o/p string be outStr.

1. Incoming symbol:  Since it is an operand, print it to the o/p directly. outStr = m

2. Incoming symbol: $\uparrow$.  Operator so push it onto the stack. Stack = ( $\uparrow$ )

3. Incoming symbol: p  Print it to the o/p. outStr = (mp)

4. Incoming symbol: * . Since * has lower precedence that $\uparrow$, pop and print TOS onto o/p. So outStr = (mp $\uparrow$) and then push the incoming symbol *. So stack now is (*)

5. Incoming symbol: q. outStr = (mp $\uparrow$ q)

6. Incoming symbol: *  At this point TOS is also *. Since both operators have same precedence, we check associativity. If associativity is left to right (which it is here) then pop and print the TOS. So outStr = (m p $\uparrow$ q *) and then push the incoming operator * onto the stack. So stack now is (*)

7. Incoming symbol: r. outStr = (m p $\uparrow$ q * r)

8. Incoming symbol: +.  TOS is *, which has higher precedence that +, so pop and print the TOS. So outStr = (m p $\uparrow$ q * r *) and now push incoming symbol +. So stack now is (+)

9. Incoming symbol: a. outStr = (m p $\uparrow$ q * r * a)

10. Incoming symbol: *. TOS is + which has lower precedence than *, so we push incoming symbol onto the stack. Stack now is (+ * )

11. Incoming symbol: b.  outStr = (m p $\uparrow$ q * r * a b) 

12. Incoming symbol: $\uparrow$. Since it has higher precedence than TOS (which is * ), push it onto stack.

Stack now : (+ * $\uparrow$ )

13. Incoming symbol: c. outStr = (m p $\uparrow$ q * r * a b c). 

Now since symbols are over, we just have to pop all the operators one by one. Also we see that the current operator stack has the maximum size at this time (which is 3).

final outStr = (m p $\uparrow$ q * r * a b c $\uparrow$ * +). 

selected by

Related questions

0 votes
0 votes
3 answers
1
0 votes
0 votes
1 answer
2
3 votes
3 votes
5 answers
3
srestha asked May 22, 2019
1,857 views
Consider the following function height, to which pointer to the root node of a binary tree shown below is passedNote that max(a,b) defined by #define max(a,b) (a>b)?a:b.i...