Evaluation of Prefix and Postfix Expressions using stack – DSA Course playlist in C++

🔧 Evaluation Methods by Notation:

NotationEvaluation MethodNeeds Stack?
InfixConvert using precedence rules❌ (uses parsing trees, not stack)
PostfixLeft to right using a stack✅ Yes
PrefixRight to left using a stack✅ Yes

For Postfix and Prefix evaluation, a stack is typically used.

✅ 1. Evaluation of Postfix Expression

Postfix = Operators come after operands
👉 Scan left to right, use stack

✍️ Example: 5 3 2 * +

Step-by-step:

  1. Push 5 → Stack: [5]
  2. Push 3 → Stack: [5, 3]
  3. Push 2 → Stack: [5, 3, 2]
  4. * → Pop 3 & 23 * 2 = 6 → Push 6 → Stack: [5, 6]
  5. + → Pop 5 & 65 + 6 = 11 → Push 11

✅ Final Answer: 11

🧠 Key Rules Recap:

  • Operands: Push them onto the stack
  • Operators: Pop 2 operands, apply operator, push the result
  • The final result is at the top of the stack

✅ 2. Evaluation of Prefix Expression

Prefix = Operators come before operands
👉 Scan right to left, use stack

✍️ Example: + 5 * 3 2

Step-by-step:

  1. Start from right: Push 2, 3 → Stack: [2, 3]
  2. * → Pop 3 and 23 * 2 = 6 → Push 6 → Stack: [6]
  3. Push 5 → Stack: [6, 5]
  4. + → Pop 5 and 65 + 6 = 11 → Push 11

✅ Final Answer: 11

🧠 Key Rules Recap:

  • Operands: Push them onto the stack
  • Operators: Pop 2 operands, apply operator, push the result
  • The final result is at the top of the stack

✅ 3. Evaluation of Infix Expression

Infix = Operators between operands (e.g. 5 + 3 * 2)
👉 Needs operator precedence and parentheses

✍️ Example: 5 + 3 * 2

Steps:

  1. Recognize precedence: * comes before +
  2. Compute 3 * 2 = 6
  3. Then 5 + 6 = 11

✅ Final Answer: 11

📊 Summary

ExpressionTypeEvaluation MethodResult
5 + 3 * 2InfixUse precedence11
+ 5 * 3 2PrefixRight to left stack11
5 3 2 * +PostfixLeft to right stack11

Can Machines Use Infix for Evaluation?

Not directly.

Machines do not evaluate infix expressions directly — they first convert them into a form that is easier to process, such as postfix, prefix, or an abstract syntax tree (AST).

🧠 Why Not Infix?

Because infix expressions:

  • Depend on operator precedence (e.g., * before +)
  • Require parentheses to clarify order
  • Are ambiguous without a full grammar and parsing

These factors make infix hard to evaluate directly in a linear or stack-based way.

🔄 What Do Machines Do Instead?

  1. Parse the infix expression using grammar rules and precedence
  2. Build an Abstract Syntax Tree (AST) or convert to postfix/prefix
  3. Evaluate the result using:
    • A stack (postfix/prefix), or
    • Tree traversal (AST-based)

Can Machines Use both Prefix and Postfix for Evaluation?

Yes — machines can evaluate both prefix and postfix.

But postfix is more commonly used in stack-based evaluation models, especially in practical implementations like compilers, interpreters, and virtual machines.

🔄 Postfix vs. Prefix in Machines

FeaturePostfix (RPN)Prefix (Polish Notation)
Evaluation OrderLeft to rightRight to left
Needs Stack?YesYes
Ease of EvaluationSimpler for stack-based machinesSlightly trickier (right-to-left parsing)
Common UsageWidely used (e.g., JVM bytecode, calculators)Rarely used in real systems

🔧 Why Do Machines Prefer Postfix?

  • Postfix works naturally with LIFO (stack):
    • You push operands.
    • When you encounter an operator, you pop the top two operands, evaluate, and push the result.
  • Simple left-to-right scan → perfect for interpreters.

🧠 Prefix is Valid — Just Less Used

Machines can evaluate prefix:

  • Just requires scanning right to left
  • Still needs a stack
  • Less natural for left-to-right interpreters and compilers

✅ Summary

QuestionAnswer
Can machines only use postfix?❌ No — they can use prefix too
Why is postfix preferred?✔ Easier left-to-right evaluation with a stack
Is prefix used in real-world systems?❌ Rarely — mostly theoretical or academic

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top