-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
258 lines (226 loc) · 4.26 KB
/
Node.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
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
import java.util.ArrayList;
import java.util.List;
/*
* The Node class is the basic structural unit of
* the AST (Abstract Syntax Tree). This was my chosen
* method of representing the mathematical expressions.
* Each node is arranged such that it is either an operator
* or operand. It is binary so that each node has either
* 0 or 2 children, seen in the children ArrayList.
* The AST is arranged such that "(x+2)*(x-3)+1" is:
* +
* 1
* *
* +
* x
* 2
* +
* x
* -3
*
* Most methods in this class are just "get" methods
* or other similar methods for the convenience of programming.
*/
public class Node {
private List<Node> children = new ArrayList<Node>();
private Node parent;
private Expr data;
public Node()
{
}
public Node(Expr e)
{
data=e;
}
public Expr getData()
{
return data;
}
public void setData(Expr e)
{
data=e;
}
public List<Node> getChildren()
{
return children;
}
public Node getChild(int pos)
{
return children.get(pos);
}
public void setChildren(List<Node> c)
{
children = new ArrayList<Node>();
for (Node child : c)
{
children.add(child);
}
}
public Node getParent()
{
return parent;
}
public void setParent(Node p)
{
parent=p;
}
public Node setNode(Node node)
{
parent=node.getParent();
data=node.getData();
this.setChildren(node.getChildren());
return this;
}
public Node addChild(Node node)
{
this.getChildren().add(node);
node.setParent(this);
return node;
}
public boolean typeIs(String type)
{
return this.getData().getType().equals(type);
}
public boolean typeIsOr(String a, String b)
{
return (this.typeIs(a) || this.typeIs(b));
}
public boolean sameType(Node node)
{
return this.getData().getType().equals(node.getData().getType());
}
public boolean dataIs(String data)
{
return this.getData().getData().equals(data);
}
public boolean dataIsOr(String a, String b)
{
return (this.dataIs(a) || this.dataIs(b));
}
public boolean dataIsIn(String[] s)
{
for (String i : s)
{
if (i.equals(data.getData()))
return true;
}
return false;
}
public boolean sameData(Node node)
{
return this.dataIs(node.getData().getData());
}
public int comparePrec(Node node)
{
return this.getData().getPrec() - node.getData().getPrec();
}
public boolean hasChildOfType(String type)
{
for (Node node : children)
{
if (node.typeIs(type))
return true;
}
return false;
}
public int getChildOfTypeInd(String type)
{
// returns index of child of type "type"
for (int i = 0; i < children.size(); i++)
{
if (children.get(i).typeIs(type))
return i;
}
return -1;
}
public boolean hasChildOfData(String s)
{
for (Node node : children)
{
if (node.dataIs(s))
return true;
}
return false;
}
public int getChildOfDataInd(String s)
{
for (int i = 0; i < children.size(); i++)
{
if (children.get(i).dataIs(s))
return i;
}
return -1;
}
public int getOtherChildInd()
{
List<Node> Children = this.getParent().getChildren();
for (int i = 0; i < Children.size(); i++)
{
if (Children.get(i) != this)
return i;
}
return -1;
}
public Node getChildOfType(String type)
{
// returns index of child of type "type"
for (int i = 0; i < children.size(); i++)
{
if (children.get(i).typeIs(type))
return children.get(i);
}
return null;
}
public Node getChildOfData(String s)
{
for (int i = 0; i < children.size(); i++)
{
if (children.get(i).dataIs(s))
return children.get(i);
}
return null;
}
public Node getOtherChild()
{
if (this.isRoot())
{
return this;
}
else
{
List<Node> Children = this.getParent().getChildren();
for (int i = 0; i < Children.size(); i++)
{
if (Children.get(i).isSame(this))
return Children.get((i+1)%2);
}
}
return null;
}
public boolean isSame(Node node)
{
if (node.sameData(this))
{
if (children.size() == 0 && node.getChildren().size() == 0)
{
return true;
}
else
{
return node.getChild(0).isSame(this.getChild(0)) && node.getChild(1).isSame(this.getChild(1));
}
}
else
{
return false;
}
}
public boolean isRoot()
{
return parent==null;
}
public boolean hasChildren()
{
return children.size()!=0;
}
}