Add Infix To Prefix new algorithm with unit tests (#5537)

* Add `Infix To Prefix` new algorithm

* Update directory

* Update directory

* Fix clang

* Fix clang

* Add more tests

* Fix comma error

* Fix test cases

* Fix comment

* Remove unused import

* Update directory

* Add tests for null & empty strings

* Implement suggested changes

* Update directory

* Fix comment

---------

Co-authored-by: Hardvan <Hardvan@users.noreply.github.com>
Co-authored-by: Alex Klymenko <alexanderklmn@gmail.com>
This commit is contained in:
Hardik Pawar
2024-10-04 22:10:44 +05:30
committed by GitHub
parent 5cbdb475ee
commit b61c54797b
3 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,92 @@
package com.thealgorithms.stacks;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class InfixToPrefix {
private InfixToPrefix() {
}
/**
* Convert an infix expression to a prefix expression using stack.
*
* @param infixExpression the infix expression to convert
* @return the prefix expression
* @throws IllegalArgumentException if the infix expression has unbalanced brackets
* @throws NullPointerException if the infix expression is null
*/
public static String infix2Prefix(String infixExpression) throws IllegalArgumentException {
if (infixExpression == null) {
throw new NullPointerException("Input expression cannot be null.");
}
infixExpression = infixExpression.trim();
if (infixExpression.isEmpty()) {
return "";
}
if (!BalancedBrackets.isBalanced(filterBrackets(infixExpression))) {
throw new IllegalArgumentException("Invalid expression: unbalanced brackets.");
}
StringBuilder output = new StringBuilder();
Stack<Character> stack = new Stack<>();
// Reverse the infix expression for prefix conversion
String reversedInfix = new StringBuilder(infixExpression).reverse().toString();
for (char element : reversedInfix.toCharArray()) {
if (Character.isLetterOrDigit(element)) {
output.append(element);
} else if (element == ')') {
stack.push(element);
} else if (element == '(') {
while (!stack.isEmpty() && stack.peek() != ')') {
output.append(stack.pop());
}
stack.pop();
} else {
while (!stack.isEmpty() && precedence(element) < precedence(stack.peek())) {
output.append(stack.pop());
}
stack.push(element);
}
}
while (!stack.isEmpty()) {
output.append(stack.pop());
}
// Reverse the result to get the prefix expression
return output.reverse().toString();
}
/**
* Determines the precedence of an operator.
*
* @param operator the operator whose precedence is to be determined
* @return the precedence of the operator
*/
private static int precedence(char operator) {
switch (operator) {
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
case '^':
return 2;
default:
return -1;
}
}
/**
* Filters out all characters from the input string except brackets.
*
* @param input the input string to filter
* @return a string containing only brackets from the input string
*/
private static String filterBrackets(String input) {
Pattern pattern = Pattern.compile("[^(){}\\[\\]<>]");
Matcher matcher = pattern.matcher(input);
return matcher.replaceAll("");
}
}

View File

@ -0,0 +1,44 @@
package com.thealgorithms.stacks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class InfixToPrefixTest {
@ParameterizedTest
@MethodSource("provideValidExpressions")
void testValidExpressions(String infix, String expectedPrefix) throws Exception {
assertEquals(expectedPrefix, InfixToPrefix.infix2Prefix(infix));
}
@Test
void testEmptyString() {
// Assuming that an empty string returns an empty prefix or throws an exception
assertEquals("", InfixToPrefix.infix2Prefix(""));
}
@Test
void testNullValue() {
// Assuming that a null input throws a NullPointerException
assertThrows(NullPointerException.class, () -> InfixToPrefix.infix2Prefix(null));
}
private static Stream<Arguments> provideValidExpressions() {
return Stream.of(Arguments.of("3+2", "+32"), // Simple addition
Arguments.of("1+(2+3)", "+1+23"), // Parentheses
Arguments.of("(3+4)*5-6", "-*+3456"), // Nested operations
Arguments.of("a+b*c", "+a*bc"), // Multiplication precedence
Arguments.of("a+b*c/d", "+a/*bcd"), // Division precedence
Arguments.of("a+b*c-d", "-+a*bcd"), // Subtraction precedence
Arguments.of("a+b*c/d-e", "-+a/*bcde"), // Mixed precedence
Arguments.of("a+b*(c-d)", "+a*b-cd"), // Parentheses precedence
Arguments.of("a+b*(c-d)/e", "+a/*b-cde") // Mixed precedence with parentheses
);
}
}