Consider the following New-order strategy for traversing a binary tree: Visit…
2016
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:
- 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 * - ˆ -
Attempted by 185 students.
Show answer & explanation
Correct answer: C
Solution: Build the expression tree from the given postfix (RPN) expression 3 4 * 5 - 2 ˆ 6 7 * 1 + - and then apply New-order traversal (visit root, then right subtree, then left subtree).
Step 1 — Parse the postfix expression into a tree (stack method):
Push 3, push 4. On '*', pop 4 (right) and 3 (left) → node (* 3 4).
Push 5. On '-', pop 5 (right) and (* 3 4) (left) → node (- (* 3 4) 5).
Push 2. On 'ˆ', pop 2 (right) and the previous '-' node (left) → node (ˆ (- (* 3 4) 5) 2).
Push 6, push 7. On '*', pop 7 (right) and 6 (left) → node (* 6 7).
Push 1. On '+', pop 1 (right) and (* 6 7) (left) → node (+ (* 6 7) 1).
Finally on '-', pop the '+' node (right) and the 'ˆ' node (left) → root node (- (ˆ ...) (+ ...)).
Step 2 — Summary of the resulting tree structure:
Root is '-', right subtree is '+' (left child '*' with 6 and 7, right child 1), left subtree is 'ˆ' (left child '-' which has '*' with 3 and 4 and right child 5, and right child 2).
Step 3 — Apply New-order traversal (root, right, left):
Visit root: -
Traverse right subtree '+' in New-order: visit '+', then its right child 1, then its left child '*' (visit '*', then right 7, then left 6) → + 1 * 7 6
Traverse left subtree 'ˆ' in New-order: visit 'ˆ', then its right child 2, then its left child '-' (visit '-', then right 5, then left '*' (visit '*', then right 4, then left 3)) → ˆ 2 - 5 * 4 3
Combine the parts (root, right-subtree result, left-subtree result):
Final New-order traversal: - + 1 * 7 6 ˆ 2 - 5 * 4 3
A video solution is available for this question — log in and enroll to watch it.