Given an expression (A+B*D)/(E-F)+G A. The prefix notation is +/+A*DB-EFG B.…
2022
Given an expression
(A+B*D)/(E-F)+G
A. The prefix notation is +/+A*DB-EFG
B. The prefix expression is the reverse of the postfix expression
C. The order of operands in infix expression and postfix expression are the same.
D. The order of operands in infix expression, prefix expression and postfix expression are the same.
Choose the most appropriate answer from the options given below:
Answer: B. C and D Only — Concept: An expression tree has operators at internal nodes and operands at the leaves, and its leaves are always encountered left to right in the same order…
- A.
A, C and D Only
- B.
C and D Only
- C.
A and B Only
- D.
A and D Only
Attempted by 667 students.
Show answer & explanation
Correct answer: B
Concept:
An expression tree has operators at internal nodes and operands at the leaves, and its leaves are always encountered left to right in the same order no matter how the tree is traversed. A preorder traversal (root, then left subtree, then right subtree) produces the prefix form; a postorder traversal (left subtree, then right subtree, then root) produces the postfix form. Because both traversals always visit a node's left subtree before its right subtree, the operands appear in the same left-to-right order in infix, prefix, and postfix -- only the position of the operators changes. Reversing a postfix string token-by-token is a different operation from a preorder traversal, so it does not, in general, reproduce the prefix form.
Application:
For (A + B * D)/(E - F) + G, build the expression tree: the root is '+' with left child '/' and right child G; the '/' node has left child '+' (with children A and 'B*D') and right child '-' (with children E and F).
Preorder (prefix) traversal of the tree: + / + A * B D - E F G.
Postorder (postfix) traversal of the tree: A B D * + E F - / G +.
Compare with the given statement's prefix, +/+A*DB-EFG: it writes the multiplication operands as D then B, but the preorder traversal above places B then D -- so the stated prefix does not match the tree's actual preorder output.
Notation | Operand order (left to right) |
|---|---|
Infix | A, B, D, E, F, G |
Prefix | A, B, D, E, F, G |
Postfix | A, B, D, E, F, G |
Cross-check:
Reverse the correct postfix string token by token: A B D * + E F - / G + reversed is + G / - F E + * D B A. This is not the same sequence as the actual prefix (+ / + A * B D - E F G) -- the operator positions and operand order both come out different, confirming that prefix is not simply the reverse of postfix for this expression.
Conclusion:
The operand order is preserved across infix, prefix and postfix (so both operand-order statements hold), the given prefix notation is misordered, and prefix is not the reverse of postfix. So the correct selection is the pairing that keeps only the two operand-order statements.
A video solution is available for this question — log in and enroll to watch it.