How can we evaluate infix expressions in Data Structures and Algorithms (DSA)?
Loading
How can we evaluate infix expressions in Data Structures and Algorithms (DSA)?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Oct 8, 2024, 2:02 AM EDT
To evaluate infix expressions, we generally follow a two-step process: conversion to postfix notation (also known as Reverse Polish Notation) and then evaluation of the postfix expression.
Conversion to Postfix: This can be achieved using the Shunting Yard algorithm developed by Edsger Dijkstra. The algorithm utilizes a stack to hold operators and ensures that the output maintains the correct order of operations (precedence and associativity).
def infix_to_postfix(expression): precedence = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3} output = [] stack = [] for char in expression: if char.isalnum(): # If the character is an operand output.append(char) elif char in precedence: # If the character is an operator while (stack and stack[-1] != '(' and precedence[char] <= precedence[stack[-1]]): output.append(stack.pop()) stack.append(char) elif char == '(': stack.append(char) elif char == ')': while stack and stack[-1] != '(': output.append(stack.pop()) stack.pop() # Remove '(' from stack while stack: output.append(stack.pop()) return ''.join(output)def evaluate_postfix(expression): stack = [] for char in expression: if char.isdigit(): # If the character is an operand stack.append(int(char)) else: # The character is an operator b = stack.pop() a = stack.pop() if char == '+': stack.append(a + b) elif char == '-': stack.append(a - b) elif char == '*': stack.append(a * b) elif char == '/': stack.append(a / b) return stack[0]Sandhiya PriyaRank #26 · 43.2K reputationPosted Jan 1, 2026, 2:16 AM EST
What is an Infix Expression?
An infix expression is the standard way we write math: operators are placed between operands.
Example:
A + B * CThe challenge: You can’t just evaluate left to right because multiplication/division have higher precedence than addition/subtraction.
How to Evaluate Infix Expressions
There are two main approaches:
1. Convert Infix ? Postfix (or Prefix) and Evaluate
Use the Shunting Yard Algorithm (by Dijkstra) to convert infix to postfix.
Then evaluate the postfix expression using a stack.
Steps:
Scan the infix expression left to right.
Use a stack to store operators and parentheses.
Apply precedence and associativity rules to reorder operators.
Generate a postfix expression.
Evaluate postfix using another stack (push operands, pop two when operator appears, apply operator, push result).
Example:
2. Direct Evaluation Using Two Stacks
Maintain two stacks:
Operand stack (numbers)
Operator stack (+, -, *, /, etc.)
Algorithm:
Scan the infix expression left to right.
Push operands onto the operand stack.
Push operators onto the operator stack, but handle precedence:
If the new operator has lower/equal precedence than the top of the stack, pop and evaluate first.
Handle parentheses by pushing
(and popping until you find it.At the end, apply remaining operators.
Example:
Comparison of Approaches
In Practice
Postfix conversion + evaluation is the most common in DSA courses and competitive programming because it’s easier to implement and debug.
Direct two-stack evaluation is closer to how compilers/interpreters work internally.