Skip to main content

Convert A+B-C*(D-E+F/G)/H to postfix

Short answer

Scanning A+B-C*(D-E+F/G)/H with stack and precedence rules (pop higher/equal) gives postfix AB+CDE-FG/+*H/-.

Convert A + B - C * (D - E + F/G) / H expression into postfix expression using stack.

Rules: operand → output; ( → push; ) → pop to output until (; operator → pop operators of higher or equal precedence, then push; at the end pop everything. Precedence: * / above + -; equal precedence is evaluated left to right.

Infix: A + B - C * (D - E + F / G) / H

StepSymbolStack (bottom → top)Postfix output
1A(empty)A
2++A
3B+AB
4--AB+
5C-AB+C
6*-*AB+C
7(-*(AB+C
8D-*(AB+CD
9--*(-AB+CD
10E-*(-AB+CDE
11+-*(+AB+CDE-
12F-*(+AB+CDE-F
13/-*(+/AB+CDE-F
14G-*(+/AB+CDE-FG
15)-*AB+CDE-FG/+
16/-/AB+CDE-FG/+*
17H-/AB+CDE-FG/+*H
end(pop all)(empty)AB+CDE-FG/+*H/-

Answer: AB+CDE-FG/+*H/-

Check with a tree: the expression is (A+B) − ((C * (…)) / H), where the bracket is (D−E) + (F/G). Post-order of this tree gives AB+CDE-FG/+*H/-.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 2 questions →