Postfix Evaluation

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

Overview

Postfix notation (also known as Reverse Polish Notation) is a way to represent an expression, where operators follow their corresponding operands. Evaluating an expression represented as postfix notation can easily be done using the stack data structure.

Takeaways

  • Stack data structure is used to efficiently evaluate Postfix Expression.
  • Time and Space complexity for evaluating postfix expression using stack are
    • Time complexity - O(N)O(N)
    • Space complexity - O(N)O(N)

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Introduction

Postfix notation is one of the few ways to represent algebraic expressions. It is used in computers because it is faster than other types of notations (such as infix notation) as parentheses are not required to represent them.

As the name suggests, in the postfix expression operators follow their operands. Therefore, the process of postfix evaluation is quite different than solving the infix notation (normal notation used in daily use).

Input-Output

Input - A string consisting of numeric values along with operator symbols (*, /, +, -, etc) is given as the input. Output - The result obtained by solving the given input is required to be printed.

Examples of Postfix Expression Evaluation

Example 1 -

Input - 2345+2 3 * 4 5 + *

Output - 5454

Example 2 -

Input - 5235 2 3 * *

Output - 3030

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Algorithm of Postfix Expression Evaluation

As we have discussed earlier, the operators follow their operands in the postfix expression. Hence, for the postfix evaluation we will use the Stack data structure.

We know that the stack data structure works on the last in first out (LIFO) principle. Therefore, previously encountered operands can easily be accessed using the stack.

The algorithm for evaluation of postfix expression is as follows -

  • Create a stack that holds integer type data to store the operands of the given postfix expression. Let it be st.
  • Iterate over the string from left to right and do the following -
    • If the current element is an operand, push it into the stack.
    • Otherwise, if the current element is an operator (say /)do the following -
      • Pop an element from st, let it be op1.
      • Pop another element from st, let it be op2.
      • Computer the result of op2 / op1, and push it into the stack. Note the order i.e.i.e. op2 / op1 should not be changed otherwise it will affect the final result in some cases.
  • At last, st will consist of a single element i.e.i.e. the result after evaluating the postfix expression.

How to Evaluate a Postfix Expression Using Stack?

We have discussed the algorithm that uses the stack for postfix evaluation. Now we dry run the above algorithm to make the concept crystal clear.

Let the the postfix expression be "10 22 + 8 / 6 * 5 +". Now, proceeding as per the algorithm -

  1. Create a stack (say st).

  2. Now since the first element is an operand, so we will push it in the stack i.e.i.e. st.push(10). After which stack looks like -

    st
    10
  3. Now we traverse further in the postfix expression and found that the next element is again an operand. So, we will push it into the stack, after which the stack will look like -

    st
    22
    10
  4. The next element of the expression is an operator (+ operator) so we will do the following -

    1. Pop an element (say op1) from the stack. Here op1 = 22.
    2. Pop another element (say op2) from the stack. Here op2 = 10.
    3. Now, we will compute op2 + op1 which is 32, and push it into the stack. After this step stack will look like -
    st
    32
  5. On traversing further in the expression string, we found next element is an operand. So we will push it into the stack. Upon doing this, the stack will look like -

    st
    8
    32
  6. The next element is the / operator, so we will do the following -

    1. Pop an element (say op1) from the stack. Here op1 = 8.
    2. Pop another element (say op2) from the stack. Here op2 = 32.
    3. Now, we will compute op2 / op1 which is 4, and push it into the stack. After this step stack will look like -
    st
    4
  7. The next element is an operand, hence pushing it in the stack. After this step stack will look like -

    st
    6
    4
  8. The next element is the * operator, hence we need to perform the following steps -

    1. Pop an element (say op1) from the stack. Here op1 = 6.
    2. Pop another element (say op2) from the stack. Here op2 = 4.
    3. Now, we will compute op2 * op1 which is 24, and push it into the stack. After this step stack will look like -
    st
    24
  9. The next operator is an operand, hence pushing it into the stack. Upon doing which stack will look like -

    st
    5
    24
  10. The last element is the + operator. Hence it is required to do the following steps -

    1. Pop an element (say op1) from the stack. Here op1 = 5.
    2. Pop another element (say op2) from the stack. Here op2 = 24.
    3. Now, we will compute op2 + op1 which is 29, and push it into the stack. After this step stack will look like -
    st
    29
  11. Now, we have traversed the given postfix expression, and hence as per the algorithm the only element remaining in the stack i.e. 29 is the result we get on evaluating the postfix expression.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Implementation of the Algorithm

Java Implementation

Output -

C++ Implementation

Output -

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Python Implementation

Output -

Complexity Analysis

Time Complexity - Since, we are traversing the expression once, which costs O(n)O(n) time. Therefore the overall time complexity of postfix evaluation is O(n)O(n).

Space Complexity - The maximum number of elements in our stack can reach up to O(n)O(n) in the worst case. The space complexity is O(n)O(n). :::

A Limitation of the Previous Approach

While implementing the previous approach, we are not taking care of the following two cases -

  1. If the given postfix expression itself is invalid.
  2. If the operands in expression are of two or more digits.

To handle these cases, we need to make some minor changes in the code which are as follows -

  1. If while popping out any element we found that the stack is already empty. It indicates that the given postfix expression is invalid.
  2. If the expression contains operands with two or more digits, all of them must be separated by blank space. Therefore, for each element, we need to calculate its value.

Java Implementation

Output -

C++ Implementation

Output -

Python Implementation

Output -

Conclusion

  • In postfix notation, the operator appears after the operands.
  • It does not require the use of parentheses and therefore it is faster when compared to other notations.
  • Stack data structure is used for evaluation of postfix expression efficiently.