stacks rules
Operator Precedence and Associativity
Operator Precedence: Indicates the order in which operators are evaluated in expressions.
- Highest:
^(exponentiation) - High:
*,/,%(multiplication, division, modulo) - Low:
+,-(addition, subtraction) - Control:
( )(parentheses are used to override precedence)
- Highest:
Associativity: Defines the order of operations when operators of the same precedence are present.
- Right-to-left:
^(evaluated from right to left) - Left-to-right:
*, /, +, -(evaluated from left to right)
- Right-to-left:
Stack Operation Rules for Infix to Postfix Conversion
If the scanned character is an operand:
- Append it directly to the output (postfix expression).
- Note: Operands can be variables or numbers (e.g.,
a,b,1,2).
If the scanned character is an opening parenthesis
(:- Push it onto the stack.
If the scanned character is an operator (e.g.,
+,-,*,/,^):- Stack is empty: Push the operator onto the stack.
- Top of stack is an opening parenthesis: Push the operator onto the stack.
- Operator precedence rules:
- If the incoming operator has a higher precedence than the operator on top of the stack, push the operator onto the stack.
- If it has lower precedence, pop all operators from the stack until an operator with lower precedence or an opening parenthesis is found, then push the incoming operator onto the stack.
- If it has equal precedence and is left-associative (
+,-,*,/): Pop all operators until a lower precedence operator or an opening parenthesis is found and then push the incoming operator onto the stack. - Reason: Left-associative operators are evaluated from left to right.
- If it has equal precedence and is right-associative (
^): Push the incoming operator onto the stack without popping. - Reason: Right-associative operators (like exponentiation) are evaluated from right to left.
If the scanned character is a closing parenthesis
):- Pop from the stack to append operators to the postfix expression until an opening parenthesis is encountered.
- Remove the opening parenthesis from the stack (do not add it to the postfix expression).
After processing the entire infix expression:
- Pop all remaining operators from the stack and append them to the postfix expression.
- Important: If any parentheses remain in the stack, the expression is invalid.
Rules for Converting Postfix to Infix
Scan the expression from left to right.
Operands (variables or constants):
- If the scanned character is an operand, push it onto the stack.
Operators (e.g.,
+,-,*,/, etc.):- If the scanned character is an operator:
- Pop the top two elements from the stack.
- Form an infix expression by placing the operator between the two popped operands with parentheses:
- Infix:
(operand1 operator operand2) - Push the resulting infix expression back onto the stack.
- If the scanned character is an operator:
After scanning the entire postfix expression:
- The element left in the stack is the required infix expression.
Example: Postfix to Infix Conversion
- Given Postfix:
AB+C*- Read
A→ Push:[A] - Read
B→ Push:[A, B] - Read
+→ PopB,A: form(A + B), Push:[(A + B)] - Read
C→ Push:[(A + B), C] - Read
*→ PopC,(A + B): form((A + B) * C);
- Read
Answer: Infix Conversion Result
- Infix:
((A + B) * C)
Rules for Converting Prefix to Infix
Scan the expression from right to left.
Operands:
- If the scanned character is an operand, push it onto the stack.
Operators:
- If the scanned character is an operator:
- Pop the top two elements from the stack.
- Form an infix expression:
- Infix:
(operand1 operator operand2) - Push the resulting infix expression back onto the stack.
- If the scanned character is an operator:
After scanning the entire prefix expression:
- The element in the stack is the infix expression.
Example: Prefix to Infix Conversion
- Given Prefix:
*+ABC- Read
C→ Operand → Push onto stack →[C] - Read
B→ Operand → Push onto stack →[B, C] - Read
A→ Operand → Push onto stack →[A, B, C] - Read
+→ Operator - Pop top two operands
AandB - Form infix:
(A + B) - Push result back →
[(A + B), C] - Read
*→ Operator - Pop top two operands
(A + B)andC - Form infix:
((A + B) * C);
- Read
Answer: Infix Conversion Result
- Infix:
((A + B) * C)
Infix to Prefix and Infix to Postfix Conversion Examples
Example 1: Convert (A + B) * C [Infix] to Prefix
- Step 1: Reverse the Infix Expression:
- Given:
(A + B) * C - Reversed:
C * (B + A)
- Given:
- Step 2: Convert Reversed Infix to Postfix:
- Infix:
C * (B + A) - Postfix (using stack method):
C B A + *
- Infix:
- Step 3: Reverse the Postfix Expression:
- Reverse
C B A + *→* + A B C - Prefix Expression:
* + A B C
- Reverse
Example 2: Convert A + B * C [Infix] to Prefix
- Step 1: Reverse the Infix Expression:
- Given:
A + B * C - Reversed:
C * B + A
- Given:
- Step 2: Convert Reversed Infix to Postfix:
- Infix:
C * B + A - Postfix:
C B * A +
- Infix:
- Step 3: Reverse the Postfix Expression:
- Reverse
C B * A +→+ A * B C - Prefix Expression:
+ A * B C
- Reverse