---
title: "Expression tree and traversals"
subject: "Data Structure and Algorithm"
chapter: 6
key: "semester_4/data_structure_and_algorithm:expression-tree-traversal"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/expression-tree-traversal
site: BCT Engineering
---

# Expression tree and traversals

Data Structure and Algorithm · Chapter 6: Trees

- Asked in: 2071 Bhadra, 6 marks
- Video: [3.12 Expression Trees | Binary Expression Tree | Data Structures Tutorials](https://www.youtube.com/watch?v=2Z6g3kNymd0) — Jenny's Lectures CS IT
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/expression-tree-traversal
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-6

## Question

Construct expression tree for (a+(b*c)-d+(e*f/g)) and traverse it according to pre-order and in-order traversal algorithm.

## 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.

## Answer

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 + (b*c)) − d) + ((e*f) / g)

#### Expression tree
```text
                  +
             /         \
            -           /
          /   \        /  \
         +     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+(b*c))−d)+((e*f)/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`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the expression group by precedence?
   A. (a+b)*(c-d)+e*(f/g)
   B. a+((b*c)-(d+e))*f/g
   C. (a+(b*c)-d+e)*(f/g)
   D. ((a+(b*c))-d)+((e*f)/g)
   Answer: D. * and / bind tighter and equal precedence goes left to right. (Key point: Grouping)

2. What is the root of the expression tree?
   A. The first -
   B. The final +
   C. The /
   D. The leaf a
   Answer: B. The last operation to be performed forms the root. (Key point: Root)

3. What does preorder give?
   A. a + b * c - d + e * f / g
   B. a b c * + d - e f * g / +
   C. + a - * b c d
   D. + - + a * b c d / * e f g
   Answer: D. Preorder of an expression tree gives prefix form. (Key point: Preorder)

4. Why does inorder need parentheses?
   A. Inorder is prefix
   B. Plain inorder loses the grouping
   C. Operators are missing
   D. It visits leaves only
   Answer: B. Inorder gives a + b * c - d + e * f / g, which needs parentheses to show the tree structure. (Key point: Inorder)

## Related in Data Structure and Algorithm

- [Binary tree traversals; preorder of BST](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abinary-tree-traversal-methods-preorder) — Data Structure and Algorithm, chapter 6
- [B-tree of order 5 for 82, 12, 22, ...](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abtree-order5-82-12-22) — Data Structure and Algorithm, chapter 6, asked 2×
- [B-tree of order 5 for letters C N G A H ...](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abtree-order5-cngahekq) — Data Structure and Algorithm, chapter 6, asked 2×
- [B-tree properties; order 5 for 3, 14, 7, ...](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abtree-properties-order5-3-14) — Data Structure and Algorithm, chapter 6, asked 2×
- [B-tree of order 3 for 52, 46, 27, ...](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abtree-order3-52-46-27) — Data Structure and Algorithm, chapter 6
- [TOH algorithm and recursion tree](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Atoh-algorithm-recursion-tree) — Data Structure and Algorithm, chapter 5, asked 2×

## Related in other subjects

- [Decision tree classifier and Hunt's algorithm](https://www.bctengineer.com/question.md?key=semester_7%2Fdata_mining%3Adecision-tree-hunt) — Data Mining, chapter 3
