Skip to content

Commit

Permalink
Merge pull request #614 from tiger3768/stjava
Browse files Browse the repository at this point in the history
Add : Balanced Brackets and InfixToPrefix program in Java
  • Loading branch information
gantavyamalviya authored Oct 5, 2022
2 parents 2afd88f + b9880e6 commit 59104e4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Java/Stack/BalancedBrackets.java
Original file line number Diff line number Diff line change
@@ -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<Character> 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';
}
}
42 changes: 42 additions & 0 deletions Java/Stack/InfixToPrefix.java
Original file line number Diff line number Diff line change
@@ -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<Character> 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;
}
}

0 comments on commit 59104e4

Please sign in to comment.