|
| 1 | +package com.thealgorithms.stacks; |
| 2 | + |
| 3 | +import java.util.Stack; |
| 4 | +import java.util.regex.Matcher; |
| 5 | +import java.util.regex.Pattern; |
| 6 | + |
| 7 | +public final class InfixToPrefix { |
| 8 | + private InfixToPrefix() { |
| 9 | + } |
| 10 | + |
| 11 | + /** |
| 12 | + * Convert an infix expression to a prefix expression using stack. |
| 13 | + * |
| 14 | + * @param infixExpression the infix expression to convert |
| 15 | + * @return the prefix expression |
| 16 | + * @throws IllegalArgumentException if the infix expression has unbalanced brackets |
| 17 | + * @throws NullPointerException if the infix expression is null |
| 18 | + */ |
| 19 | + public static String infix2Prefix(String infixExpression) throws IllegalArgumentException { |
| 20 | + if (infixExpression == null) { |
| 21 | + throw new NullPointerException("Input expression cannot be null."); |
| 22 | + } |
| 23 | + infixExpression = infixExpression.trim(); |
| 24 | + if (infixExpression.isEmpty()) { |
| 25 | + return ""; |
| 26 | + } |
| 27 | + if (!BalancedBrackets.isBalanced(filterBrackets(infixExpression))) { |
| 28 | + throw new IllegalArgumentException("Invalid expression: unbalanced brackets."); |
| 29 | + } |
| 30 | + |
| 31 | + StringBuilder output = new StringBuilder(); |
| 32 | + Stack<Character> stack = new Stack<>(); |
| 33 | + // Reverse the infix expression for prefix conversion |
| 34 | + String reversedInfix = new StringBuilder(infixExpression).reverse().toString(); |
| 35 | + for (char element : reversedInfix.toCharArray()) { |
| 36 | + if (Character.isLetterOrDigit(element)) { |
| 37 | + output.append(element); |
| 38 | + } else if (element == ')') { |
| 39 | + stack.push(element); |
| 40 | + } else if (element == '(') { |
| 41 | + while (!stack.isEmpty() && stack.peek() != ')') { |
| 42 | + output.append(stack.pop()); |
| 43 | + } |
| 44 | + stack.pop(); |
| 45 | + } else { |
| 46 | + while (!stack.isEmpty() && precedence(element) < precedence(stack.peek())) { |
| 47 | + output.append(stack.pop()); |
| 48 | + } |
| 49 | + stack.push(element); |
| 50 | + } |
| 51 | + } |
| 52 | + while (!stack.isEmpty()) { |
| 53 | + output.append(stack.pop()); |
| 54 | + } |
| 55 | + |
| 56 | + // Reverse the result to get the prefix expression |
| 57 | + return output.reverse().toString(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Determines the precedence of an operator. |
| 62 | + * |
| 63 | + * @param operator the operator whose precedence is to be determined |
| 64 | + * @return the precedence of the operator |
| 65 | + */ |
| 66 | + private static int precedence(char operator) { |
| 67 | + switch (operator) { |
| 68 | + case '+': |
| 69 | + case '-': |
| 70 | + return 0; |
| 71 | + case '*': |
| 72 | + case '/': |
| 73 | + return 1; |
| 74 | + case '^': |
| 75 | + return 2; |
| 76 | + default: |
| 77 | + return -1; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Filters out all characters from the input string except brackets. |
| 83 | + * |
| 84 | + * @param input the input string to filter |
| 85 | + * @return a string containing only brackets from the input string |
| 86 | + */ |
| 87 | + private static String filterBrackets(String input) { |
| 88 | + Pattern pattern = Pattern.compile("[^(){}\\[\\]<>]"); |
| 89 | + Matcher matcher = pattern.matcher(input); |
| 90 | + return matcher.replaceAll(""); |
| 91 | + } |
| 92 | +} |
0 commit comments