Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

GATE | GATE-CS-2002 | Question 44

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

To evaluate an expression without any embedded function calls :
 

(A)

As many stacks as the height of the expression tree are needed

(B)

One stack is enough

(C)

Two stacks are needed

(D)

A Turing machine is needed in the general case


Answer: (B)

Explanation:

Any expression can be converted into Postfix or Prefix form. 

Prefix and postfix evaluation can be done using a single stack. 

For example : Expression \’10 2 8 * + 3 -\’ is given. 
PUSH 10 in the stack. 
PUSH 2 in the stack. 
PUSH 8 in the stack. 
When operator \’*\’ occurs, POP 2 and 8 from the stack. 
PUSH 2 * 8 = 16 in the stack. 
When operator \’+\’ occurs, POP 16 and 10 from the stack. 
PUSH 10 * 16 = 26 in the stack. 
PUSH 3 in the stack. 
When operator \’-\’ occurs, POP 26 and 3 from the stack. 
PUSH 26 – 3 = 23 in the stack. 
So, 23 is the answer obtained using single stack. 

 

Thus, option (A) is correct. 

 

Please comment below if you find anything wrong in the above post. 

 


Quiz of this Question
Please comment below if you find anything wrong in the above post

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads