Question
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
is given by:
Official answer and solution
- Official answer
- C
- From
- Official GATE 2016 answer key
Solution
Construct the expression tree from the reverse Polish string and perform the New-order traversal (Root, Right, Left).
Given Reverse Polish (postfix) expression:
Construct the expression tree step-by-step using a stack:
3,4,*: SubtreeT_1,5,-: SubtreeT_2,2,^: Subtree6,7,*: SubtreeT_4,1,+: SubtreeT_3,T_5,-: Root is-with left child and right child .
Apply New-order traversal: [Root, New-order(Right), New-order(Left)]:
- Root:
- - New-order on Right Subtree :
- Visit root:
+ - Right child:
1 - Left child :
- Visit root:
* - Right child:
7 - Left child:
6
Right traversal sequence:+ 1 * 7 6
- Visit root:
- Visit root:
- New-order on Left Subtree :
- Visit root:
^ - Right child:
2 - Left child :
- Visit root:
- - Right child:
5 - Left child :
- Visit root:
* - Right child:
4 - Left child:
3
Left traversal sequence:^ 2 - 5 * 4 3
- Visit root:
- Visit root:
- Visit root:
Concatenating Root + Right + Left:
This matches option (C).
C
Visiting left before right (standard pre-order) instead of the defined New-order (right before left).
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2016 Computer Science and Information Technology (CS) Session 2
- Question
- 46 of 65, printed as CS Q.36
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.