-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbolTable.java
349 lines (310 loc) · 6.64 KB
/
SymbolTable.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package pzhao.com;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;
/*
* 基于无序链表的顺序查找
*
*/
class SequentialSearchST<Key, Value> implements Iterable<Key> {
private class Node {
private Key key;
Value value;
Node next;
public Node(Key key, Value value, Node next) {
this.key = key;
this.value = value;
this.next = next;
}
}
private Node first;
private int N;
public int size(){
return N;
}
public void put(Key key, Value value) {
for (Node xNode = first; xNode != null; xNode = xNode.next) {
if (key.equals(xNode.key)) {
xNode.value = value;
return;
}
}
first = new Node(key, value, first);
N++;
}
public Value get(Key key) {
for (Node xNode = first; xNode != null; xNode = xNode.next) {
if (key.equals(xNode.key))
return xNode.value;
}
return null;
}
public void delete_1(Key key){
Node xNode=first;
Node previousNode=xNode;
while(xNode!=null&&!key.equals(xNode.key)){
previousNode=xNode;
xNode=xNode.next;
}
if(xNode==null) return;
else {
if(xNode==first)
first=first.next;
else {
previousNode.next=xNode.next;
}
}
}
public void delete(Key key){
first=delete(first, key);
}
private Node delete(Node x,Key key){
if(x==null)return null;
Node previous =x;
while(!x.key.equals(key)){
if(x.next==null)return null;
else{
previous=x;
x=x.next;
}
}
N--;
if(x==first)return first.next;
else {
previous.next=x.next;
}
return first;
}
public boolean contains(Key key) {
return get(key) != null;
}
@Override
public Iterator<Key> iterator() {
// TODO Auto-generated method stub
return new Iterator<Key>() {
Node first1 = first;
Node tmpNode;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return first1 != null;
}
@Override
public Key next() {
// TODO Auto-generated method stub
tmpNode = first1;
first1 = first1.next;
return tmpNode.key;
}
};
}
}
/*
* 基于有序数组的二分查找
*
*/
class BinarySearchST<Key extends Comparable<Key>, Value> implements
Iterable<Key> {
private Key[] keys;
private Value[] values;
private int N;
public BinarySearchST() {
this(1);
}
public BinarySearchST(int n) {
keys = (Key[]) new Comparable[n];
values = (Value[]) new Object[n];
}
public int size() {
return N;
}
public boolean isEmpty() {
return size() == 0;
}
private void reSize(int n) {
Key[] tmpKeys = (Key[]) new Comparable[n];
Value[] tmpValues = (Value[]) new Object[n];
for (int i = 0; i < N; i++) {
tmpKeys[i] = keys[i];
tmpValues[i] = values[i];
}
keys = tmpKeys;
values = tmpValues;
}
public void put(Key key, Value value) {
if (value == null) {
delete(key);
return;
}
int i = rank(key);
if (i < N && key.equals(keys[i])) {
values[i] = value;
return;
}
if (N == keys.length - 1)
reSize(keys.length * 2);
for (int j = N; j > i; j--) {
keys[j] = keys[j - 1];
values[j] = values[j - 1];
}
keys[i] = key;
values[i] = value;
N++;
}
public Value get(Key key) {
if (isEmpty())
return null;
int i = rank(key);
if (i < N && keys[i].equals(key))
return values[i];
return null;
}
public void delete(Key key) {
if (isEmpty())
return;
int i = rank(key);
if (!keys[i].equals(key))
return;
for (int j = i; j < N - 1; j++) {
keys[j] = keys[j + 1];
values[j] = values[j + 1];
}
N--;
keys[N] = null;
values[N] = null;
if (N > 0 && N < keys.length / 4)
reSize(keys.length / 2);
}
public boolean contains(Key key) {
if (isEmpty())
return false;
int i = rank(key);
if (i < N && keys[i].equals(key))
return true;
return false;
}
public int rank(Key key) {
int lo = 0, hi = N - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (key.compareTo(keys[mid]) < 0)
hi = mid - 1;
else if (key.compareTo(keys[mid]) > 0)
lo = mid + 1;
else
return mid;
}
return lo;
}
public Key min() {
if (isEmpty())
return null;
return keys[0];
}
public Key max() {
if (isEmpty())
return null;
return keys[N - 1];
}
public Key select(int k) {
if (k < 0 || k >= N)
return null;
return keys[k];
}
public Key ceilling(Key key) {
int i = rank(key);
if (i == N)
return null;
return keys[i];
}
public Key floor(Key key) {
int i = rank(key);
if (i < N && key.equals(keys[i]))
return keys[i - 1];
if (i == 0)
return null;
return keys[i];
}
@Override
public Iterator<Key> iterator() {
// TODO Auto-generated method stub
return new Iterator<Key>() {
int i = 0;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return i < N;
}
@Override
public Key next() {
// TODO Auto-generated method stub
return keys[i++];
}
};
}
}
public class SymbolTable {
@SuppressWarnings("resource")
public static void main(String[] argus) throws IOException {
// int MinLen = 0;
// SequentialSearchST<String, Integer> st = new SequentialSearchST<>();
Scanner scanner = new Scanner(new File("tinyTale.txt"));
// while (scanner.hasNext()) {
// String word = scanner.next();
// if (word.length() < MinLen)
// continue;
// if (!st.contains(word))
// st.put(word, 1);
// else {
// st.put(word, st.get(word) + 1);
// }
// }
// String max = "";
// st.put(max, 0);
// Iterator<String> iterator = st.iterator();
// while (iterator.hasNext()) {
// String wordString = iterator.next();
// int i = st.get(wordString);
// if (i > st.get(max)) {
// max = wordString;
// st.put(max, i);
// }
// }
//
// System.out.print(max + " " + st.get(max));
// System.out.println();
BinarySearchST<String, Integer> binarySearchST = new BinarySearchST<>();
while (scanner.hasNext()) {
String word = scanner.next();
if (binarySearchST.contains(word))
binarySearchST.put(word, binarySearchST.get(word) + 1);
else {
binarySearchST.put(word, 1);
}
}
String max="";
binarySearchST.put(max, 0);
Iterator<String> iterator=binarySearchST.iterator();
while(iterator.hasNext()){
String wordString = iterator.next();
if(binarySearchST.get(wordString)>binarySearchST.get(max))
max=wordString;
}
System.out.print("max: "+max+": "+binarySearchST.get(max));
System.out.println();
SequentialSearchST<String, Integer> st = new SequentialSearchST<>();
for(int i=0;i<5;i++){
st.put("a"+i, i);
}
// st.delete("a0");
st.delete_1("a2");
Iterator<String> iterator1 = st.iterator();
while(iterator1.hasNext())
System.out.print(iterator1.next()+" ");
System.out.println();
for(String a:st){
System.out.print(a+" ");
}
}
}