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):
- Scan the expression from left to right.
- If the symbol is an operand, add to result.
- 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. - If it's '(', push it onto the stack (its Incoming Priority is high).
- If it's ')', pop operators and add to result until '(' is found on the stack. Discard both parentheses.
- 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:
- Reverse the infix expression.
- Swap '(' with ')' and vice versa.
- Apply Infix to Postfix steps on this modified expression (using the new priority rules).
- Reverse the resulting expression to get Prefix.
๐ Example:
Convert: (A + B) * C
- Step 1: Reverse →
C * (B + A) - Step 2: Swap brackets →
C * ) 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