forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInfixToPrefix.java
83 lines (77 loc) · 2.64 KB
/
InfixToPrefix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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.
*
* @param infixExpression the infix expression to convert
* @return the prefix expression
* @throws Exception if the infix expression has unbalanced brackets
*/
public static String infix2Prefix(String infixExpression) throws Exception {
if (!BalancedBrackets.isBalanced(filterBrackets(infixExpression))) {
throw new Exception("invalid expression");
}
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("");
}
}