Infix to postfix conversion of regular expression | Set 2

 



import java.util.Stack;

class InfixToPostfix {
    static String toPostfix(String infix) {
        // stack to store operators 
        Stack stack = new Stack<>();
        StringBuilder result = new StringBuilder(); // store final result
        int i = 0;
        while(i < infix.length()) {
            char ch = infix.charAt(i);
            if(isOperand(ch)) {
                // if it's operand then add into result 
                result.append(ch);
                i++;
            } else {
                // deal with operator 
                if(!stack.empty() && getPrecedence(/*ch=*/ch, /*isOperatorFromStack=*/false) 
                == getPrecedence(/*ch=*/stack.peek(), /*isOperatorFromStack=*/true)) {
                    // we got ')' outside stack and '(' inside stack
                    i++;
                    continue;
                }else if(stack.empty() || getPrecedence(/*ch=*/ch, /*isOperatorFromStack=*/false) 
                > getPrecedence(/*ch=*/stack.peek(), /*isOperatorFromStack=*/true)) {
                    // precendence of input operator is greater than operator 
                    // at stack top then add input operator to stack 
                    stack.push(ch);
                    i++;
                } else {
                    // precendence of input operator is <= precendence of stack top 
                    // operator so pop stack and add into result
                    result.append(stack.pop());
                }
            }
        }
        // pop out whatever is left in stack and insert into result 
        while(!stack.empty()) {
            char ch = stack.pop();
            if(getPrecedence(/*ch=*/ch, /*isOperatorFromStack=*/true) == 0)  {
                continue;
            }
            result.append(ch);
        }
        return result.toString();
    }
    private static boolean isOperand(char ch) {
        switch(ch){
            case '+':
            case '-':
            case '*':
            case '/':
            case '^':
            case '(':
            case ')':
                return false;
        }
        return true;
    }
    private static int getPrecedence(char ch, boolean isOperatorFromStack) {
        if(!isOperatorFromStack) {
            // operator outside stack 
            switch(ch){
                case '+':
                case '-':
                    return 1;
                case '*':
                case '/':
                    return 3;
                case '^':
                    return 6;
                case '(':
                    return 7;
                case ')':
                    return 0;
            }
        } else {
            // operator inside stack 
            switch(ch){
                case '+':
                case '-':
                    return 2;
                case '*':
                case '/':
                    return 4;
                case '^':
                    return 5;
                case '(':
                    return 0;
            }
        }
        return 0;
    }
	public static void main (String[] args) {
		String infix = "((a+b)*c)-d^e^f";
		System.out.println("Infix : " + infix + ", Postfix : " + toPostfix(infix));
	}
}
          

          Output
          Infix : ((a+b)*c)-d^e^f, Postfix : ab+c*def^^-
          

Comments

Popular posts from this blog

SQL basic interview question

gsutil Vs Storage Transfer Service Vs Transfer Appliance