GATE / CS ARCHIVE

GATE CSE 2002 / QUESTION 2.19

Stack space for expression evaluation

Data Structures · Stacks & Queues · Full GATE CSE 2002 paper

GATE 2002, Q2.19 2 marks · MCQ
Report issue

Stacks & Queues

Question 2.19, GATE 2002: To evaluate an expression containing no embedded function calls, which claim is correct?
(A) One stack suffices.
(B) Two stacks are required.
(C) The number of required stacks equals the expression-tree height.
(D) A Turing machine is required in the general case.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step 1: Walk through a one-stack evaluation.

Step 2: For postfix tokens a b + c *, push a then b, pop them and push a+b, then push c, and finally replace the top two values with(a+b)c. The same procedure handles arbitrary finite postfix expressions by induction on the expression tree. Extra stacks can help conversion or implementation, but are not inherently required for evaluation.

Conclusion: For a postfix representation, push operands and replace the required top operands by the result whenever an operator is encountered. One evaluation stack suffices for any finite expression tree without embedded calls, A. Infix parsing can use a combined stack representation or first produce postfix; two separate stacks are a convenient implementation, not a necessity.

Original paper, page 5