-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionNode.java
More file actions
executable file
·137 lines (109 loc) · 3.34 KB
/
ExpressionNode.java
File metadata and controls
executable file
·137 lines (109 loc) · 3.34 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
import java.util.LinkedList;
/**
* An abstract class that constructs a tree from the given list of expressions. Converts the expression into a String
* and proceeds to flatten the tree if the parent node contains the same operator in the lext level of its children.
*/
public abstract class ExpressionNode implements Expression
{
private String _data;
private CompoundExpression _parent;
private LinkedList<ExpressionNode> _children;
public ExpressionNode(String data)
{
_data = data;
_parent = null;
_children = new LinkedList<ExpressionNode>();
}
/**
* @return Return the data associated with this ExpressionNode.
*/
public String getData()
{
return _data;
}
/**
*
* @return Return a linked list of children associated with an ExpressionNode.
*/
public LinkedList<ExpressionNode> getChildren()
{
return _children;
}
/**
* Method that recursively calls itself to create a String of type Expression and its children
* that starts at the given indent level.
* @param stringBuilder the stringBuilder that appends a name and a new line to create a String
* @param indentLevel the current indentation level
*/
public void convertToString(StringBuilder stringBuilder, int indentLevel)
{
indent(stringBuilder, indentLevel);
stringBuilder.append(_data);
stringBuilder.append('\n');
for (Expression expr : _children) {
expr.convertToString(stringBuilder, indentLevel + 1);
}
System.out.println(stringBuilder.toString());
}
/**
* Helper method for indentation for StirngBuilder
* @param stringBuilder the stringBuilder that appends each tab character
* @param indentLevel the number of tabs that must be appended to StirngBuilder
*/
public static void indent(StringBuilder stringBuilder, int indentLevel) {
for(int i = 0; i < indentLevel; i++) {
stringBuilder.append('\t');
}
}
/**
* Returns the expression's parent
* @return the expression's parent
*/
public CompoundExpression getParent()
{
return _parent;
}
/**
* Sets the parent to the specified parent expression
* @param parent the CompoundExpression that should be the parent of the target object
*/
public void setParent (CompoundExpression parent)
{
_parent = parent;
}
/**
* Creates and returns a deep copy of the expression.
* The entire tree rooted at the target node is copied, i.e.,
* the copied Expression is as deep as possible.
* @return the deep copy
*/
public Expression deepCopy()
{
return null;
}
/**
* Recursively flattens the expression as much as possible
* throughout the entire tree. Specifically, in every multiplicative
* or additive expression x whose first or last
* child c is of the same type as x, the children of c will be added to x, and
* c itself will be removed. This method modifies the expression itself.
*/
public void flatten()
{
if (_children.size() > 0)
{
int i = 0;
while(i < _children.size())
{
ExpressionNode subExpr = _children.get(i);
subExpr.flatten();
if (_data.equals(subExpr._data))
{
_children.addAll(subExpr._children);
_children.remove(subExpr);
}
i++;
}
}
}
}