Skip to content
Official PYQ · 2016

GATE CS 2016 Set 2 Q36 — Trees

Explain with AI

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:

Choose one option
View worked solution

Answer: C

To find the New-order traversal, we first construct the expression tree from the given reverse polish (postfix) expression:

Postfix: 3 4 ∗ 5 − 2 ∧ 6 7 ∗ 1 + −\text{Postfix: } 3 \ 4 \ * \ 5 \ - \ 2 \ \wedge \ 6 \ 7 \ * \ 1 \ + \ -

Evaluating the postfix expression yields the following binary expression tree:

  • The root is the final operator: -
  • The left subtree TLT_L represents: (((3∗4)−5)∧2)(((3 * 4) - 5) \wedge 2)
  • The right subtree TRT_R represents: ((6∗7)+1)((6 * 7) + 1)

The New-order traversal scheme is defined as: Root →\to Right Subtree →\to Left Subtree.

Applying this recursively:

  1. At the Root: New-order(T)=[−]⋅New-order(TR)⋅New-order(TL)\text{New-order}(T) = [-] \cdot \text{New-order}(T_R) \cdot \text{New-order}(T_L)
  2. For the Right Subtree TRT_R (root +, left * (6, 7), right 1): New-order(TR)=[+]⋅[1]⋅New-order(6∗7)\text{New-order}(T_R) = [+] \cdot [1] \cdot \text{New-order}(6 * 7) New-order(6∗7)=[∗]⋅[7]⋅[6]\text{New-order}(6 * 7) = [*] \cdot [7] \cdot [6] New-order(TR)=+ 1 ∗ 7 6\text{New-order}(T_R) = + \ 1 \ * \ 7 \ 6
  3. For the Left Subtree TLT_L (root ^, left - (T_1, 5), right 2): New-order(TL)=[∧]⋅[2]⋅New-order(T1−5)\text{New-order}(T_L) = [\wedge] \cdot [2] \cdot \text{New-order}(T_1 - 5) New-order(T1−5)=[−]⋅[5]⋅New-order(3∗4)\text{New-order}(T_1 - 5) = [-] \cdot [5] \cdot \text{New-order}(3 * 4) New-order(3∗4)=[∗]⋅[4]⋅[3]\text{New-order}(3 * 4) = [*] \cdot [4] \cdot [3] New-order(TL)=∧ 2 − 5 ∗ 4 3\text{New-order}(T_L) = \wedge \ 2 \ - \ 5 \ * \ 4 \ 3

Combining these results gives the complete traversal:

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

This matches Option C.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Trees questions