-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeOperations.java
More file actions
357 lines (320 loc) · 14 KB
/
Copy pathTreeOperations.java
File metadata and controls
357 lines (320 loc) · 14 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package GP;
import java.util.ArrayList;
public class TreeOperations {
public static ArrayList<Node> getAllNodesBFS(OperatorNode root) {
ArrayList<Node> allNodes = new ArrayList<>();
if (root == null) return allNodes;
ArrayList<Node> queue = new ArrayList<>();
queue.add(root);
while (!queue.isEmpty()) {
Node currentNode = queue.remove(0);
allNodes.add(currentNode);
if (currentNode instanceof UnopNode) {
Node child = ((UnopNode) currentNode).getChild();
if (child != null) {
queue.add(child);
}
} else if (currentNode instanceof BinopNode) {
Node leftChild = ((BinopNode) currentNode).getLeftChild();
Node rightChild = ((BinopNode) currentNode).getRightChild();
if (leftChild != null) {
queue.add(leftChild);
}
if (rightChild != null) {
queue.add(rightChild);
}
}
}
// for (Node node : allNodes) {
// //print parents
// if (node instanceof OperatorNode) {
// System.out.println("Node: " + node + ", Parent: " + ((OperatorNode) node).parent);
// }
// else if (node instanceof TerminalNode) {
// System.out.println("Node: " + node + ", Parent: " + ((TerminalNode) node).parent);
// }
// else {
// System.out.println("Node: " + node + ", Parent: null");
// }
// }
return allNodes;
}
public static ArrayList<Node> getAllNodesDFS(OperatorNode root) {
ArrayList<Node> allNodes = new ArrayList<>();
if (root == null) return allNodes;
ArrayList<Node> stack = new ArrayList<>();
stack.add(root);
while (!stack.isEmpty()) {
Node currentNode = stack.remove(stack.size() - 1);
allNodes.add(currentNode);
if (currentNode instanceof UnopNode) {
Node child = ((UnopNode) currentNode).getChild();
if (child != null) {
stack.add(child);
}
} else if (currentNode instanceof BinopNode) {
Node leftChild = ((BinopNode) currentNode).getLeftChild();
Node rightChild = ((BinopNode) currentNode).getRightChild();
if (rightChild != null) {
stack.add(rightChild);
}
if (leftChild != null) {
stack.add(leftChild);
}
}
}
return allNodes;
}
public static ArrayList<TerminalNode> getTerminalNodes(OperatorNode root, ArrayList<TerminalNode> terminalNodes) {
if (root == null) return new ArrayList<>();
if (root instanceof UnopNode) {
Node child = ((UnopNode) root).getChild();
if (child == null) {
throw new IllegalArgumentException("Function node has no child");
}
if (child instanceof TerminalNode) {
TerminalNode c = ((TerminalNode) child);
terminalNodes.add(c);
}
else if (child instanceof OperatorNode) {
OperatorNode c = ((OperatorNode) child);
terminalNodes.addAll(getTerminalNodes(c, terminalNodes));
}
return terminalNodes;
}
if (root instanceof BinopNode) {
Node leftChild = ((BinopNode) root).getLeftChild();
Node rightChild = ((BinopNode) root).getRightChild();
if (leftChild == null || rightChild == null) {
throw new IllegalArgumentException("Function node has no child");
}
if (leftChild instanceof TerminalNode) {
TerminalNode c = ((TerminalNode) leftChild);
terminalNodes.add(c);
}
else if (leftChild instanceof OperatorNode) {
OperatorNode c = ((OperatorNode) leftChild);
terminalNodes.addAll(getTerminalNodes(c, terminalNodes));
}
if (rightChild instanceof TerminalNode) {
TerminalNode c = ((TerminalNode) rightChild);
terminalNodes.add(c);
}
else if (rightChild instanceof OperatorNode) {
OperatorNode c = ((OperatorNode) rightChild);
terminalNodes.addAll(getTerminalNodes(c, terminalNodes));
}
return terminalNodes;
}
throw new IllegalArgumentException("Unknown node type: " + root.getClass().getName());
}
public static ArrayList<OperatorNode> getNonTerminalNodes(OperatorNode root, ArrayList<OperatorNode> nonTerminalNodes) {
if (root == null) return new ArrayList<>();
if (root instanceof UnopNode) {
Node child = ((UnopNode) root).getChild();
if (child == null) {
throw new IllegalArgumentException("Function node has no child");
}
nonTerminalNodes.add(root);
if (child instanceof OperatorNode) {
OperatorNode c = ((OperatorNode) child);
nonTerminalNodes.add(c);
nonTerminalNodes.addAll(getNonTerminalNodes(c, nonTerminalNodes));
}
return nonTerminalNodes;
}
if (root instanceof BinopNode) {
Node leftChild = ((BinopNode) root).getLeftChild();
Node rightChild = ((BinopNode) root).getRightChild();
if (leftChild == null || rightChild == null) {
throw new IllegalArgumentException("Function node has no child");
}
nonTerminalNodes.add(root);
if (leftChild instanceof OperatorNode) {
OperatorNode c = ((OperatorNode) leftChild);
nonTerminalNodes.add(c);
nonTerminalNodes.addAll(getNonTerminalNodes(c, nonTerminalNodes));
}
if (rightChild instanceof OperatorNode) {
OperatorNode c = ((OperatorNode) rightChild);
nonTerminalNodes.add(c);
nonTerminalNodes.addAll(getNonTerminalNodes(c, nonTerminalNodes));
}
return nonTerminalNodes;
}
throw new IllegalArgumentException("Unknown node type: " + root.getClass().getName());
}
public static Node getRandomNode(OperatorNode root) {
if (root == null) {
throw new IllegalArgumentException("Tree is empty");
}
ArrayList<Node> allNodes = getAllNodesBFS(root);
if (allNodes.isEmpty()) {
throw new IllegalArgumentException("Tree is empty");
}
int randomIndex = Utils.getGlobalRandom().nextInt(allNodes.size());
if (Config.DEBUG_PRINT) System.out.println("getRandomNode result: " + allNodes.get(randomIndex));
return allNodes.get(randomIndex);
}
public static TerminalNode getRandomTerminalNode(OperatorNode root) {
if (root == null) {
throw new IllegalArgumentException("Tree is empty");
}
ArrayList<TerminalNode> terminalNodes = getTerminalNodes(root, new ArrayList<>());
if (terminalNodes.isEmpty()) {
throw new IllegalArgumentException("Tree has no terminal nodes");
}
int randomIndex = Utils.getGlobalRandom().nextInt(terminalNodes.size());
if (Config.DEBUG_PRINT) System.out.println("getRandomTerminalNode result: " + terminalNodes.get(randomIndex));
return terminalNodes.get(randomIndex);
}
public static OperatorNode getRandomNonTerminalNode(OperatorNode root) {
if (root == null) {
throw new IllegalArgumentException("Tree is empty");
}
ArrayList<OperatorNode> nonTerminalNodes = getNonTerminalNodes(root, new ArrayList<>());
if (nonTerminalNodes.isEmpty()) {
throw new IllegalArgumentException("Tree has no non-terminal nodes");
}
int randomIndex = Utils.getGlobalRandom().nextInt(nonTerminalNodes.size());
if (Config.DEBUG_PRINT) System.out.println("getRandomNonTerminalNode result: " + nonTerminalNodes.get(randomIndex));
return nonTerminalNodes.get(randomIndex);
}
// get parent
// public static Node getParent(OperatorNode root, Node child) {
// System.out.println("getParent: " + root + " " + child);
// if (root == null || child == null) {
// return null;
// }
// if (root instanceof UnopNode) {
// if (((UnopNode) root).getChild() == child) {
// return root;
// }
// return getParent(((UnopNode) root).getChild(), child);
// }
// if (root instanceof BinopNode) {
// if (((BinopNode) root).getLeftChild() == child || ((BinopNode) root).getRightChild() == child) {
// return root;
// }
// Node leftParent = getParent(((BinopNode) root).getLeftChild(), child);
// if (leftParent != null) {
// return leftParent;
// }
// return getParent(((BinopNode) root).getRightChild(), child);
// }
// if (root instanceof TerminalNode) {
// return null;
// }
// throw new IllegalArgumentException("Unknown node type: " + root.getClass().getName());
// }
public static OperatorNode getParent(Node node) {
if (node == null) {
throw new IllegalArgumentException("Node is null");
}
if (Config.DEBUG_PRINT) System.out.println("getParent: " + node);
if (node instanceof TerminalNode) {
return ((TerminalNode) node).parent;
} else if (node instanceof OperatorNode) {
return ((OperatorNode) node).parent;
}
throw new IllegalArgumentException("Unknown node type: " + node.getClass().getName());
}
// swap nodes
public static void swapNodes(OperatorNode root, Node node1, Node node2) {
if (root == null || node1 == null || node2 == null) {
if (Config.DEBUG_PRINT) MagicPrinter.printError("Tree or nodes are null. root=" + root + " node1=" + node1 + " node2=" + node2);
return;
}
if (node1 == node2) {
System.out.println("Both nodes are the same. No swap needed.");
return;
}
// Get the parents of node1 and node2
Node parent1 = getParent(node1);
Node parent2 = getParent(node2);
if (parent1 == null || parent2 == null) {
System.out.println("One or both nodes do not have a parent.");
return;
}
if (Config.DEBUG_PRINT) System.out.println("swapping nodes: " + node1 + " and " + node2);
if (Config.DEBUG_PRINT) System.out.println("before swap: " + parent1);
if (parent1 instanceof UnopNode) {
((UnopNode) parent1).setChild(node2);
if (Config.DEBUG_PRINT) System.out.println("1: set: " + ((UnopNode) parent1).getChild() + " to " + node2);
} else if (parent1 instanceof BinopNode) {
if (((BinopNode) parent1).getLeftChild() == node1) {
((BinopNode) parent1).setLeftChild(node2);
if (Config.DEBUG_PRINT) System.out.println("2: set: " + ((BinopNode) parent1).getLeftChild() + " to " + node2);
} else {
((BinopNode) parent1).setRightChild(node2);
if (Config.DEBUG_PRINT) System.out.println("3: set: " + ((BinopNode) parent1).getRightChild() + " to " + node2);
}
}
else {
System.out.println("Parent1 is not a valid operator node.");
return;
}
if (Config.DEBUG_PRINT) System.out.println("after swap: " + parent1);
if (Config.DEBUG_PRINT) System.out.println("before swap: " + parent2);
if (parent2 instanceof UnopNode) {
((UnopNode) parent2).setChild(node1);
} else if (parent2 instanceof BinopNode) {
if (((BinopNode) parent2).getLeftChild() == node2) {
((BinopNode) parent2).setLeftChild(node1);
} else {
((BinopNode) parent2).setRightChild(node1);
}
}
else {
System.out.println("Parent2 is not a valid operator node.");
return;
}
if (Config.DEBUG_PRINT) System.out.println("after swap: " + parent2);
}
public static int getHeight(Node root) {
if (root == null) {
return 0;
}
if (root instanceof TerminalNode) {
return 1;
}
if (root instanceof UnopNode) {
return 1 + getHeight(((UnopNode) root).getChild());
}
if (root instanceof BinopNode) {
return 1 + Math.max(getHeight(((BinopNode) root).getLeftChild()),
getHeight(((BinopNode) root).getRightChild()));
}
throw new IllegalArgumentException("Unknown node type: " + root.getClass().getName());
}
public static int getNumberOfNodes(Node root) {
if (root == null) {
return 0;
}
if (root instanceof TerminalNode) {
return 1;
}
if (root instanceof UnopNode) {
return 1 + getNumberOfNodes(((UnopNode) root).getChild());
}
if (root instanceof BinopNode) {
return 1 + getNumberOfNodes(((BinopNode) root).getLeftChild())
+ getNumberOfNodes(((BinopNode) root).getRightChild());
}
throw new IllegalArgumentException("Unknown node type: " + root.getClass().getName());
}
// public static OperatorNode getTreeRoot(Node node) {
// if (node == null) {
// throw new IllegalArgumentException("Node is null");
// }
// while (node != null) {
// if (node instanceof OperatorNode) {
// OperatorNode n = ((OperatorNode) node).parent;
// if
// }
// if (node instanceof TerminalNode) {
// node = ((TerminalNode) node).parent;
// }
// }
// }
}