Skip to content

Commit 20892f7

Browse files
committed
Add PrefixToInfix.java new algorithm
1 parent 842ff52 commit 20892f7

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Prefix to Infix implementation via Stack
7+
*
8+
* Function: String getPrefixToInfix(String prefix)
9+
* Returns the Infix Expression for the given prefix parameter.
10+
*
11+
* Avoid using parentheses/brackets/braces for the prefix string.
12+
* Prefix Expressions don't require these.
13+
*
14+
* @author OpenAI
15+
*
16+
*/
17+
18+
public final class PrefixToInfix {
19+
private PrefixToInfix() {
20+
}
21+
22+
/**
23+
* Determines if a given character is a valid arithmetic operator.
24+
*
25+
* @param token the character to check
26+
* @return true if the character is an operator, false otherwise
27+
*/
28+
public static boolean isOperator(char token) {
29+
return token == '+' || token == '-' || token == '/' || token == '*' || token == '^';
30+
}
31+
32+
/**
33+
* Converts a valid prefix expression to an infix expression.
34+
*
35+
* @param prefix the prefix expression to convert
36+
* @return the equivalent infix expression
37+
* @throws NullPointerException if the prefix expression is null
38+
*/
39+
public static String getPrefixToInfix(String prefix) {
40+
if (prefix == null) {
41+
throw new NullPointerException("Null prefix expression");
42+
}
43+
if (prefix.isEmpty()) {
44+
return "";
45+
}
46+
47+
Stack<String> stack = new Stack<>();
48+
49+
// Iterate over the prefix expression from right to left
50+
for (int i = prefix.length() - 1; i >= 0; i--) {
51+
char token = prefix.charAt(i);
52+
53+
if (isOperator(token)) {
54+
// Pop two operands from stack
55+
String operandA = stack.pop();
56+
String operandB = stack.pop();
57+
58+
// Form the infix expression with parentheses
59+
String infix = "(" + operandA + token + operandB + ")";
60+
61+
// Push the resulting infix expression back onto the stack
62+
stack.push(infix);
63+
} else {
64+
// Push operand onto stack
65+
stack.push(Character.toString(token));
66+
}
67+
}
68+
69+
return stack.pop(); // final element on the stack is the full infix expression
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.stream.Stream;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
class PrefixToInfixTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("provideValidPrefixToInfixTestCases")
16+
void testValidPrefixToInfixConversion(String prefix, String expectedInfix) {
17+
assertEquals(expectedInfix, PrefixToInfix.getPrefixToInfix(prefix));
18+
}
19+
20+
static Stream<Arguments> provideValidPrefixToInfixTestCases() {
21+
return Stream.of(
22+
Arguments.of("A", "A"), // Single operand
23+
Arguments.of("+AB", "(A+B)"), // Addition
24+
Arguments.of("*+ABC", "((A+B)*C)"), // Addition and multiplication
25+
Arguments.of("-+A*BCD", "((A+(B*C))-D)"), // Mixed operators
26+
Arguments.of("/-A*BC+DE", "((A-(B*C))/(D+E))"), // Mixed operators
27+
Arguments.of("^+AB*CD", "((A+B)^(C*D))") // Mixed operators
28+
);
29+
}
30+
31+
@Test
32+
void testEmptyPrefixExpression() {
33+
assertEquals("", PrefixToInfix.getPrefixToInfix(""));
34+
}
35+
36+
@Test
37+
void testNullPrefixExpression() {
38+
assertThrows(NullPointerException.class, () -> PrefixToInfix.getPrefixToInfix(null));
39+
}
40+
}

0 commit comments

Comments
 (0)