001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
|
package iterator.exprtwo;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;
import java.util.Set;
import java.util.HashSet;
import enumeration.Op;
public interface Expr {
public int evaluate();
public Iterator<Object> preorderIterator();
public Iterator<Object> postorderIterator();
public Iterator<Object> breadthIterator();
}
abstract class AbsExpr implements Expr {
public abstract int evaluate();
public Iterator<Object> preorderIterator() {
PreorderIterator i = new PreorderIterator();
i.addNode(this);
return i;
}
public Iterator<Object> postorderIterator() {
PostorderIterator i = new PostorderIterator();
i.addNode(this);
return i;
}
public Iterator<Object> breadthIterator() {
BreadthIterator i = new BreadthIterator();
i.addNode(this);
return i;
}
abstract Object acceptPreOrder(PreorderIterator i);
abstract Object acceptPostOrder(PostorderIterator i);
abstract Object acceptBreadth(BreadthIterator i);
}
class Const extends AbsExpr {
private final int v;
public Const(int v) {
this.v = v;
}
public int evaluate() {
return v;
}
Object acceptPreOrder(PreorderIterator i) {
return v;
}
Object acceptPostOrder(PostorderIterator i) {
i.markVisited(this);
return v;
}
Object acceptBreadth(BreadthIterator i) {
i.markVisited(this);
return v;
}
}
class BinOp extends AbsExpr {
private final AbsExpr l;
private final AbsExpr r;
private final Op op;
public BinOp(AbsExpr l, Op op, AbsExpr r) {
if ((l == null) || (op == null) || (r == null)) {
throw new IllegalArgumentException();
}
this.op = op;
this.l = l;
this.r = r;
}
public int evaluate() {
return op.eval(l.evaluate(), r.evaluate());
}
Object acceptPreOrder(PreorderIterator i) {
i.addNode(r);
i.addNode(l);
return op;
}
Object acceptPostOrder(PostorderIterator i) {
i.markVisited(this);
if (i.visited(r)) {
return op;
} else {
i.addNode(this);
if (i.visited(l))
return r.acceptPostOrder(i);
else
return l.acceptPostOrder(i);
}
}
Object acceptBreadth(BreadthIterator i) {
if (i.visited(this)) {
i.addNode(l);
i.addNode(r);
return i.next();
} else {
i.markVisited(this);
i.addNode(this);
return op;
}
}
}
class PreorderIterator implements Iterator<Object> {
private Stack<AbsExpr> s = new Stack<AbsExpr>();
public boolean hasNext() {
return ! s.empty();
}
void addNode(AbsExpr e) {
s.push(e);
}
public Object next() {
if (hasNext())
return (s.pop()).acceptPreOrder(this);
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
class PostorderIterator implements Iterator<Object> {
private Set<AbsExpr> v = new HashSet<AbsExpr>();
private Stack<AbsExpr> s = new Stack<AbsExpr>();
public boolean hasNext() {
return ! s.empty();
}
boolean visited(AbsExpr e) {
return v.contains(e);
}
void markVisited(AbsExpr e) {
v.add(e);
}
void addNode(AbsExpr e) {
s.push(e);
}
public Object next() {
if (hasNext())
return (s.pop()).acceptPostOrder(this);
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
class BreadthIterator implements Iterator<Object> {
private Set<AbsExpr> v = new HashSet<AbsExpr>();
private List<AbsExpr> l = new ArrayList<AbsExpr>();
public boolean hasNext() {
return ! l.isEmpty();
}
boolean visited(AbsExpr e) {
return v.contains(e);
}
void markVisited(AbsExpr e) {
v.add(e);
}
void addNode(AbsExpr e) {
l.add(e);
}
public Object next() {
if (hasNext()) {
AbsExpr e = l.get(0);
l.remove(0);
return e.acceptBreadth(this);
}
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
|