Infix to Postfix Expression Evaluater
Evaluate a generic mathematical expression.
The Idea: Convert the expression from Infix (AOB) to Postfix (ABO), where A and B are numbers and O is an operator. The algorithms go as follows:
Infix to Postfix:
Maintain a stack and scan the infix expression from left to right.
When we get a number, output it.
When we get an operator O, pop the top element in the stack until there is no operator having higher priority then O and then push(O) into the stack.
When the expression is ended, pop all the operators remain in the stack
Postfix to Answer:
Maintain a stack and scan the postfix expression from left to right.
When we get a number, place it into the stack.
When we get an operator O, pop two elements from the stack and append new number O(a, b).
In the end, return the top of the stack.
Complexity: O(n) time and space
Last updated