-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathNode.java
More file actions
329 lines (305 loc) · 8.46 KB
/
Copy pathNode.java
File metadata and controls
329 lines (305 loc) · 8.46 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package structure;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Node implements Serializable {
private static final long serialVersionUID = -1127009463482561785L;
public String label;
public List<Node> children;
public int quantIndex; // Assign this to max_quant+1 for question
public QuantSpan qs;
public double val;
public String infRuleType;
public String key;
public Node() {
children = new ArrayList<>();
}
// For internal nodes
public Node(String label, List<Node> children) {
this();
this.label = label;
this.children = children;
}
// For leaves
public Node(int quantindex, QuantSpan qs, String label) {
this();
this.quantIndex = quantindex;
this.qs = qs;
this.val = qs.val;
this.label = label;
}
public Node(Node other) {
this();
this.quantIndex = other.quantIndex;
this.qs = other.qs;
this.label = other.label;
this.val = other.val;
this.infRuleType = other.infRuleType;
this.key = other.key;
for(Node child : other.children) {
this.children.add(new Node(child));
}
}
@Override
public String toString() {
if(children.size() == 0) return "" + val;
String labelSym = null;
switch(label) {
case "ADD" : labelSym = "+"; break;
case "SUB" : labelSym = "-"; break;
case "MUL" : labelSym = "*"; break;
case "DIV" : labelSym = "/"; break;
}
return "("+children.get(0).toString() + " " +
labelSym +
" " + children.get(1).toString()+")";
}
public String toTemplateString() {
if(children.size() == 0) return "NUM";
return "("+children.get(0).toTemplateString() + " " +
label + "_" +
children.get(1).toTemplateString()+")";
}
public String toStringForPython() {
if(children.size() == 0) return "" + qs.val + "||" + quantIndex;
String labelSym = null;
switch(label) {
case "ADD" : labelSym = "+"; break;
case "SUB" : labelSym = "-"; break;
case "MUL" : labelSym = "*"; break;
case "DIV" : labelSym = "/"; break;
}
return "("+children.get(0).toStringForPython() + labelSym +
children.get(1).toStringForPython()+")";
}
public List<Node> getLeaves() {
List<Node> leaves = new ArrayList<Node>();
if(children.size() == 0) {
leaves.add(this);
} else {
leaves.addAll(children.get(0).getLeaves());
leaves.addAll(children.get(1).getLeaves());
}
return leaves;
}
public List<Node> getAllSubNodes() {
List<Node> all = new ArrayList<Node>();
all.add(this);
if(children.size() == 2) {
all.addAll(children.get(0).getAllSubNodes());
all.addAll(children.get(1).getAllSubNodes());
}
return all;
}
// Input is quantIndex
public boolean hasLeaf(int index) {
if(label.equals("NUM")) {
if(quantIndex == index) return true;
else return false;
} else {
return (children.get(0).hasLeaf(index) ||
children.get(1).hasLeaf(index));
}
}
// Input are quantIndices
public String findLabelofLCA(int i, int j) {
for(Node node : getAllSubNodes()) {
if(!node.label.equals("NUM") && node.children.get(0).hasLeaf(i)
&& node.children.get(1).hasLeaf(j)) {
return node.label;
}
if((node.label.equals("SUB") || node.label.equals("DIV"))
&& node.children.get(0).hasLeaf(j)
&& node.children.get(1).hasLeaf(i)) {
return node.label+"_REV";
}
if((node.label.equals("ADD") || node.label.equals("MUL"))
&& node.children.get(0).hasLeaf(j)
&& node.children.get(1).hasLeaf(i)) {
return node.label;
}
}
return "NONE";
}
// Input are quantIndices
public Node findLCAnode(int i, int j) {
for(Node node : getAllSubNodes()) {
if(!node.label.equals("NUM") && node.children.get(0).hasLeaf(i)
&& node.children.get(1).hasLeaf(j)) {
return node;
}
if((node.label.equals("SUB") || node.label.equals("DIV"))
&& node.children.get(0).hasLeaf(j)
&& node.children.get(1).hasLeaf(i)) {
return node;
}
if((node.label.equals("ADD") || node.label.equals("MUL"))
&& node.children.get(0).hasLeaf(j)
&& node.children.get(1).hasLeaf(i)) {
return node;
}
}
return null;
}
// Input is quantIndex
public String findRelevanceLabel(int i) {
for(Node node : getLeaves()) {
if(node.quantIndex == i) {
return "REL";
}
}
return "IRR";
}
public double getValue() {
if(label.equals("NUM")) return qs.val;
if(label.equals("ADD")) return children.get(0).getValue() +
children.get(1).getValue();
if(label.equals("SUB")) return children.get(0).getValue() -
children.get(1).getValue();
if(label.equals("MUL")) return children.get(0).getValue() *
children.get(1).getValue();
if(label.equals("DIV")) return children.get(0).getValue() /
children.get(1).getValue();
return 0.0;
}
public boolean isEqual(Node node) {
if(!label.equals(node.label)) {
return false;
}
if(label.equals("NUM")) {
if(quantIndex == node.quantIndex) {
return true;
}
else {
return false;
}
}
if(label.equals("ADD") || label.equals("MUL")) {
return (children.get(0).isEqual(node.children.get(0)) &&
children.get(1).isEqual(node.children.get(1))) ||
(children.get(0).isEqual(node.children.get(1)) &&
children.get(1).isEqual(node.children.get(0)));
}
return (children.get(0).isEqual(node.children.get(0)) &&
children.get(1).isEqual(node.children.get(1)));
}
public static Node parseNode(String eqString) {
eqString = eqString.trim();
// System.out.println("EqString : "+eqString);
int index = eqString.indexOf("=");
if(index > 0) eqString = eqString.substring(index+1).trim();
if(eqString.charAt(0)=='(' && eqString.charAt(eqString.length()-1)==')') {
eqString = eqString.substring(1, eqString.length()-1).trim();
}
index = indexOfMathOp(eqString, Arrays.asList('+', '-', '*', '/'));
Node node = new Node();
if(index > 0) {
if(eqString.charAt(index) == '+') node.label = "ADD";
else if(eqString.charAt(index) == '-') node.label = "SUB";
else if(eqString.charAt(index) == '*') node.label = "MUL";
else if(eqString.charAt(index) == '/') node.label = "DIV";
else node.label = "ISSUE";
node.children.add(parseNode(eqString.substring(0, index)));
node.children.add(parseNode(eqString.substring(index+1)));
return node;
} else {
node.label = "NUM";
node.val = Double.parseDouble(eqString.trim());
}
return node;
}
public static int indexOfMathOp(String equationString, List<Character> keys) {
for(int index=0; index<equationString.length(); ++index) {
if(keys.contains(equationString.charAt(index))) {
int open = 0, close = 0;
for(int i=index; i>=0; --i) {
if(equationString.charAt(i) == ')') close++;
if(equationString.charAt(i) == '(') open++;
}
if(open==close) {
return index;
}
}
}
return -1;
}
public Node getParent(Node child) {
for(Node node : getAllSubNodes()) {
if(node.children.size() == 2 && (node.children.get(0) == child
|| node.children.get(1) == child)) {
return node;
}
}
return null;
}
public List<String> getPath(int i, int j) {
Node n1 = null, n2 = null;
for(Node leaf : getLeaves()) {
if(leaf.quantIndex == i) {
n1 = leaf;
}
}
for(Node leaf : getLeaves()) {
if(leaf.quantIndex == j) {
n2 = leaf;
}
}
List<String> path1 = new ArrayList<>();
List<String> path2 = new ArrayList<>();
Node lcaNode = findLCAnode(i, j);
Node parent = null;
String postFix = "";
while(n1 != lcaNode && n1 != null) {
postFix = "";
parent = getParent(n1);
if(parent.label.equals("SUB") || parent.label.equals("DIV")) {
if(parent.children.get(1) == n1) {
postFix = "_REV";
}
}
n1 = parent;
path1.add(n1.label+postFix);
}
while(n2 != lcaNode && n2 != null) {
postFix = "";
parent = getParent(n2);
if(parent.label.equals("SUB") || parent.label.equals("DIV")) {
if(parent.children.get(1) == n2) {
postFix = "_REV";
}
}
n2 = parent;
path2.add(n2.label+postFix);
}
Collections.reverse(path2);
path2.remove(0);
path1.addAll(path2);
return path1;
}
public List<String> getPathToRoot(int i) {
Node n = null;
for(Node leaf : getLeaves()) {
if(leaf.quantIndex == i) {
n = leaf;
}
}
List<String> path = new ArrayList<>();
Node parent = null;
String postFix = "";
while(true) {
postFix = "";
parent = getParent(n);
if(parent == null) break;
if(parent.label.equals("SUB") || parent.label.equals("DIV")) {
if(parent.children.get(1) == n) {
postFix = "_REV";
}
}
n = parent;
path.add(n.label+postFix);
}
return path;
}
}