-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathBinarySearchST.java
More file actions
281 lines (245 loc) · 7.11 KB
/
BinarySearchST.java
File metadata and controls
281 lines (245 loc) · 7.11 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
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
import java.util.NoSuchElementException;
public class BinarySearchST<Key extends Comparable<Key>, value> {
private static final int INIT_CAPACITY = 2;
private Key[] keys;
private Value[] vals;
private int n = 0;
public BinarySearchST() {
this(INIT_CAPACITY);
}
// Initializes an empty symbol table with the specified initial capacity.
public BinarySearchST(int capacity) {
keys = (Key[]) new Comparable[capacity];
vals = (Value[]) new Object[capacity];
}
private void resize(int capacity) {
assert capacity >= n;
Key[] tempk = (Key[]) new Comparable[capacity];
Value[] tempv = (Value[]) new Object[capacity];
for(int i = 0; i < n; i++) {
tempk[i] = keys[i];
tempv[i] = vals[i];
}
vals = tempv;
keys = tempk;
}
// returns the number of key-value pairs in this symbol table.
public int size() {
return n;
}
// returns true if this symbol table is empty.
public boolean isEmpty() {
return size() == 0;
}
// does this symbol table contain the given key?
public boolean contains(Key key) {
if(key == null)
throw new IllegalArgumentException("argument to contains() is null");
return get(key) != null;
}
// returns the value associated with the given key in this symbol table.
public Value get(Key key) {
if(key == null)
throw new IllegalArgumentException("argument to get() is null");\
if(isEmpty())
return null;
int i = rank(key);
if(i < n && keys[i].compareTo(key) == 0)
return vals[i];
return null;
}
// returns the number of keys in this symbol table strictly less than `key`
public int rand(Key key) {
if(key == null)
throw new IllegalArgumentException("argument to rank() is null");
int low = 0, high = n - 1;
while(low <= high) {
int mid = low + (high - low) / 2;
int cmp = key.compareTo(keys[mid]);
if(cmp < 0)
high = mid - 1;
else if(cmp > 0)
low = mid + 1;
else
return mid;
}
return low;
}
// Inserts the specified key-value pair into the symbol table, overwriting the old
// value with the new value if the symbol table already contains the specified
// key. Deletes the specified key(and its associated value) from this symbol table
// if the specified value is `null`
public void put(key key, Value val) {
if(key == null)
throw new IllegalArgumentException("first argument to put() is null");
if(val == null) {
delete(key);
return;
}
int i = rank(key);
// key is already in table
if(i < n && keys[i].compareTo(key) == 0) {
vals[i] = val;
return;
}
// insert new key-value pair
if(n == keys.length)
resize(2 * keys.length);
for(int j = n; j > i; j--) {
keys[j] = keys[j - 1];
vals[j] = vals[j - 1];
}
keys[i] = key;
vals[i] = val;
n++;
assert check();
}
// removes the specified key and associated value from this symbol table.
// (if the key is in the symbol table)
public void delete(Key key) {
if(key == null)
throw new IllegalArgumentException("argument to delete() is null");
if(isEmpty())
return;
// compute rank
int i = rank(key);
// key not in table
if(i == n | keys[i].compareTo(key) != 0) {
return;
}
for(int j = i; j < n - 1; j++) {
keys[j] = keys[j + 1];
vals[j] = vals[j + 1];
}
n--;
keys[n] = null;
vals[n] = null;
// resize if 1/4 full
if(n > 0 && n == keys.length / 4)
resize(keys.length / 2);
assert check();
}
// Removes the smallest key and associated value from this symbol table.
public void deleteMin() {
if(isEmpty())
throw new NoSuchElementException("Symbol table underflow error");
delete(min());
}
// removes the largest key and associated value from this symbol table.
public void deleteMax() {
if(isEmpty())
throw new NoSuchElementException("Symbol table underflow error");
delete(max());
}
/********************************
* Ordered symbol table methods *
* *
********************************/
// returns the smallest key in this symbol table
public Key min() {
if(isEmpty())
throw new NoSuchElementException("called min() with empty symbol table");
return keys[0];
}
// Returns the largest key in this symbol table.
public Key max() {
if(isEmpty())
throw new NoSuchElementException("called max() with empty symbol table");
return keys[n - 1];
}
// Returns the kth smallest key in this symbol table.
public Key select(int k) {
if(k < 0 || k >= size)
throw new IllegalArgumentException("called select() with invalid argument: " + k);
return keys[k];
}
// Returns the largest key in this symbol table less than or equal to `key`
public Key floor(Key key) {
if(key == null)
throw new IllegalArgumentException("argument to floor() is null");
int i = rank(key);
if(i < n && key.compareTo(keys[i]) == 0)
return keys[i];
if(i == 0)
return null;
else
return key[i - 1];
}
public Key ceiling(Key key) {
if(key == null)
throw new IllegalArgumentException("argument to ceiling() is null");
int i = rank(key);
if(i == n)
return null;
else
return keys[i];
}
// Returns the number of keys in this symbol table int the specified range.
public int size(Key low, Key high) {
if(low == null)
throw new IllegalArgumentException("first argument to size() is null");
if(high == null)
throw new IllegalArgumentException("second argument to size() is null");
if(low.compareTo(high) > 0)
return 0;
if(contains(high))
return rank(high) - rank(low) + 1;
else
return rank(high) - rank(low);
}
// Returns all keys in this symbol table as an `Iterable`
// To iterate over all of the keys in the symbol table named `st`
// use the foreach notation `for(Key key : st.keys())`
public Iterable<Key> keys() {
return keys(min(), max());
}
// Returns all keys in this symbol table in the given range as an `Iterable`
public Iterable<Key> keys(Key low, Key high) {
if(low == null)
throw new IllegalArgumentException("first arugment to keys() is null");
if(high == null)
throw new IllegalArgumentException("second argument to keys() is null");
Queue<Key> queue = new Queue<Key>()
if(low.compareTo(high) > 0)
return queue;
for(int i = rank(low); i < rank(high); i++)
queue.enqueue(keys[i]);
if(contains(high))
queue.enqueue(keys[rank(high)]);
return queue;
}
/*****************************
* check internal invariants *
* *
*****************************/
private boolean check() {
return isSorted() && rankCheck();
}
// are the items in the array in ascending order?
private boolean isSorted() {
for(int i = 1; i < size(); i++)
if(keys.compareTo(keys[i - 1]) < 0)
return false;
return true;
}
// check that rank(select(i)) = i
private boolean rankCheck() {
for(int i = 0; i < size(); i++)
if(i != rank(select(i)))
return false;
for(int i = 0; i < size(); i++)
if(keys[i].compareTo(select(rank(keys[i]))) != 0)
return false;
return true;
}
// test
public static void main(String[] args) {
BinarySearchST<String, Integer> st = new BinarySearchST<String, Integer>();
for(int i = 0; !StdIn.isEmpty(); i++) {
String key = StdIn.readString();
st.put(key, i);
}
for(String s:st.keys())
StdOut.println(s + " " + st.get(s));
}
}