Postfix:
Similarly, the expression in which the operator appears after the operands is known as postfix expression. For example, ab+, where a and b are operands, + is an operator that appears after the operands a and b.
Example: 456*+
Algorithm of Postfix Evaluation:
Step1: Add a ")" at the end of the postfix expression.
Step2: Scan every character of the postfix expression and repeat.
Step 3 and 4 until ")" is encountered.
Step 3: If an operand is encountered, then
a. Pop the top elements from the stack as A and B as A and B.
b. Evaluate B 0 A, where A is the topmost element below A.
c. Push the result of the evaluation on the stack.
[END OF IF]
Step 4: SET RESULT equal to the topmost element of the stack.
Step 5: EXIT.
TIME COMPLEXITY:
In postfix evaluation, we have to scan n symbols.
So, the Time complexity of postfix evaluation is O(n).
SPACE COMPLEXITY:
We require n stack to evaluate postfix to infix. So, space complexity is O(n).
Related
Contributed by