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
The expression tree will be like
After building the tree, you just need to return root node
分析:
先把expression 转成RPN,然后遇到operator,直接建立一颗树,数的root是operator, 左右节点从stack里面pop就可以,然后把该root压栈。
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; } } }
相关文章推荐
- LeetCode 376. Wiggle Subsequence(摇摆子序列)
- [leetcode] 376. Wiggle Subsequence 解题报告
- 解决SurfaceView调用setZOrderOnTop(true)遮挡其他控件的问题
- LeetCode 232. Implement Queue using Stacks
- LeetCode 62. Unique Paths
- LeetCode 225. Implement Stack using Queues
- Android适配——采用Values-dpi-wSize X hSize 模式,并分析原理
- LeetCode 303. Range Sum Query – Immutable
- LeetCode 347. Top K Frequent Elements
- LeetCode 63. Unique Paths II
- iOS 分类之UIColor+Hex
- LeetCode 96. Unique Binary Search Trees
- LeetCode 300. Longest Increasing Subsequence
- LeetCode 304. Range Sum Query 2D – Immutable
- UITableView修改索引的方法
- ionic生成apk使用build命令下载gradle-2.2.1-all.zip卡,解决方案
- LeetCode 51. N-Queens
- LeetCode 52. N-Queens II
- php学习笔记之require()和include()
- HashMap按value的顺序输出