LintCode - Infix to Prefix / Convert Expression to Polish Notation - 我的博客 - ITeye技术网站
Given an expression string array, return the Polish notation of this expression. (remove the parentheses)
Aka, convert infix notation to prefix notation.
Polish notation, also known as Polish prefix notation or simply prefix notation
Its distinguishing feature is that it places operators to the left of their operands.
https://wxx5433.gitbooks.io/interview-preparation/content/part_ii_leetcode_lintcode/stack/convert_expression_to_polish_notation.html
http://www.jianshu.com/p/e21b83eef010
https://simpledevcode.wordpress.com/2015/01/03/convert-infix-to-reverse-polish-notation-c-implementation/
Read full article from LintCode - Infix to Prefix / Convert Expression to Polish Notation - 我的博客 - ITeye技术网站
Given an expression string array, return the Polish notation of this expression. (remove the parentheses)
Aka, convert infix notation to prefix notation.
Example
For the expression [(5 − 6) * 7] (which represented by
["(", "5", "−", "6", ")", "*", "7"]
), the corresponding polish notation is [* - 5 6 7] (which the return value should be["*", "−", "5", "6", "7"]
).Its distinguishing feature is that it places operators to the left of their operands.
https://wxx5433.gitbooks.io/interview-preparation/content/part_ii_leetcode_lintcode/stack/convert_expression_to_polish_notation.html
Need some modification on Convert Expression to Reverse Polish Notation.
- Need to scan the expression from right to left.
- Each time ")" is met, put into opStack. (Instead of "(")
- Each time "(" is met, pop from opStack until see ")". (The reason is that it evaluation from end to start)
- Only pop op from element when priority(prevOp) > priority(op), instead of >=. The reason is the same, since we evaluate from end to start.
public ArrayList<String> convertToPN(String[] expression) {
ArrayList<String> result = new ArrayList<>();
if (expression == null || expression.length == 0) {
return result;
}
Stack<String> opStack = new Stack<>();
for (int i = expression.length - 1; i >= 0; i--) {
String token = expression[i];
if (isNumber(token)) {
result.add(token);
} else if (token.equals("(")) {
while (!opStack.peek().equals(")")) {
result.add(opStack.pop());
}
opStack.pop();
} else if (token.equals(")")) {
opStack.push(token);
} else {
while (!opStack.isEmpty() && getPriority(opStack.peek()) > getPriority(token)) {
result.add(opStack.pop());
}
opStack.push(token);
}
}
while (!opStack.isEmpty()) {
result.add(opStack.pop());
}
Collections.reverse(result);
return result;
}
private boolean isNumber(String token) {
return Character.isDigit(token.charAt(0));
}
private int getPriority(String op) {
if (op.equals(")")) {
return 0;
} else if (op.equals("+") || op.equals("-")) {
return 1;
} else {
return 2;
}
}
- public List<String> convertToPN(String[] expression) {
- Stack<String> operand = new Stack<>();
- Stack<String> operator = new Stack<>();
- for(int i=expression.length-1; i>=0; i--) {
- String s = expression[i];
- if("+-*/".contains(s)) {
- // note: must be >, not >=
- while(!operator.isEmpty() && getPriority(operator.peek()) > getPriority(s)) {
- operand.push(operator.pop());
- }
- operator.push(s);
- } else if(")".equals(s)) {
- operator.push(s);
- } else if("(".equals(s)) {
- while(!operator.peek().equals(")")) {
- operand.push(operator.pop());
- }
- operator.pop();
- } else {
- operand.push(s);
- }
- }
- while(!operator.isEmpty()) {
- operand.push(operator.pop());
- }
- List<String> pn = new ArrayList<>(operand);
- Collections.reverse(pn);
- return pn;
- }
- private int getPriority(String s) {
- char c = s.charAt(0);
- if(c=='+' || c=='-') return 1;
- if(c=='*' || c=='/') return 2;
- return 0;
- }
http://www.jianshu.com/p/e21b83eef010
- 首先建立表达式树,如题[Expression Tree Build]
- Polish Notation即表达式树前序遍历的结果
https://simpledevcode.wordpress.com/2015/01/03/convert-infix-to-reverse-polish-notation-c-implementation/
Read full article from LintCode - Infix to Prefix / Convert Expression to Polish Notation - 我的博客 - ITeye技术网站