Infix to Postfix conversion is a critical concept in Unit 2 Notes Data Structures, forming the foundation for understanding expression evaluation, compiler design, and data structure manipulation using stacks. This topic plays a vital role in simplifying mathematical expression processing by computers, especially when dealing with expressions that include operators, operands, and parentheses. The conversion from infix notation to postfix notation enables machines to evaluate expressions more efficiently without the need for operator precedence rules or parentheses. This makes postfix (also known as Reverse Polish Notation or RPN) particularly suitable for stack-based evaluation and is widely used in interpreters and compilers.
In an infix expression, the operator is written in-between the operands (e.g., A + B). While this is naturally readable for humans, it becomes complicated for machines due to the necessity of managing operator precedence, associativity, and parentheses. In contrast, postfix expressions do not require any such rules. For instance, the postfix form of A + B is AB+, and for A + B * C, it is ABC*+. The transformation from infix to postfix is algorithmically handled using stack data structures, following specific rules regarding operators, operands, and brackets.
1. Basic Terminology and Definitions
- Infix Expression: An expression where the operator is written between two operands. Example: A + B.
- Postfix Expression (Reverse Polish Notation): An expression where the operator is written after the operands. Example: AB+.
- Operands: These are the data inputs (like variables or numbers) used in an expression.
- Operators: These symbols (+, -, *, /, ^) define the operation to be performed.
- Precedence: The priority level of an operator in an expression. Higher precedence operators are evaluated before lower ones.
- Associativity: The direction (left-to-right or right-to-left) in which operators of the same precedence are evaluated.
- Stack: A linear data structure that follows LIFO (Last-In-First-Out) principle. Used to store operators temporarily during the conversion.
2. Operator Precedence and Associativity Table
Operator | Description | Precedence | Associativity |
---|---|---|---|
^ | Exponentiation | Highest | Right-to-Left |
* / | Multiplication, Division | High | Left-to-Right |
+ – | Addition, Subtraction | Low | Left-to-Right |
3. Rules for Infix to Postfix Conversion
To convert an infix expression to postfix, we follow the below algorithm using a stack:
- Initialize an empty stack for operators and an empty output string for the result.
- Scan the infix expression from left to right.
- If the scanned character is an operand, add it to the output string.
- If the scanned character is an opening parenthesis ‘(‘, push it onto the stack.
- If the scanned character is a closing parenthesis ‘)’, pop and append all operators from the stack to the output string until an opening parenthesis is encountered. Pop and discard the opening parenthesis.
- If the scanned character is an operator:
- While the stack is not empty and the precedence of the scanned operator is less than or equal to the precedence of the operator on top of the stack (and if associativity is left-to-right), pop the operator from the stack and append to output.
- Push the scanned operator onto the stack.
- After scanning the entire expression, pop any remaining operators from the stack and append them to the output.
4. Step-by-Step Example: Infix to Postfix
Example 1: Convert A + B * C to postfix
Infix Expression: A + B * C
Step 1: Read ‘A’ β Operand β Add to output β Output: A
Step 2: Read ‘+’ β Operator β Stack is empty β Push ‘+’ β Stack: [+]
Step 3: Read ‘B’ β Operand β Add to output β Output: AB
Step 4: Read ‘‘ β Operator β Precedence of ‘‘ > ‘+’ β Push ‘*’ β Stack: [+, *]
Step 5: Read ‘C’ β Operand β Add to output β Output: ABC
Step 6: End of expression β Pop stack to output β Output: ABC*+
Postfix Expression: ABC*+
Example 2: Convert (A + B) * (C – D) to postfix
Infix: (A + B) * (C – D)
Step-by-step:
- Read ‘(‘ β Push to stack β Stack: [ ( ]
- Read ‘A’ β Output: A
- Read ‘+’ β Stack: [ (, + ]
- Read ‘B’ β Output: AB
- Read ‘)’ β Pop ‘+’ β Output: AB+ β Pop ‘(‘
- Read ‘*’ β Push to stack β Stack: [ * ]
- Read ‘(‘ β Push to stack β Stack: [ *, ( ]
- Read ‘C’ β Output: AB+C
- Read ‘-‘ β Stack: [ *, (, – ]
- Read ‘D’ β Output: AB+CD
- Read ‘)’ β Pop ‘-‘ β Output: AB+CD- β Pop ‘(‘
- End β Pop ‘‘ β Final Output: AB+CD-
Postfix: AB+CD-*
π Insert Diagram: Stack state transition for (A + B) * (C – D)
5. Pseudocode for Infix to Postfix Algorithm
Algorithm InfixToPostfix(expression)
Create an empty stack S
Create an empty output string
For each character ch in the expression:
If ch is an operand:
Add ch to output
Else If ch is '('
Push ch to S
Else If ch is ')'
While top of S is not '('
Pop from S and add to output
Pop '(' from S
Else If ch is operator:
While S is not empty and precedence(top of S) >= precedence(ch):
Pop from S and add to output
Push ch to S
While S is not empty:
Pop from S and add to output
Return output
6. Applications of Postfix Notation
Postfix expressions find major applications in the following domains:
- Compiler design for intermediate code generation.
- Evaluators in calculators, where the machine directly processes postfix without precedence rules.
- Expression tree traversal in a bottom-up manner.
- Reverse Polish Notation (RPN) calculators.
- Parsing and evaluating expressions in programming languages.
7. Evaluating Postfix Expressions
Evaluation of postfix expressions is simpler and faster. Hereβs how:
- Initialize an empty stack.
- Scan from left to right.
- If operand, push it onto stack.
- If operator, pop top two operands, apply operator, and push result.
- Final stack top is the result.
Example: Evaluate 2354+
Steps:
- Push 2 β [2]
- Push 3 β [2, 3]
-
- β 2*3 = 6 β Push 6 β [6]
- Push 5 β [6, 5]
- Push 4 β [6, 5, 4]
-
- β 5*4 = 20 β Push 20 β [6, 20]
-
- β 6+20 = 26 β Final result: 26
πΉ Short Answer Questions (2β3 Marks)
- What is infix expression? An expression where the operator is placed between operands, like A + B.
- Why is postfix expression preferred in compilers? Because it eliminates the need for precedence and associativity rules during evaluation.
- What data structure is used for infix to postfix conversion? Stack.
- What is the precedence of the ‘^’ operator? Highest precedence with right-to-left associativity.
- Define associativity with an example. Associativity defines the direction of operator evaluation. For instance, a – b – c is (a – b) – c due to left-to-right associativity.
- What is the postfix of A + B * C? ABC*+
- What is the role of parentheses in infix expressions? To override normal precedence rules.
πΈ Long Answer Questions (5β10 Marks)
- Explain the infix to postfix conversion algorithm with a complete example. Include step-by-step stack states and final output.
- Why is postfix expression easier for machines to evaluate than infix? Explain with examples and reasoning.
- Write a C program or pseudocode for converting infix to postfix. Explain how stack is used in it.
- Evaluate a given postfix expression: 82/3-42+ and show each step.*
- Draw and explain the transition of stack for the expression: (A + B) * (C – D / E)
π Chapter Summary
Infix expressions are human-readable but complex for machinesPostfix expressions remove ambiguity by avoiding precedence and parenthesesStack is the ideal data structure for converting infix to postfixConversion algorithm includes scanning, using stack for operators, and output string for operandsOperator precedence and associativity are critical in processingOperators are popped based on precedence rules and stack is used to store until appropriateEvaluating postfix is simple using stack: push operands, apply operator to top two, push resultPostfix notation is widely used in compilers, calculators, and interpreters
π Model Paper / Sample Internal Exam Questions
- Convert the following infix expression to postfix: A + B * (C ^ D – E)
- Write the algorithm for infix to postfix conversion.
- What are the advantages of postfix expression over infix?
- Evaluate the postfix expression: 345*+2-
- Explain associativity and precedence with reference to infix and postfix expressions.