-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVisitorQ.java
111 lines (81 loc) · 2.08 KB
/
VisitorQ.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
package org.blueocean;
public class VisitorQ {
public static interface Visitable{
abstract Object accept(TraversalVisitor v);
}
public static abstract class Tree implements Visitable {
public Object data;
public Tree left;
public Tree right;
}
public static class Node extends Tree {
@Override
public Void accept(TraversalVisitor v) {
v.visit(this);
return null;
}
}
public static interface TraversalVisitor {
void visit(Tree t);
}
public static class PreOrderVisitor implements TraversalVisitor{
@Override
public void visit(Tree t) {
System.out.println(t.data);
t.left.accept(this);
t.right.accept(this);
}
public void s() {return;}
}
///////////////////////////////////////////////////////////////
public static interface TreeExpression2{
abstract int accept(ExpVisitor v);
}
public static interface t extends Visitable, TreeExpression2 {
}
public static abstract class Tree2 {
public String data;
public TreeExpression2 left;
public TreeExpression2 right;
}
public static interface ExpVisitor {
int visit(DataNode n);
int visit(PlusNode n);
int visit(MinusNode n);
}
public static class DataNode extends Tree2 implements TreeExpression2 {
DataNode(String data) {this.data = data;}
@Override
public int accept(ExpVisitor v) {
return v.visit(this);
}
}
public static class PlusNode extends Tree2 implements TreeExpression2 {
PlusNode() {this.data = "+";}
@Override
public int accept(ExpVisitor v) {
return v.visit(this);
}
}
public static class MinusNode extends Tree2 implements TreeExpression2 {
MinusNode() {this.data = "-";}
@Override
public int accept(ExpVisitor v) {
return v.visit(this);
}
}
public static class OpsVisitor implements ExpVisitor{
@Override
public int visit(DataNode n) {
return Integer.valueOf(n.data);
}
@Override
public int visit(PlusNode n) {
return n.left.accept(this) + n.right.accept(this);
}
@Override
public int visit(MinusNode n) {
return n.left.accept(this) - n.right.accept(this);
}
}
}