-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathTree.java
79 lines (69 loc) · 1.9 KB
/
MathTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.LinkedList;
class MathTree {
static Node root;
MathTree(String expr) {
root = buildTree(listify(expr.replaceAll(" ", "")));
}
private LinkedList<Node> listify(String expr) {
LinkedList<Node> list = new LinkedList<>();
Node n = null;
for (int i = 0; i < expr.length(); i += n.value.length()) {
String currExp = expr.substring(i);
n = Operator.parse(currExp);
if (n == null) {
n = Digit.parse(currExp);
}
list.add(n);
}
return list;
}
private Node getfirstOperator(LinkedList<Node> nodes) {
Node first = null;
int pri = 0;
for (Node n : nodes) {
if (Operator.isOperator(n)) {
if (n.isOneOf(Operator.SUB, Operator.ADD)) {
first = n;
break;
} else if (pri < 3 && n.isOneOf(Operator.DIVIDE, Operator.MULTIPLY)) {
first = n;
pri = 3;
} else if (pri < 2 && n.isOneOf(Operator.POW)) {
first = n;
pri = 3;
} else if (pri < 1 && n.isOneOf(Operator.COS, Operator.SIN, Operator.TAN)) {
first = n;
pri = 1;
}
}
}
return first;
}
private Node buildTree(LinkedList<Node> nodes) {
if (nodes.size() == 1) return nodes.getFirst();
if (nodes.isEmpty()) return null;
Node operator = getfirstOperator(nodes);
LinkedList<Node> left = new LinkedList<Node>();
LinkedList<Node> right = new LinkedList<Node>();
boolean found = false;
for (Node n : nodes) {
if (n == operator) {
found = true;
} else {
if (!found) left.add(n);
else right.add(n);
}
}
operator.left = buildTree(left);
operator.right = buildTree(right);
return operator;
}
@Override
public String toString() {
if (root != null) return root.toString();
else return "Empty tree";
}
static double calculate() {
return root.calculate();
}
}