您的位置:首页 > 产品设计 > UI/UE

Expression Tree Build

2016-07-22 07:53 441 查看
The structure of Expression Tree is a binary tree to evaluate certain expressions.
All leaves of the Expression Tree have an number string value. All non-leaves of the Expression Tree have an operator string value.

Now, given an expression array, build the expression tree of this expression, return the root of this expression tree.

Clarification

See wiki:
Expression Tree

Example

For the expression
(2*6-(23+7)/(1+2))
(which can be represented by ["2" "*" "6" "-" "(" "23" "+" "7" ")" "/" "(" "1" "+" "2" ")"]).
The expression tree will be like

[ - ]
/          \
[ * ]              [ / ]
/     \           /         \
[ 2 ]  [ 6 ]      [ + ]        [ + ]
/    \       /      \
[ 23 ][ 7 ] [ 1 ]   [ 2 ] .

After building the tree, you just need to return root node
[-]
.

分析:

先把expression 转成RPN,然后遇到operator,直接建立一颗树,数的root是operator, 左右节点从stack里面pop就可以,然后把该root压栈。

/**
* Definition of ExpressionTreeNode:
* public class ExpressionTreeNode {
*     public String symbol;
*     public ExpressionTreeNode left, right;
*     public ExpressionTreeNode(String symbol) {
*         this.symbol = symbol;
*         this.left = this.right = null;
*     }
* }
*/

/**
* Definition of ExpressionTreeNode:
* public class ExpressionTreeNode {
*     public String symbol;
*     public ExpressionTreeNode left, right;
*     public ExpressionTreeNode(String symbol) {
*         this.symbol = symbol;
*         this.left = this.right = null;
*     }
* }
*/

public class Solution {
/**
* @param expression: A string array
* @return: The root of expression tree
*/
public ExpressionTreeNode build(String[] expression) {

ArrayList<String> RPN = convertToRPN(expression);
if (RPN == null || RPN.size() == 0) return null;

Stack<ExpressionTreeNode> stack   = new Stack<ExpressionTreeNode>();
for (String str : RPN) {
if (isOperator(str)) {
ExpressionTreeNode opnode = new ExpressionTreeNode(str);
ExpressionTreeNode data1 = stack.pop();
ExpressionTreeNode data2 = stack.pop();
opnode.left = data2;
opnode.right = data1;
stack.push(opnode);
} else {
stack.push(new ExpressionTreeNode(str));
}
}
return stack.pop();
}

public ArrayList<String> convertToRPN(String[] expression) {
ArrayList<String> list = new ArrayList<String>();
Stack<String> stack = new Stack<String>();

for (int i = 0; i < expression.length; i++) {
String str = expression[i];
if (isOperator(str)) {
if (str.equals("(")) {
stack.push(str);
} else if (str.equals(")")) {
while (!stack.isEmpty() && !stack.peek().equals("(")) {
list.add(stack.pop());
}
stack.pop();
} else {
while (!stack.isEmpty() && order(str) <= order(stack.peek())) {
list.add(stack.pop());
}
stack.push(str);
}
} else {
list.add(str);
}
}
while (!stack.isEmpty()) {
list.add(stack.pop());
}
return list;
}

private boolean isOperator(String str) {
if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") || str.equals("(")
|| str.equals(")")) {
return true;
}
return false;
}

private int order(String a) {
if (a.equals("*") || a.equals("/")) {
return 2;
} else if (a.equals("+") || a.equals("-")) {
return 1;
} else {
return 0;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: