Skip to main content

GATE CSE 2016Data Structures › Trees & Binary Search TreesVerified

GATE CSE 2016 PYQ: Consider the following New-order strategy for traversing a binary tree: Visit the root; Visit the right…

Consider the following New-order strategy for traversing a binary tree:

  • Visit the root;
  • Visit the right subtree using New-order;
  • Visit the left subtree using New-order;

The New-order traversal of the expression tree corresponding to the reverse polish expression

3 4 * 5 - 2 ^ 6 7 * 1 + - 

is given by:

  1. A
    +−1 6 7∗2∧5 − 3 4 ∗+ - 1 \ 6 \ 7 * 2 \wedge 5 \ - \ 3 \ 4 \ *
  2. B
    −+1∗6 7∧2−5∗3 4- + 1 * 6 \ 7 \wedge 2 - 5 * 3 \ 4
  3. C
    −+1∗7 6∧2 − 5 ∗ 4 3- + 1 * 7 \ 6 \wedge 2 \ - \ 5 \ * \ 4 \ 3
    Correct answer
  4. D
    1 7 6∗+ 2 5 4 3 ∗ −∧−1 \ 7 \ 6 * + \ 2 \ 5 \ 4 \ 3 \ * \ - \wedge -

Answer

(C) − + 1 ∗ 7 6 ∧ 2 − 5 ∗ 4 3 - + 1 * 7 \ 6 \wedge 2 \ - \ 5 \ * \ 4 \ 3 − + 1 ∗ 7 6 ∧ 2 − 5 ∗ 4 3

Approach

Convert the reverse Polish (postfix) expression into an expression tree, then perform the defined New-order traversal (root, right subtree, left subtree). Equivalently, note that New-order is the mirror of postorder, so its sequence is the reverse of the postfix token order.

The expression tree's New-order traversal (root, right, left) is the reverse of its postorder traversal. Since the given reverse Polish expression is already a postorder traversal, reversing its tokens gives - + 1 * 7 6 ^ 2 - 5 * 4 3, which is option C.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.