Skip to content

Commit 0d0688a

Browse files
Merge branch 'master' into feature/bitwiseAnd-in-range
2 parents 811d754 + bae7f89 commit 0d0688a

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

DIRECTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,9 @@
599599
* [MaximumMinimumWindow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/MaximumMinimumWindow.java)
600600
* [NextGreaterElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextGreaterElement.java)
601601
* [NextSmallerElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java)
602+
* [PostfixEvaluator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixEvaluator.java)
602603
* [PostfixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixToInfix.java)
604+
* [PrefixEvaluator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixEvaluator.java)
603605
* [PrefixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixToInfix.java)
604606
* [SortStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/SortStack.java)
605607
* [StackPostfixNotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java)
@@ -1130,7 +1132,9 @@
11301132
* [LargestRectangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/LargestRectangleTest.java)
11311133
* [NextGreaterElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextGreaterElementTest.java)
11321134
* [NextSmallerElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextSmallerElementTest.java)
1135+
* [PostfixEvaluatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixEvaluatorTest.java)
11331136
* [PostfixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixToInfixTest.java)
1137+
* [PrefixEvaluatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixEvaluatorTest.java)
11341138
* [PrefixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixToInfixTest.java)
11351139
* [SortStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/SortStackTest.java)
11361140
* [StackPostfixNotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/StackPostfixNotationTest.java)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Set;
4+
import java.util.Stack;
5+
6+
/**
7+
* Evaluate a postfix (Reverse Polish) expression using a stack.
8+
*
9+
* <p>Example: Expression "5 6 + 2 *" results in 22.
10+
* <p>Applications: Used in calculators and expression evaluation in compilers.
11+
*
12+
* @author Hardvan
13+
*/
14+
public final class PostfixEvaluator {
15+
private PostfixEvaluator() {
16+
}
17+
18+
private static final Set<String> OPERATORS = Set.of("+", "-", "*", "/");
19+
20+
/**
21+
* Evaluates the given postfix expression and returns the result.
22+
*
23+
* @param expression The postfix expression as a string with operands and operators separated by spaces.
24+
* @return The result of evaluating the postfix expression.
25+
* @throws IllegalArgumentException if the expression is invalid.
26+
*/
27+
public static int evaluatePostfix(String expression) {
28+
Stack<Integer> stack = new Stack<>();
29+
30+
for (String token : expression.split("\\s+")) {
31+
if (isOperator(token)) {
32+
int operand2 = stack.pop();
33+
int operand1 = stack.pop();
34+
stack.push(applyOperator(token, operand1, operand2));
35+
} else {
36+
stack.push(Integer.valueOf(token));
37+
}
38+
}
39+
40+
if (stack.size() != 1) {
41+
throw new IllegalArgumentException("Invalid expression");
42+
}
43+
44+
return stack.pop();
45+
}
46+
47+
/**
48+
* Checks if the given token is an operator.
49+
*
50+
* @param token The token to check.
51+
* @return true if the token is an operator, false otherwise.
52+
*/
53+
private static boolean isOperator(String token) {
54+
return OPERATORS.contains(token);
55+
}
56+
57+
/**
58+
* Applies the given operator to the two operands.
59+
*
60+
* @param operator The operator to apply.
61+
* @param a The first operand.
62+
* @param b The second operand.
63+
* @return The result of applying the operator to the operands.
64+
*/
65+
private static int applyOperator(String operator, int a, int b) {
66+
return switch (operator) {
67+
case "+" -> a + b;
68+
case "-" -> a - b;
69+
case "*" -> a * b;
70+
case "/" -> a / b;
71+
default -> throw new IllegalArgumentException("Invalid operator");
72+
};
73+
}
74+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Set;
4+
import java.util.Stack;
5+
6+
/**
7+
* Evaluate a prefix (Polish) expression using a stack.
8+
*
9+
* <p>Example: Expression "+ * 2 3 4" results in 10.
10+
* <p>Applications: Useful for implementing compilers and interpreters.
11+
*
12+
* @author Hardvan
13+
*/
14+
public final class PrefixEvaluator {
15+
private PrefixEvaluator() {
16+
}
17+
18+
private static final Set<String> OPERATORS = Set.of("+", "-", "*", "/");
19+
20+
/**
21+
* Evaluates the given prefix expression and returns the result.
22+
*
23+
* @param expression The prefix expression as a string with operands and operators separated by spaces.
24+
* @return The result of evaluating the prefix expression.
25+
* @throws IllegalArgumentException if the expression is invalid.
26+
*/
27+
public static int evaluatePrefix(String expression) {
28+
Stack<Integer> stack = new Stack<>();
29+
String[] tokens = expression.split("\\s+");
30+
31+
for (int i = tokens.length - 1; i >= 0; i--) {
32+
String token = tokens[i];
33+
if (isOperator(token)) {
34+
int operand1 = stack.pop();
35+
int operand2 = stack.pop();
36+
stack.push(applyOperator(token, operand1, operand2));
37+
} else {
38+
stack.push(Integer.valueOf(token));
39+
}
40+
}
41+
42+
if (stack.size() != 1) {
43+
throw new IllegalArgumentException("Invalid expression");
44+
}
45+
46+
return stack.pop();
47+
}
48+
49+
/**
50+
* Checks if the given token is an operator.
51+
*
52+
* @param token The token to check.
53+
* @return true if the token is an operator, false otherwise.
54+
*/
55+
private static boolean isOperator(String token) {
56+
return OPERATORS.contains(token);
57+
}
58+
59+
/**
60+
* Applies the given operator to the two operands.
61+
*
62+
* @param operator The operator to apply.
63+
* @param a The first operand.
64+
* @param b The second operand.
65+
* @return The result of applying the operator to the operands.
66+
*/
67+
private static int applyOperator(String operator, int a, int b) {
68+
return switch (operator) {
69+
case "+" -> a + b;
70+
case "-" -> a - b;
71+
case "*" -> a * b;
72+
case "/" -> a / b;
73+
default -> throw new IllegalArgumentException("Invalid operator");
74+
};
75+
}
76+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.EmptyStackException;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class PostfixEvaluatorTest {
10+
11+
@Test
12+
public void testValidExpressions() {
13+
assertEquals(22, PostfixEvaluator.evaluatePostfix("5 6 + 2 *"));
14+
assertEquals(27, PostfixEvaluator.evaluatePostfix("7 2 + 3 *"));
15+
assertEquals(3, PostfixEvaluator.evaluatePostfix("10 5 / 1 +"));
16+
}
17+
18+
@Test
19+
public void testInvalidExpression() {
20+
assertThrows(EmptyStackException.class, () -> PostfixEvaluator.evaluatePostfix("5 +"));
21+
}
22+
23+
@Test
24+
public void testMoreThanOneStackSizeAfterEvaluation() {
25+
assertThrows(IllegalArgumentException.class, () -> PostfixEvaluator.evaluatePostfix("5 6 + 2 * 3"));
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.EmptyStackException;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class PrefixEvaluatorTest {
10+
11+
@Test
12+
public void testValidExpressions() {
13+
assertEquals(10, PrefixEvaluator.evaluatePrefix("+ * 2 3 4"));
14+
assertEquals(5, PrefixEvaluator.evaluatePrefix("- + 7 3 5"));
15+
assertEquals(6, PrefixEvaluator.evaluatePrefix("/ * 3 2 1"));
16+
}
17+
18+
@Test
19+
public void testInvalidExpression() {
20+
assertThrows(EmptyStackException.class, () -> PrefixEvaluator.evaluatePrefix("+ 3"));
21+
}
22+
23+
@Test
24+
public void testMoreThanOneStackSizeAfterEvaluation() {
25+
assertThrows(IllegalArgumentException.class, () -> PrefixEvaluator.evaluatePrefix("+ 3 4 5"));
26+
}
27+
}

0 commit comments

Comments
 (0)