-
Notifications
You must be signed in to change notification settings - Fork 20k
Add PrefixToInfix.java
new algorithm
#5552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
20892f7
Add `PrefixToInfix.java` new algorithm
Hardvan 840081a
Update directory
Hardvan ff1ad59
Fix clang error
Hardvan 19268f0
Merge branch 'prefix_to_infix_new_algo' of https://github.com/Hardvan…
Hardvan 70a0e89
Merge branch 'master' into prefix_to_infix_new_algo
Hardvan f01e868
Update directory
Hardvan ba07fdb
Fix comment
Hardvan 5f14ff3
Merge branch 'prefix_to_infix_new_algo' of https://github.com/Hardvan…
Hardvan 4de9a22
Add suggested changes
Hardvan 41f49b1
Merge branch 'master' into prefix_to_infix_new_algo
alxkm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.thealgorithms.stacks; | ||
|
||
import java.util.Stack; | ||
|
||
/** | ||
* Converts a prefix expression to an infix expression using a stack. | ||
* | ||
* The input prefix expression should consist of | ||
* valid operands (letters or digits) and operators (+, -, *, /, ^). | ||
* Parentheses are not required in the prefix string. | ||
*/ | ||
public final class PrefixToInfix { | ||
private PrefixToInfix() { | ||
} | ||
|
||
/** | ||
* Determines if a given character is a valid arithmetic operator. | ||
* | ||
* @param token the character to check | ||
* @return true if the character is an operator, false otherwise | ||
*/ | ||
public static boolean isOperator(char token) { | ||
return token == '+' || token == '-' || token == '/' || token == '*' || token == '^'; | ||
} | ||
|
||
/** | ||
* Converts a valid prefix expression to an infix expression. | ||
* | ||
* @param prefix the prefix expression to convert | ||
* @return the equivalent infix expression | ||
* @throws NullPointerException if the prefix expression is null | ||
*/ | ||
public static String getPrefixToInfix(String prefix) { | ||
if (prefix == null) { | ||
throw new NullPointerException("Null prefix expression"); | ||
} | ||
if (prefix.isEmpty()) { | ||
return ""; | ||
} | ||
|
||
Stack<String> stack = new Stack<>(); | ||
|
||
// Iterate over the prefix expression from right to left | ||
for (int i = prefix.length() - 1; i >= 0; i--) { | ||
char token = prefix.charAt(i); | ||
|
||
if (isOperator(token)) { | ||
// Pop two operands from stack | ||
String operandA = stack.pop(); | ||
String operandB = stack.pop(); | ||
|
||
// Form the infix expression with parentheses | ||
String infix = "(" + operandA + token + operandB + ")"; | ||
|
||
// Push the resulting infix expression back onto the stack | ||
stack.push(infix); | ||
} else { | ||
// Push operand onto stack | ||
stack.push(Character.toString(token)); | ||
} | ||
} | ||
|
||
if (stack.size() != 1) { | ||
throw new ArithmeticException("Malformed prefix expression"); | ||
} | ||
|
||
return stack.pop(); // final element on the stack is the full infix expression | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/com/thealgorithms/stacks/PrefixToInfixTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
class PrefixToInfixTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideValidPrefixToInfixTestCases") | ||
void testValidPrefixToInfixConversion(String prefix, String expectedInfix) { | ||
assertEquals(expectedInfix, PrefixToInfix.getPrefixToInfix(prefix)); | ||
} | ||
|
||
static Stream<Arguments> provideValidPrefixToInfixTestCases() { | ||
return Stream.of(Arguments.of("A", "A"), // Single operand | ||
Arguments.of("+AB", "(A+B)"), // Addition | ||
Arguments.of("*+ABC", "((A+B)*C)"), // Addition and multiplication | ||
Arguments.of("-+A*BCD", "((A+(B*C))-D)"), // Mixed operators | ||
Arguments.of("/-A*BC+DE", "((A-(B*C))/(D+E))"), // Mixed operators | ||
Arguments.of("^+AB*CD", "((A+B)^(C*D))") // Mixed operators | ||
); | ||
} | ||
|
||
@Test | ||
void testEmptyPrefixExpression() { | ||
assertEquals("", PrefixToInfix.getPrefixToInfix("")); | ||
} | ||
|
||
@Test | ||
void testNullPrefixExpression() { | ||
assertThrows(NullPointerException.class, () -> PrefixToInfix.getPrefixToInfix(null)); | ||
} | ||
|
||
@Test | ||
void testMalformedPrefixExpression() { | ||
assertThrows(ArithmeticException.class, () -> PrefixToInfix.getPrefixToInfix("+ABC")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.