---
title: "Convert expression to prefix and postfix"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:infix-prefix-postfix-tabular"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/infix-prefix-postfix-tabular
site: BCT Engineering
---

# Convert expression to prefix and postfix

Data Structure and Algorithm · Chapter 2: The Stack and Queue

- Asked in: 2069 Poush, 6 marks
- Video: [3.4 Infix Prefix and Postfix expressions | Data Structures Tutorials](https://www.youtube.com/watch?v=RY4GkLahbCI) — 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/infix-prefix-postfix-tabular
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Convert the given infix expression (A+(B*C-(D/E-F)*G)*H) into equivalent prefix and postfix expressions showing each step in tabular form.

## Short answer

Postfix: ABC*DE/F-G*-H*+. Prefix: +A*-*BC*-/DEFGH.

## Answer

Infix: `A + ( B * C − ( D / E − F ) * G ) * H`.

#### Postfix (left to right)

| Step | Symbol | Stack (bottom → top) | Postfix output |
|---|---|---|---|
| 1 | A | (empty) | A |
| 2 | + | + | A |
| 3 | ( | +( | A |
| 4 | B | +( | AB |
| 5 | \* | +(\* | AB |
| 6 | C | +(\* | ABC |
| 7 | - | +(- | ABC\* |
| 8 | ( | +(-( | ABC\* |
| 9 | D | +(-( | ABC\*D |
| 10 | / | +(-(/ | ABC\*D |
| 11 | E | +(-(/ | ABC\*DE |
| 12 | - | +(-(- | ABC\*DE/ |
| 13 | F | +(-(- | ABC\*DE/F |
| 14 | ) | +(- | ABC\*DE/F- |
| 15 | \* | +(-\* | ABC\*DE/F- |
| 16 | G | +(-\* | ABC\*DE/F-G |
| 17 | ) | + | ABC\*DE/F-G\*- |
| 18 | \* | +\* | ABC\*DE/F-G\*- |
| 19 | H | +\* | ABC\*DE/F-G\*-H |
| end | (pop all) | (empty) | ABC\*DE/F-G\*-H\*+ |

**Postfix:** `ABC*DE/F-G*-H*+`

#### Prefix

Reverse the infix string, swap `(` and `)`, convert it like postfix (an incoming operator pops only operators of *strictly higher* precedence), then reverse the result.

Reversed with swapped brackets: `H * ( G * ( F − E / D ) − C * B ) + A`.

| Step | Symbol (reversed) | Stack | Output (before reversing) |
|---|---|---|---|
| 1 | H | (empty) | H |
| 2 | \* | \* | H |
| 3 | ( | \*( | H |
| 4 | G | \*( | HG |
| 5 | \* | \*(\* | HG |
| 6 | ( | \*(\*( | HG |
| 7 | F | \*(\*( | HGF |
| 8 | - | \*(\*(- | HGF |
| 9 | E | \*(\*(- | HGFE |
| 10 | / | \*(\*(-/ | HGFE |
| 11 | D | \*(\*(-/ | HGFED |
| 12 | ) | \*(\* | HGFED/- |
| 13 | - | \*(- | HGFED/-\* |
| 14 | C | \*(- | HGFED/-\*C |
| 15 | \* | \*(-\* | HGFED/-\*C |
| 16 | B | \*(-\* | HGFED/-\*CB |
| 17 | ) | \* | HGFED/-\*CB\*- |
| 18 | + | + | HGFED/-\*CB\*-\* |
| 19 | A | + | HGFED/-\*CB\*-\*A |
| end | (pop all) | (empty) | HGFED/-\*CB\*-\*A+ |

Reversing the output gives **Prefix:** `+A*-*BC*-/DEFGH`

Check: `+A*-*BC*-/DEFGH` read from the right with a stack gives the same expression tree as the postfix `ABC*DE/F-G*-H*+`.

## Check yourself

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

1. When the symbol ')' is scanned during infix to postfix conversion, what happens?
   A. It is pushed on the stack
   B. Operators are popped to output until '(' is reached, and '(' is discarded
   C. The whole stack is emptied
   D. It is added to the output
   Answer: B. A closing bracket pops operators until the matching opening bracket, which is removed. (Key point: Postfix stack rule)

2. What is the postfix of A+(B*C-(D/E-F)*G)*H?
   A. ABC*DE/F-G*-H*+
   B. ABC*DEF/-G*-H*+
   C. AB*C-DE/F-G*H*+
   D. ABC*DE/F-G*H*--+
   Answer: A. The table gives ABC*DE/F-G*-H*+. (Key point: Postfix result)

3. What is the first step for converting infix to prefix using the method shown?
   A. Convert directly left to right with no change
   B. Reverse the infix string and swap '(' with ')'
   C. Evaluate the expression
   D. Convert to postfix and keep it as is
   Answer: B. The infix is reversed with brackets swapped, converted like postfix, then the result is reversed. (Key point: Prefix method)

4. In the prefix conversion, an incoming operator pops stack operators of which precedence?
   A. Greater than or equal
   B. Strictly higher only
   C. Any operator
   D. Strictly lower only
   Answer: B. For prefix the incoming operator pops only operators of strictly higher precedence. (Key point: Operator popping in prefix)

5. What is the prefix expression obtained?
   A. +A*-*BC*-/DEFGH
   B. +A*-*BC*-/DEFHG
   C. A+*-*BC*-/DEFGH
   D. +A*-*BC-*/DEFGH
   Answer: A. Reversing HGFED/-*CB*-*A+ gives +A*-*BC*-/DEFGH. (Key point: Final prefix)

## Related in Data Structure and Algorithm

- [Convert expression to prefix and postfix](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aprefix-postfix-abcdefgk) — Data Structure and Algorithm, chapter 2
- [Importance of postfix; convert expression](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apostfix-importance-conversion) — Data Structure and Algorithm, chapter 2
- [Algorithm for infix to postfix conversion](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Ainfix-postfix-algorithm) — Data Structure and Algorithm, chapter 2, asked 3×
- [Evaluate postfix expression 574-*8/4+](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apostfix-evaluate-574) — Data Structure and Algorithm, chapter 2
- [Infix to postfix and evaluation](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Ainfix-postfix-explain-evaluate) — Data Structure and Algorithm, chapter 2
- [Convert and evaluate A/B-(C+D)*E](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apostfix-evaluate-ab-slash-cd-e) — Data Structure and Algorithm, chapter 2, asked 2×
