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)
  • 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)

Stack Operation Rules for Infix to Postfix Conversion

  1. 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).
  2. If the scanned character is an opening parenthesis (:

    • Push it onto the stack.
  3. 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.
  4. 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).
  5. 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

  1. Scan the expression from left to right.

  2. Operands (variables or constants):

    • If the scanned character is an operand, push it onto the stack.
  3. 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.
  4. 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*
    1. Read A → Push: [A]
    2. Read B → Push: [A, B]
    3. Read + → Pop B, A: form (A + B), Push: [(A + B)]
    4. Read C → Push: [(A + B), C]
    5. Read * → Pop C, (A + B): form ((A + B) * C);
Answer: Infix Conversion Result
  • Infix: ((A + B) * C)

Rules for Converting Prefix to Infix

  1. Scan the expression from right to left.

  2. Operands:

    • If the scanned character is an operand, push it onto the stack.
  3. 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.
  4. After scanning the entire prefix expression:

    • The element in the stack is the infix expression.

Example: Prefix to Infix Conversion

  • Given Prefix: *+ABC
    1. Read C → Operand → Push onto stack → [C]
    2. Read B → Operand → Push onto stack → [B, C]
    3. Read A → Operand → Push onto stack → [A, B, C]
    4. Read + → Operator
    5. Pop top two operands A and B
    6. Form infix: (A + B)
    7. Push result back → [(A + B), C]
    8. Read * → Operator
    9. Pop top two operands (A + B) and C
    10. Form infix: ((A + B) * C);
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

  1. Step 1: Reverse the Infix Expression:
    • Given: (A + B) * C
    • Reversed: C * (B + A)
  2. Step 2: Convert Reversed Infix to Postfix:
    • Infix: C * (B + A)
    • Postfix (using stack method): C B A + *
  3. Step 3: Reverse the Postfix Expression:
    • Reverse C B A + * → * + A B C
    • Prefix Expression: * + A B C

Example 2: Convert A + B * C [Infix] to Prefix

  1. Step 1: Reverse the Infix Expression:
    • Given: A + B * C
    • Reversed: C * B + A
  2. Step 2: Convert Reversed Infix to Postfix:
    • Infix: C * B + A
    • Postfix: C B * A +
  3. Step 3: Reverse the Postfix Expression:
    • Reverse C B * A + → + A * B C
    • Prefix Expression: + A * B C