Saturday, 26 July 2025

Polish Notation & Conversion

Polish Notation and Conversion


๐Ÿ“˜ What is Polish Notation?

Polish Notation is a way of writing arithmetic expressions where the position of the operator defines the order of operations instead of using parentheses.

There are two main types:

  • ✅ Prefix Notation (Polish): Operator comes before operands.
  • ✅ Postfix Notation (Reverse Polish): Operator comes after operands.

๐Ÿ”น Basic Terms


๐Ÿ”ธ Operands

These are the values or variables used in expressions.

  • ๐Ÿงพ Examples: A, B, 10, x

๐Ÿ”ธ Operators

These are symbols that represent operations performed on operands.

  • ๐Ÿงพ Examples: +, -, *, /, ^

๐Ÿ”ธ Operator Precedence Table

Operator Meaning Precedence Associativity
^ Exponentiation Highest Right to Left
* / Multiplication / Division Medium Left to Right
+ - Addition / Subtraction Lowest Left to Right

⚠ Parentheses () are used in infix notation to override precedence.


๐Ÿ”ธ Operator Priorities for Stack-Based Conversions

When converting infix expressions to postfix or prefix using a stack, we often use two different priority values for operators: In-Stack Priority (or In-Stack Precedence) and Incoming Priority (or Incoming Precedence).

  • In-Stack Priority: The priority of an operator when it is already on the stack.
  • Incoming Priority: The priority of an operator when it is read from the input expression.

This distinction is crucial for handling parentheses and right-to-left associativity correctly during the pop/push decisions.

Symbol In-Stack Priority Incoming Priority
+ , - 2 1
* , / 4 3
^ 5 6
( 0 9
) 0
End of Expression (or bottom of stack) -1 -1

Note: The specific numerical values can vary, but their relative order and the distinction between in-stack and incoming are key. A higher number typically means higher precedence. Parentheses have unique rules: an incoming '(' generally has a very high priority to ensure it's pushed, while an in-stack '(' has a very low priority to ensure operators are popped until it's found. An incoming ')' has a priority that triggers popping until an in-stack '(' is found.


๐Ÿงพ Expression Notations

Infix Prefix (Polish) Postfix (Reverse Polish)
A + B + A B A B +
A + B * C + A * B C A B C * +
(A + B) * C * + A B C A B + C *

๐Ÿ” Conversion Using Stack


✅ Infix to Postfix (Reverse Polish Notation)


๐Ÿง  Algorithm (using Stack):

  1. Scan the expression from left to right.
  2. If the symbol is an operand, add to result.
  3. If it's an operator:
    Pop operators from the stack to the result until you find one whose In-Stack Priority is less than the Incoming Priority of the current operator. Then push the current operator.
  4. If it's '(', push it onto the stack (its Incoming Priority is high).
  5. If it's ')', pop operators and add to result until '(' is found on the stack. Discard both parentheses.
  6. At end of expression, pop all remaining operators from the stack to the result.

๐Ÿ“Œ Example:

Convert: (A + B) * C

Step Stack Output
( (
A ( A
+ (+ A
B (+ A B
) A B +
* * A B +
C * A B + C
End A B + C *

✅ Final Postfix: A B + C *


✅ Infix to Prefix (Polish Notation)


๐Ÿง  Steps:

  1. Reverse the infix expression.
  2. Swap '(' with ')' and vice versa.
  3. Apply Infix to Postfix steps on this modified expression (using the new priority rules).
  4. Reverse the resulting expression to get Prefix.

๐Ÿ“Œ Example:

Convert: (A + B) * C

  • Step 1: ReverseC * (B + A)
  • Step 2: Swap bracketsC * ) B + A (
  • Step 3: Convert to postfix: C B A + * (Apply Infix to Postfix algorithm here)
  • Step 4: Reverse* + A B C

✅ Final Prefix: * + A B C


๐Ÿง  Quick Summary

Task What to do
Infix to Postfix Use stack, precedence, L→R scan
Infix to Prefix Reverse → Swap → Postfix → Reverse again

Would you like a flowchart or C/Python code for this conversion?

No comments:

Post a Comment

The Building Blocks of IoT: Embedded Devices, Sensors, and Actuators ๐Ÿง  The Building Blocks of IoT: Embedded D...

Popular Posts