Evaluating arithmetic expressions is a fundamental problem in computer science. This problem can be efficiently solved using the stack data structures. Stack can operate on the last in First Out(LIFO) principle which is particularly useful for parsing expressions and ensuring the proper operator precedence and associativity.
The main concept behind using the stacks for evaluating the arithmetic expressions involves converting the expression into a format that can be easily processed.
Postfix (Reverse Polish Notation)Postfix notation is also known as Reverse Polish Notation. It is a mathematical notation in which every operator follows all of its operands. It does not require any parentheses as long as the number of operands for each operator is fixed/
Characteristics:- Operator Placements: Operators can be placed after their operands in the expressions.
- Evaluation Order: Evaluated from left to right then applying the operators to the most recent operands of the expression.
Example: For the infix expression 2+3*5Conversion to Postfix- The infixTopostfix method can process each token of the infix expression.
- Operands are directly added to the result.
- Operators can be pushed onto the stack after ensuring the proper precedence handling.
Token
| Output List
| Operator Stack
| Action
|
---|
2
|
2
|
| Add the operator to the output
|
+
|
2
|
+
| Push the operator to stack
|
3
|
2,3
|
+
| Add the operand to output.
|
*
|
2,3
|
+, *
| Push the operator onto the stack.
|
5
|
2,3,5
|
+, *
| Add the operand to output.
|
|
2,3,5,*
|
+
| Pop the stack to output util the lower precedence (*)
|
|
2,3,5,*,+
|
| Pop remaining the operators to output(+).
|
Result of conversion : 2 3 5 * +
Evaluation of Postfix Expression- The evaluatePostfix method be processes the each token of the postfix expression.
- Operands are pushed onto the stack.
- When the operator is encountered then the top two operands are popped, the operation is performed and the result is pushed back onto the stack.
Token
| Operand Stack
| Action
|
---|
2
|
[2]
| Push the operand onto stack
|
3
|
[2, 3]
| Push the operand onto stack
|
4
|
[2,3,5]
| Push the operand onto stack
|
*
|
[2,15]
| Pop 3 and 15 push 3*5 =15
|
+
|
[17]
| Pop 2 and 15 pop 2 + 15 = 17
|
Result of Evaluation : 17
Program to Evaluate an Expression Using StacksBelow is the implementation of Program to evaluate an expression using stacks:
Java
import java.util.Stack;
public class ExpressionEvaluator {
// Method to evaluate a postfix expression
public static int evaluatePostfix(String expression) {
// Stack to hold operands
Stack<Integer> stack = new Stack<>();
// Iterate over each character in the postfix expression
for (char token : expression.toCharArray()) {
// If the character is a digit, push it to the stack
if (Character.isDigit(token)) {
stack.push(token - '0');
} else {
// If the character is an operator,
// pop two operands from the stack
int operand2 = stack.pop();
int operand1 = stack.pop();
// Perform the operation and push
// The result back to the stack
switch (token) {
case '+':
stack.push(operand1 + operand2);
break;
case '-':
stack.push(operand1 - operand2);
break;
case '*':
stack.push(operand1 * operand2);
break;
case '/':
stack.push(operand1 / operand2);
break;
}
}
}
// The result of the expression is the last element in the stack
return stack.pop();
}
// Method to convert an infix expression to a postfix expression
public static String infixToPostfix(String expression) {
// String to hold the postfix expression
StringBuilder result = new StringBuilder();
// Stack to hold operators and parentheses
Stack<Character> stack = new Stack<>();
// Iterate over each character in the infix expression
for (char token : expression.toCharArray()) {
// If the character is an operand, append it to the result
if (Character.isLetterOrDigit(token)) {
result.append(token);
} else if (token == '(') {
// If the character is '(', push it to the stack
stack.push(token);
} else if (token == ')') {
// If the character is ')', pop and
// append from the stack until '(' is found
while (!stack.isEmpty() && stack.peek() != '(') {
result.append(stack.pop());
}
stack.pop(); // Remove the '(' from the stack
} else {
// If the character is an operator, pop and append from the stack while
// the precedence of the operator is less than or equal to the precedence
// of the operator at the top of the stack
while (!stack.isEmpty() && precedence(token) <= precedence(stack.peek())) {
result.append(stack.pop());
}
// Push the current operator to the stack
stack.push(token);
}
}
// Pop all remaining operators from the stack and append to the result
while (!stack.isEmpty()) {
result.append(stack.pop());
}
return result.toString();
}
// Method to define the precedence of operators
private static int precedence(char operator) {
switch (operator) {
case '+':
case '-':
return 1; // Precedence of + and - is 1
case '*':
case '/':
return 2; // Precedence of * and / is 2
}
return -1; // Default precedence (should not be reached)
}
// Main method to test the functionality
public static void main(String[] args) {
String infix = "2+(3*5)";
String postfix = infixToPostfix(infix);
System.out.println("Postfix Expression: " + postfix);
System.out.println("Evaluated Result: " + evaluatePostfix(postfix));
}
}
OutputPostfix Expression: 235*+
Evaluated Result: 17
Space and Time ComplexityConversion to Postfix- Time Complexity: O(n) where n is the length of the infix expression
- Space Complexity: O(n) due to space required for stack and output list.
Evaluation of Postfix- Time Complexity: O(n) where n is the length of the postfix expression.
- Space Complexity: O(n), due to space required for the stack.
Real-Time Applications- Compliers: It can be used for the parsing and evaluating expressions in the programming languages.
- Calculators: It can be handheld and software calculators use the similar techniques to the evaluate expressions.
- Expression Evaluators in Databases: Query optimizes the evaluate the expressions for the filtering and sorting data.
- Spreadsheet Software: It can be used to evaluate cell formulas.
ConclusionUsing the stacks to evaluate arithmetic expressions is the robust and efficient approach. The conversion to the postfix ensures that operator precedence and associativity are handled correctly. It can making the evaluation straightforward. Understanding this method can plays the crucial for the various real time applications including the compliers, calculators and databases.
|