Infix to Postfix Conversion Using Stack in DSA C - Time & Space Complexity
We analyze how the time needed to convert an infix expression to postfix grows as the expression gets longer.
We want to know how the number of steps changes when the input expression size increases.
Analyze the time complexity of the following code snippet.
void infixToPostfix(char* infix, char* postfix) {
Stack s;
initStack(&s);
int i = 0, k = 0;
while (infix[i] != '\0') {
if (isOperand(infix[i])) {
postfix[k++] = infix[i];
} else if (infix[i] == '(') {
push(&s, infix[i]);
} else if (infix[i] == ')') {
while (!isEmpty(&s) && peek(&s) != '(') {
postfix[k++] = pop(&s);
}
pop(&s); // remove '('
} else {
while (!isEmpty(&s) && precedence(peek(&s)) > precedence(infix[i])) {
postfix[k++] = pop(&s);
}
push(&s, infix[i]);
}
i++;
}
while (!isEmpty(&s)) {
postfix[k++] = pop(&s);
}
postfix[k] = '\0';
}
This code converts an infix expression to postfix using a stack to handle operators and parentheses.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single while loop scanning each character of the input expression once.
- How many times: Once per character in the input (n times), plus popping remaining operators from the stack at the end.
As the input expression length grows, the code processes each character once, with some extra steps for operators.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 main steps plus some stack operations |
| 100 | About 100 main steps plus stack operations proportional to operators |
| 1000 | About 1000 main steps plus stack operations proportional to operators |
Pattern observation: The total steps grow roughly in direct proportion to the input size.
Time Complexity: O(n)
This means the time to convert grows linearly with the length of the input expression.
[X] Wrong: "The nested while loops make this algorithm quadratic time O(n²)."
[OK] Correct: Each character is pushed and popped at most once, so total operations stay proportional to n, not n squared.
Understanding this linear time conversion shows you can handle stack-based parsing efficiently, a useful skill in many coding problems.
"What if the input expression included multi-digit numbers or variables? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of stack in conversion
The stack is used to hold operators temporarily while processing the infix expression to respect operator precedence and associativity.Step 2: Differentiate stack usage from other data structures
Operands are directly added to the postfix output, while operators are pushed to the stack to ensure correct order in the postfix expression.Final Answer:
To temporarily hold operators and manage precedence -> Option AQuick Check:
Stack holds operators = A [OK]
- Thinking stack stores operands
- Assuming stack reverses operands
- Believing stack counts operators
Solution
Step 1: Understand stack implementation in C
In array-based stack, 'top' is the index of the last pushed element. When empty, top is -1.Step 2: Identify correct empty condition
Checking if top == -1 correctly indicates the stack is empty.Final Answer:
if (top == -1) -> Option BQuick Check:
top == -1 means empty stack [OK]
- Checking stack pointer NULL instead of top
- Using top == 0 which means one element present
- Using struct size field incorrectly
A + B * C?Solution
Step 1: Apply operator precedence rules
Multiplication (*) has higher precedence than addition (+), so B*C is evaluated first.Step 2: Convert to postfix
Postfix places operands first, then operator: B C * then A + gives ABC*+Final Answer:
ABC*+ -> Option AQuick Check:
Infix A + (B * C) = ABC*+ [OK]
- Ignoring operator precedence
- Placing operators before operands
- Mixing operand order
if (isOperator(ch)) {
while (!isEmpty(stack) && precedence(peek(stack)) >= precedence(ch)) {
postfix[j++] = pop(stack);
}
push(stack, ch);
}Solution
Step 1: Understand operator precedence handling
For correct associativity, when precedence is equal and operator is right-associative (like '^'), we should not pop.Step 2: Identify correct comparison operator
Using >= causes popping on equal precedence, which breaks right-associativity. Using > fixes this.Final Answer:
The condition should use > instead of >= for precedence comparison -> Option CQuick Check:
Use > to handle associativity correctly [OK]
- Using >= causes wrong operator order
- Pushing outside operator check
- Misusing isEmpty function
(A + B) * (C - D) / E to postfix notation using stack. What is the correct postfix expression?Solution
Step 1: Convert each parenthesis group to postfix
(A + B) becomes AB+ and (C - D) becomes CD-.Step 2: Combine with multiplication and division
Expression becomes (AB+) * (CD-) / E. Multiplication and division are left associative, so postfix is AB+ CD- * E /.Final Answer:
AB+CD-*E/ -> Option DQuick Check:
Postfix of (A+B)*(C-D)/E = AB+CD-*E/ [OK]
- Ignoring parentheses grouping
- Mixing operator order
- Misplacing division operator
Ready to prove it?
Pass the test and this pattern moves unknown
