- 2071 Bhadra · 6 marks
Expression tree and traversals
Short answer
Expression tree: root '+', left subtree '-', right subtree '/'. Preorder (prefix): + - + a * b c d / * e f g. Inorder (infix): a + b * c - d + e * f / g. The tree respects precedence: * and / first, then + and - left-to-right.
Construct expression tree for (a+(bc)-d+(ef/g)) and traverse it according to pre-order and in-order traversal algorithm.
An expression tree is a binary tree whose leaves are operands and whose internal nodes are operators. The expression is (a + (b*c) − d + (e*f/g)). Operators * and / have higher precedence than + and −, and operators of equal precedence are evaluated left to right. So it groups as:
((a + (bc)) − d) + ((ef) / g)
Expression tree
+
/ \
- /
/ \ / \
+ d * g
/ \ / \
a * e f
/ \
b c
Pre-order traversal (root, left, right) gives prefix form
+ - + a * b c d / * e f g
In-order traversal (left, root, right) gives infix form
a + b * c - d + e * f / g
(Parentheses are needed to show the grouping: ((a+(bc))−d)+((ef)/g).)
For comparison, post-order gives a b c * + d - e f * g / +.
Traversal steps (pre-order)
Visit +(root) → left - → its left + → a → * → b, c → d → right / → * → e, f → g.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Binary tree traversals; preorder of BST · Data Structure and Algorithm, chapter 6
- B-tree of order 5 for 82, 12, 22, ... · Data Structure and Algorithm, chapter 6 · asked 2×
- B-tree of order 5 for letters C N G A H ... · Data Structure and Algorithm, chapter 6 · asked 2×
- B-tree properties; order 5 for 3, 14, 7, ... · Data Structure and Algorithm, chapter 6 · asked 2×
- B-tree of order 3 for 52, 46, 27, ... · Data Structure and Algorithm, chapter 6
- TOH algorithm and recursion tree · Data Structure and Algorithm, chapter 5 · asked 2×
- Decision tree classifier and Hunt's algorithm · Data Mining, chapter 3