From b9880e644d42ec8c6294091f32d7c06a2174c44f Mon Sep 17 00:00:00 2001 From: Kartik S Date: Wed, 5 Oct 2022 17:55:25 +0530 Subject: [PATCH] Add : Balanced Brackets and InfixToPostfix program in Java --- Java/Stack/BalancedBrackets.java | 32 ++++++++++++++++++++++++ Java/Stack/InfixToPrefix.java | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 Java/Stack/BalancedBrackets.java create mode 100644 Java/Stack/InfixToPrefix.java diff --git a/Java/Stack/BalancedBrackets.java b/Java/Stack/BalancedBrackets.java new file mode 100644 index 00000000..5b6157f0 --- /dev/null +++ b/Java/Stack/BalancedBrackets.java @@ -0,0 +1,32 @@ +//Program to check for Balanced Brackets in Java + +import java.util.Stack; + +public class BalancedBrackets { + public static boolean isBalanced(String expression) { + Stack bracketStack = new Stack<>(); + int n = expression.length(); + for(int i = 0 ; i < n ; i++){ + char bracket = expression.charAt(i); + if(isNotB(bracket)) continue; + if(isOpeningB(bracket)) bracketStack.push(bracket); + else{ + if(!bracketStack.isEmpty() && validOpeningB(bracket) != bracketStack.peek()) return false; + else bracketStack.pop(); + } + } + return bracketStack.isEmpty(); + } + static boolean isNotB(char bracket){ + return !("(){}[]".contains(bracket + "")); + } + static boolean isOpeningB(char bracket){ + return "({[".contains(bracket + ""); + } + static char validOpeningB(char bracket){ + if(bracket == ')') return '('; + if(bracket == '}') return '{'; + if(bracket == ']') return '['; + return '\0'; + } +} \ No newline at end of file diff --git a/Java/Stack/InfixToPrefix.java b/Java/Stack/InfixToPrefix.java new file mode 100644 index 00000000..8f45f8a4 --- /dev/null +++ b/Java/Stack/InfixToPrefix.java @@ -0,0 +1,42 @@ +//Java Program to convert Infix Expression to Prefix +import java.util.Stack; + +public class InfixToPrefix { + public static void main(String[] args) { + System.out.println("1+2+3 -> " + infix2Prefix("1+2+3")); + System.out.println("A^B-(C+D) -> " + infix2Prefix("A^B-(C+D)")); + System.out.println("(P/L+(F*J) -> " + infix2Prefix("(P/L+(F*J)")); + } + public static String infix2Prefix(String expression){ + if(!BalancedBrackets.isBalanced(expression)) return "Invalid Expression"; + Stack operatorStack = new Stack<>(); + StringBuilder prefixExpression = new StringBuilder(); + int n = expression.length(); + for(int i = n - 1 ; i >= 0 ; i--){ + char scanned = expression.charAt(i); + if(Character.isLetterOrDigit(scanned)) prefixExpression.append(scanned); + else if(scanned == ')') operatorStack.push(scanned); + else if(scanned == '('){ + while(!operatorStack.isEmpty() && operatorStack.peek() != ')'){ + prefixExpression.append(operatorStack.pop()); + } + if(!operatorStack.isEmpty()) operatorStack.pop(); + } + else if(operatorStack.isEmpty() || operatorStack.peek() == ')') operatorStack.push(scanned); + else{ + while(!operatorStack.isEmpty() && precedence(operatorStack.peek()) < precedence(scanned)){ + prefixExpression.append(operatorStack.pop()); + } + operatorStack.push(scanned); + } + } + while(!operatorStack.isEmpty()) prefixExpression.append(operatorStack.pop()); + return prefixExpression.reverse().toString(); + } + static int precedence(char operator){ + if(operator == '^') return 3; + if(operator == '*' || operator == '/') return 2; + if(operator == '+' || operator == '-') return 1; + return 0; + } +}