GATE / CS ARCHIVE

GATE CSE 1998 / QUESTION 19(b)

Postfix conversion

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

GATE 1998, Q19(b) Descriptive
Report issue

Stacks & Queues

Question 19(b), GATE 1998: Find postfix for 3*log(x+1)−a/2.
Question 19(b), GATE 1998: Find postfix for 3*log(x+1)−a/2.
Tap to enlarge
Answer & solution

Answer: 3 x 1 + log * a 2 / − (no official key was published)

Step through it (14 steps)

First parenthesize by the usual precedence and the unary function call: (3*log(x+1))−(a/2). A postfix traversal writes operands before their operator. The subtree x+1 becomes x 1 +; applying the unary log appends log, giving x 1 + log. Multiplication by 3 becomes 3 x 1 + log *. Independently, a/2 becomes a 2 /. Finally append subtraction after both subexpressions:

3 x 1 + log * a 2 / −

The spaces separate tokens, and log is one unary operator rather than three operands. Evaluating this stack form pushes 3, forms log(x+1), multiplies, forms a/2, then subtracts the right value from the left. Reversing the final operands would change the expression.

Original paper, page 7