-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #614 from tiger3768/stjava
Add : Balanced Brackets and InfixToPrefix program in Java
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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'; | ||
} | ||
} |
This file contains 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,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; | ||
} | ||
} |