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
|
public class BoggleHybridTST
{
private static final int R = 26;
private static final int OFFSET = 'A';
private static Node emptyNode = null;
private Node[] roots = new Node[R * R];
public class Node
{
private boolean isWord;
private char c;
private Node left, mid, right;
public boolean isWord() { return isWord; }
public String toString() { return "'"+c+"' "+isWord; }
}
public Node emptyNode() {
if (emptyNode == null) {
emptyNode = new Node();
}
return emptyNode;
}
public void put(String word)
{
int l = word.length();
if (l < 3) return;
int idx = ((word.charAt(0) - OFFSET) * R) + (word.charAt(1) - OFFSET);
roots[idx] = put(roots[idx], word, 2);
}
private Node put(Node n, String word, int d)
{
char c = word.charAt(d);
Node x = n;
if (n == null) {
x = new Node();
x.c = c;
}
char ref = x.c;
if (c < ref) {
x.left = put(x.left, word, d);
} else if (c == ref) {
if (word.length() == (d + 1)) {
x.isWord = true;
} else {
x.mid = put(x.mid, word, (d + 1));
}
} else {
x.right = put(x.right, word, d);
}
return x;
}
public boolean contains(String word)
{
return (get(word) != null);
}
private Node get(String word)
{
if (word.length() < 3) return null;
int idx = ((word.charAt(0) - OFFSET) * R) + (word.charAt(1) - OFFSET);
return get(roots[idx], word, 2);
}
private Node get(Node n, String word, int d)
{
if (n == null) return null;
char c = word.charAt(d);
char ref = n.c;
if (c < ref) {
return get(n.left, word, d);
} else if (c == ref) {
if (word.length() == (d + 1)) {
if (n.isWord) return n;
else return null;
}
return get(n.mid, word, (d + 1));
} else {
return get(n.right, word, d);
}
}
private Node get(Node n, char c)
{
if (n == null) return null;
char ref = n.c;
if (c < ref) {
return get(n.left, c);
} else if (c == ref) {
return n;
} else {
return get(n.right, c);
}
}
public Node filter(char c, Node n, StringBuilder s, int l)
{
if (n == null) return null;
if (l > 2) {
return get(n.mid, c);
} else if (l == 2) {
int idx = ((s.charAt(0) - OFFSET) * R) + (s.charAt(1) - OFFSET);
Node root = roots[idx];
if (root == null) return null;
return get(root, c);
} else {
return n;
}
}
public Node filter(char c, Node n, String s, int l)
{
if (n == null) return null;
if (l > 2) {
return get(n.mid, c);
} else if (l == 2) {
int idx = ((s.charAt(0) - OFFSET) * R) + (s.charAt(1) - OFFSET);
Node root = roots[idx];
if (root == null) return null;
return get(root, c);
} else {
return n;
}
}
public static void check(BoggleHybridTST tst, String word, boolean expected)
{
String status = "ok";
if (tst.contains(word) != expected)
status = "ERROR";
System.out.println("check "+word+" : "+status);
}
// test client
public static void main(String[] args)
{
BoggleHybridTST tst = new BoggleHybridTST();
In in = new In(args[0]);
String[] dictionary = in.readAllStrings();
for (String word : dictionary)
tst.put(word);
check(tst, "ABC", true);
check(tst, "BITE", false);
check(tst, "BITEX", false);
String word = "QUESTIONACCIA";
check(tst, word, true);
word += "X";
StringBuilder s = new StringBuilder();
Node n = tst.emptyNode();
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
n = tst.filter(c, n, s, i);
s.append(c);
System.out.println(s+" "+n);
}
}
}
|