TheAlgorithms / TheAlgorithms/Java

[FEATURE REQUEST] <title>Add Postfix Expression Evaluation implementation with JUnit tests

Open
#7,588 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
66.3k
Forks
21.3k
Avg merge
16h 57m
Merged PRs (30d)
23

Description

What would you like to Propose?

Feature Description

I would like to propose adding an implementation of Postfix (Reverse Polish Notation) Expression Evaluation in Java using a Stack.

The repository currently has InfixToPostfix.java and InfixToPrefix.java, which convert expressions into postfix/prefix notation, but there is no implementation that actually evaluates a postfix expression to compute its result. This would be a natural and useful complement to the existing conversion utilities.

Proposed Changes

I would like to add:

PostfixEvaluation.java under src/main/java/com/thealgorithms/stacks/
Complete implementation with clear Javadoc explanations (O(N) Time, O(N) Space).
Support for standard operators (+, -, *, /) and multi-digit operands.
Proper handling of invalid expressions / division by zero.
PostfixEvaluationTest.java under src/test/java/com/thealgorithms/stacks/
Comprehensive JUnit 5 test suite covering standard expressions, negative results, single-operand expressions, and invalid input handling.

Verification

I will ensure all code follows the project's formatting rules and passes ./gradlew test / mvn test locally before opening a PR.

I would love to implement this as my first open-source contribution! Could a maintainer please assign this issue to me?

Issue details

Issue Details & Algorithm Overview

  1. Algorithm Description

Algorithm: Postfix (Reverse Polish Notation) Expression Evaluation
Category: Stacks / Expression Evaluation
Language: Java

  1. How the Algorithm Works

Given a valid postfix expression (e.g., "23+" representing 2 + 3), evaluate it to a single numeric result using a stack.

Key Logic:

Scan the expression token by token.
If the token is an operand, push it onto the stack.
If the token is an operator, pop the top two operands, apply the operator, and push the result back onto the stack.
After processing all tokens, the stack contains exactly one value — the final result.

  1. Complexity Analysis

Time Complexity: O(N) — single pass through the expression.
Space Complexity: O(N) — stack storage in the worst case (all operands).

  1. Planned Files & Folder Structure

src/main/java/com/thealgorithms/stacks/PostfixEvaluation.java (Implementation)
src/test/java/com/thealgorithms/stacks/PostfixEvaluationTest.java (JUnit 5 Test Suite)

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading src/main/java/com/thealgorithms/stacks/InfixToPostfix.java and InfixToPrefix.java to match the package and project conventions. Add the named PostfixEvaluation.java and its JUnit 5 companion, covering standard operators, multi-digit operands, negative results, single operands, invalid expressions, and division by zero. Run ./gradlew test or mvn test; done means the new tests pass and formatting rules are satisfied.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.