GATE / CS ARCHIVE

GATE CSE 2016 SET 2 / QUESTION 36

GATE CSE 2016 Set 2 Q36

Data Structures · Tree Properties & Traversals · Full GATE CSE 2016 Set 2 paper

GATE 2016 Set 2, Q36 2 marks · MCQ
Report issue

Tree Properties & Traversals

Question 36, GATE 2016: New-order visits root, then right subtree, then left subtree recursively. For the expression tree of postfix “3 4 * 5 - 2 ^ 6 7 * 1 + -”, choose its New-order. A + - 1 6 7 * 2 ^ 5 - 3 4 *; B - + 1 * 6 7 ^ 2 - 5 * 3 4; C - + 1 * 7 6 ^ 2 - 5 * 4 3; D 1 7 6 * + 2 5 4 3 * - ^ -.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (15 steps)

The postfix expression gives root subtraction with left subtree((3*4)−5)2 and right subtree(6*7)+1. Root-right-left order visits −, then + 1 * 7 6, then ^ 2 − 5 * 4 3. Thus − + 1 * 7 6 ^ 2 − 5 * 4 3, C.

Build the tree with a stack: push operands; for an operator pop the right child first, then the left child. The final minus combines the two large subexpressions. Reversing child visits does not exchange the operands in the original expression; it changes only traversal order. In particular the right multiplication is visited * 7 6, excluding B.

Original paper, page 35 · Answer key