F_G

许多问题需要说清楚就可以&&走永远比跑来的重要

导航

[Lintcode] Expression Tree Build

Expression Tree Build

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.

Have you met this question in a real interview? 

Yes
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 [-].

 

这是个老题了,原先计算的结果是当做一个数字push到数据栈当中的,现在变成了节点。而且这里的特点是,根部没有计算问题,只需要在优先级的基础上构建表达式树。

 

 1 /**
 2  * Definition of ExpressionTreeNode:
 3  * public class ExpressionTreeNode {
 4  *     public String symbol;
 5  *     public ExpressionTreeNode left, right;
 6  *     public ExpressionTreeNode(String symbol) {
 7  *         this.symbol = symbol;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 import java.util.*;
13 
14 public class Solution {
15     /**
16      * @param expression: A string array
17      * @return: The root of expression tree
18      */
19     public ExpressionTreeNode build(String[] expression) {
20         // write your code here
21         Stack<ExpressionTreeNode> op   = new Stack<ExpressionTreeNode>();
22         Stack<ExpressionTreeNode> data = new Stack<ExpressionTreeNode>();
23         for(int i=0;i<expression.length;i++){
24             String tmp = expression[i];
25             char firstc = tmp.charAt(0);
26             if(!(firstc<='9'&&firstc>='0')){
27                 //System.out.println("get op "+ tmp);
28                 switch(firstc){
29                     case '(':
30                         ExpressionTreeNode node = new ExpressionTreeNode(tmp);
31                         op.push(node);
32                         break;
33                     case '+':
34                     case '-':
35                         while(!op.isEmpty()&&op.peek().symbol.charAt(0)!='('){
36                             ExpressionTreeNode opnode = op.pop();
37                             ExpressionTreeNode data1 = data.pop();
38                             ExpressionTreeNode data2 = data.pop();
39                             opnode.left = data2;
40                             opnode.right = data1;
41                             data.push(opnode);
42                         }
43                         ExpressionTreeNode node2 = new ExpressionTreeNode(tmp);
44                         op.push(node2);
45                         break;
46                     case '*':
47                     case '/':
48                         while(!op.isEmpty()&&(op.peek().symbol.charAt(0)=='*'||op.peek().symbol.charAt(0)=='/')){
49                             ExpressionTreeNode opnode = op.pop();
50                             ExpressionTreeNode data1 = data.pop();
51                             ExpressionTreeNode data2 = data.pop();
52                             opnode.left = data2;
53                             opnode.right = data1;
54                             data.push(opnode);
55                         }
56                         ExpressionTreeNode node3 = new ExpressionTreeNode(tmp);
57                         op.push(node3);
58                         break;
59                     case ')':
60                         while(op.peek().symbol.charAt(0)!='('){
61                             ExpressionTreeNode opnode = op.pop();
62                             ExpressionTreeNode data1 = data.pop();
63                             ExpressionTreeNode data2 = data.pop();
64                             opnode.left = data2;
65                             opnode.right = data1;
66                             data.push(opnode);
67                         }
68                         op.pop();
69                 }
70             }else{
71                 //System.out.println("add data "+tmp);
72                 ExpressionTreeNode data1 = new ExpressionTreeNode(tmp);
73                 data.push(data1);
74             }
75         }
76         while(!op.isEmpty()){
77             ExpressionTreeNode opnode = op.pop();
78             ExpressionTreeNode data1 = data.pop();
79             ExpressionTreeNode data2 = data.pop();
80             opnode.left = data2;
81             opnode.right = data1;
82             data.push(opnode);
83         }
84         if(data.isEmpty()) return null;
85         return data.pop();
86     }
87 }

 

posted on 2015-09-02 21:48  F_G  阅读(1285)  评论(0编辑  收藏  举报