NOIDATUT FREE E LEARNING PLATFORM • AKTU B.TECH

Application of stack: Prefix and Postfix Expressions, Evaluation of postfix expression

Stacks efficiently handle Prefix and Postfix notations, eliminating parentheses and precedence rules. To evaluate a postfix expression, scan left to right: push operands, pop top two for operators, apply the operator, and push the result back.


Theory & Explanation

Applications of Stack: Infix, Prefix & Postfix Expressions and Postfix Evaluation

1. Why Use Stacks for Expressions?

A stack (LIFO: Last-In, First-Out) is widely used by compilers and calculators to parse and evaluate arithmetic expressions. Standard human-readable expressions require rules for operator precedence (such as and / before + and -), associativity (left-to-right or right-to-left), and parentheses () to override precedence.

Prefix and Postfix notations remove the need for parentheses and precedence rules during evaluation, making expression evaluation clean and unambiguous using a stack.

2. Definitions & Examples of Notations

  • Infix Notation: The operator is placed between its two operands. Example: (A + B) C or 5 + 3 2. Easy for humans to read, but needs precedence and parentheses rules for evaluation.
  • Prefix Notation (Polish Notation): The operator is placed before its operands. Example: Infix (A + B) C becomes + A B C.
  • Postfix Notation (Reverse Polish Notation / RPN): The operator is placed after its operands. Example: Infix (A + B) C becomes A B + C .
Infix Expression Prefix Expression Postfix Expression
A + B + A B A B +
(A + B) C + A B C A B + C
A + B C + A B C A B C +
(A - B) / (C + D) / - A B + C D A B - C D + /

3. Operator Precedence & Associativity (Exam Quick Reference)

  • Highest Precedence: Exponentiation (^ or $) — typically Right-to-Left associative.
  • Medium Precedence: Multiplication () and Division (/) — Left-to-Right associative.
  • Lowest Precedence: Addition (+) and Subtraction (-) — Left-to-Right associative.
  • Parentheses ( and ): Group sub-expressions and override default precedence.

4. Expression Conversions (Algorithms & Examples)

A. Infix to Postfix Conversion (Using Stack)

  1. Scan the infix expression from left to right.
  2. If the token is an operand (variable/number), append it directly to the postfix output.
  3. If the token is (, push it onto the stack.
  4. If the token is ), pop and output operators until ( is encountered; discard both ( and ).
  5. If the token is an operator, while the stack is non-empty and top of stack has higher or equal precedence (respecting right-associativity for ^), pop to output; then push the current operator.
  6. After scanning all tokens, pop and output any remaining operators from the stack.

Example Trace — Convert Infix A + B C to Postfix:

  • Read A (operand) → Output: A, Stack: []
  • Read + (operator) → Stack empty → Output: A, Stack: [+]
  • Read B (operand) → Output: A B, Stack: [+]
  • Read (operator) → has higher precedence than + → Output: A B, Stack: [+, ]
  • Read C (operand) → Output: A B C, Stack: [+, ]
  • End of string → Pop remaining: then + → Final Postfix: A B C +.

B. Infix to Prefix Conversion (Reverse-Postfix-Reverse Method)

  1. Reverse the infix expression and swap ( with ) and ) with (.
  2. Convert the modified expression to postfix (adjusting operator popping condition so equal precedence is handled cleanly for right-to-left scan).
  3. Reverse the resulting postfix string to get the prefix expression.

Example Trace — Convert Infix (A + B) C to Prefix:

  • Step 1 (Reverse & swap brackets): (A + B) C → reversed: C ) B + A ( → swapped: C ( B + A ).
  • Step 2 (Postfix of C ( B + A )): C B A + .
  • Step 3 (Reverse result): + A B C (Prefix).

C. Postfix to Infix Conversion (Using Stack)

  1. Scan postfix from left to right.
  2. If operand, push it onto the stack.
  3. If operator, pop top two operands: op2 = pop() (right operand) and op1 = pop() (left operand). Form "(op1 operator op2)" and push it back.

Example: Postfix A B + C → A pushed, B pushed, + pops B, A → pushes (A + B); C pushed, pops C, (A + B) → pushes ((A + B) C).

D. Prefix to Infix Conversion (Using Stack)

  1. Scan prefix from right to left.
  2. If operand, push it onto the stack.
  3. If operator, pop top two operands: op1 = pop() (left operand) and op2 = pop() (right operand). Form "(op1 operator op2)" and push it back.

Example: Prefix + A B C (scan right-to-left) → push C, B, A; see + → pop A, B → push (A + B); see → pop (A + B), C → push ((A + B) C).

E. Postfix to Prefix & Prefix to Postfix

  • Postfix to Prefix: Scan left-to-right; on operand, push; on operator, pop op2 then op1, push operator + op1 + op2. Example: A B + → + A B.
  • Prefix to Postfix: Scan right-to-left; on operand, push; on operator, pop op1 then op2, push op1 + op2 + operator. Example: + A B → A B +.

5. Evaluation of a Postfix Expression

To evaluate a postfix expression, scan from left to right using a stack:

  1. If the scanned token is an operand, push its numeric value onto the stack.
  2. If the scanned token is an operator, pop the top two elements: first operand2 (right operand), then operand1 (left operand).
  3. Compute result = operand1 (operator) operand2 (note order for subtraction op1 - op2 and division op1 / op2).
  4. Push result back onto the stack.
  5. After scanning the entire expression, the single value remaining on top of the stack is the final answer.

Numerical Example Trace — Evaluate Postfix 2 3 1 + 9 -:

Token Action Stack (Bottom → Top)
2 Operand → Push 2 [2]
3 Operand → Push 3 [2, 3]
1 Operand → Push 1 [2, 3, 1]
Operator → Pop 1 (op2), Pop 3 (op1); 3 * 1 = 3; Push 3 [2, 3]
+ Operator → Pop 3 (op2), Pop 2 (op1); 2 + 3 = 5; Push 5 [5]
9 Operand → Push 9 [5, 9]
- Operator → Pop 9 (op2), Pop 5 (op1); 5 - 9 = -4; Push -4 [-4]

Final Result: -4 (Time Complexity: O(n), Space Complexity: O(n) where n is the number of tokens).

Diagrams

application-of-stack-prefix-and-postfix-expressions-evaluation-of-postfix-expression - Diagram

application-of-stack-prefix-and-postfix-expressions-evaluation-of-postfix-expression - Diagram

Advertisement
Advertisement