-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
458 lines (403 loc) · 13.8 KB
/
BinarySearchTree.java
File metadata and controls
458 lines (403 loc) · 13.8 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import java.util.*;
public class BinarySearchTree<E> extends AbstractSet<E>
{
protected Entry<E> root;
protected int size;
/*
* Initializes this BinarySearchTree object to be empty, to contain
* only elements of type E, to be ordered by the Comparable interface,
* and to contain no duplicate elements.
*
*/
public BinarySearchTree()
{
root = null;
size = 0;
} // default constructor
/*
* Initializes this BinarySearchTree object to contain a shallow copy of
* a specified BinarySearchTree object.
* The worstTime(n) is O(n), where n is the number of elements in the
* specified BinarySearchTree object.
*
* @param otherTree - the specified BinarySearchTree object that this
* BinarySearchTree object will be assigned a shallow copy of.
*
*/
public BinarySearchTree (BinarySearchTree<? extends E> otherTree)
{
root = copy (otherTree.root, null);
size = otherTree.size;
} // copy constructor
protected Entry<E> copy (Entry<? extends E> p, Entry<E> parent)
{
if (p != null)
{
Entry<E> q = new Entry<E> (p.element, parent);
q.left = copy (p.left, q);
q.right = copy (p.right, q);
return q;
} // if
return null;
} // method copy
public boolean equals (Object obj)
{
if (!(obj instanceof BinarySearchTree))
return false;
return equals (root, ((BinarySearchTree<? extends E>)obj).root);
} // method 1-parameter equals
public boolean equals (Entry<E> p, Entry<? extends E> q)
{
if (p == null || q == null)
return p == q;
if (!p.element.equals (q.element))
return false;
if (equals (p.left, q.left) && equals (p.right, q.right) )
return true;
return false;
} // method 2-parameter equals
/**
* Returns the size of this BinarySearchTree object.
*
* @return the size of this BinarySearchTree object.
*
*/
public int size( )
{
return size;
} // method size()
/**
* Returns an iterator positioned at the smallest element in this
* BinarySearchTree object.
*
* @return an iterator positioned at the smallest element in this
* BinarySearchTree object.
*
*/
public Iterator<E> iterator()
{
return new TreeIterator();
} // method iterator
/**
* Determines if there is at least one element in this BinarySearchTree
* object that equals a specified element.
* The worstTime(n) is O(n) and averageTime(n) is O(log n).
*
* @param obj - the element sought in this BinarySearchTree object.
*
* @return true - if there is an element in this BinarySearchTree object
* the equals obj; otherwise, return false.
*
* @throws ClassCastException - if obj cannot be compared to the
* elements in this BinarySearchTree object.
* @throws NullPointerException - if obj is null.
*
*/
public boolean contains (Object obj)
{
return getEntry (obj) != null;
} // method contains
/**
* Ensures that this BinarySearchTree object contains a specified element.
* The worstTime(n) is O(n) and averageTime(n) is O(log n).
*
* @param element - the element whose presence is ensured in this
* BinarySearchTree object.
*
* @return true - if this BinarySearchTree object changed as a result of
* this method call (that is, if element was actually
* inserted); otherwise, return false.
*
* @throws ClassCastException - if element cannot be compared to the
* elements already in this BinarySearchTree object.
* @throws NullPointerException - if element is null.
*
*/
public boolean add (E element)
{
if (root == null)
{
if (element == null)
throw new NullPointerException();
root = new Entry<E> (element, null);
size++;
return true;
} // empty tree
else
{
Entry<E> temp = root;
int comp;
while (true)
{
comp = ((Comparable)element).compareTo (temp.element);
if (comp == 0)
return false;
if (comp < 0)
if (temp.left != null)
temp = temp.left;
else
{
temp.left = new Entry<E> (element, temp);
size++;
return true;
} // temp.left == null
else if (temp.right != null)
temp = temp.right;
else
{
temp.right = new Entry<E> (element, temp);
size++;
return true;
} // temp.right == null
} // while
} // root not null
} // method add
/**
* Ensures that this BinarySearchTree object does not contain a specified
* element.
* The worstTime(n) is O(n) and averageTime(n) is O(log n).
*
* @param obj - the object whose absence is ensured in this
* BinarySearchTree object.
*
* @return true - if this BinarySearchTree object changed as a result of
* this method call (that is, if obj was actually removed);
* otherwise, return false.
*
* @throws ClassCastException - if obj cannot be compared to the
* elements already in this BinarySearchTree object.
* @throws NullPointerException - if obj is null.
*
*/
public boolean remove (Object obj)
{
Entry<E> e = getEntry (obj);
if (e == null)
return false;
deleteEntry (e);
return true;
} // method remove
/**
* Finds the Entry object that houses a specified element, if there is
* such an Entry.
* The worstTime(n) is O(n), and averageTime(n) is O(log n).
*
* @param obj - the element whose Entry is sought.
*
* @return the Entry object that houses obj - if there is such an Entry;
* otherwise, return null.
*
* @throws ClassCastException - if obj is not comparable to the elements
* already in this BinarySearchTree object.
* @throws NullPointerException - if obj is null.
*
*/
protected Entry<E> getEntry (Object obj)
{
int comp;
if (obj == null)
throw new NullPointerException();
Entry<E> e = root;
while (e != null)
{
comp = ((Comparable)obj).compareTo (e.element);
if (comp == 0)
return e;
else if (comp < 0)
e = e.left;
else
e = e.right;
} // while
return null;
} // method getEntry
/**
* Deletes the element in a specified Entry object from this
* BinarySearchTree.
*
* @param p - the Entry object whose element is to be deleted from this
* BinarySearchTree object.
*
* @return the Entry object that was actually deleted from this
* BinarySearchTree object.
*
*/
protected Entry<E> deleteEntry (Entry<E> p)
{
size--;
// If p has two children, replace p's element with p's successor's
// element, then make p reference that successor.
if (p.left != null && p.right != null)
{
Entry<E> s = successor (p);
p.element = s.element;
p = s;
} // p had two children
// At this point, p has either no children or one child.
Entry<E> replacement;
if (p.left != null)
replacement = p.left;
else
replacement = p.right;
// If p has at least one child, link replacement to p.parent.
if (replacement != null)
{
replacement.parent = p.parent;
if (p.parent == null)
root = replacement;
else if (p == p.parent.left)
p.parent.left = replacement;
else
p.parent.right = replacement;
} // p has at least one child
else if (p.parent == null)
root = null;
else
{
if (p == p.parent.left)
p.parent.left = null;
else
p.parent.right = null;
} // p has a parent but no children
return p;
} // method deleteEntry
/**
* Finds the successor of a specified Entry object in this
* BinarySearchTree. The worstTime(n) is O(n) and averageTime(n) is
* constant.
*
* @param e - the Entry object whose successor is to be found.
*
* @return the successor of e, if e has a successor; otherwise, return null
*
*/
protected Entry<E> successor (Entry<E> e)
{
if (e == null)
return null;
else if (e.right != null)
{
// successor is leftmost Entry in right subtree of e
Entry<E> p = e.right;
while (p.left != null)
p = p.left;
return p;
} // e has a right child
else
{
// go up the tree to the left as far as possible, then go up
// to the right.
Entry<E> p = e.parent;
Entry<E> ch = e;
while (p != null && ch == p.right)
{
ch = p;
p = p.parent;
} // while
return p;
} // e has no right child
} // method successor
protected class TreeIterator implements Iterator<E>
{
protected Entry<E> lastReturned = null,
next;
/**
* Positions this TreeIterator to the smallest element, according to
* the Comparable interface, in the BinarySearchTree object.
* The worstTime(n) is O(n) and averageTime(n) is O(log n).
*
*/
protected TreeIterator()
{
next = root;
if (next != null)
while (next.left != null)
next = next.left;
} // default constructor
/**
* Determines if there are still some elements, in the
* BinarySearchTree object this TreeIterator object is iterating
* over, that have not been accessed by this TreeIterator object.
*
* @return true - if there are still some elements that have not
* been accessed by this TreeIterator object; otherwise, return
* false.
*
*/
public boolean hasNext()
{
return next != null;
} // method hasNext
/**
* Returns the element in the Entry this TreeIterator object was positioned at
* before this call, and advances this TreeIterator object.
* The worstTime(n) is O(n) and averageTime(n) is constant.
*
* @return the element this TreeIterator object was positioned at before this call.
*
* @throws NoSuchElementException - if this TreeIterator object was not
* positioned at an Entry before this call.
*
*/
public E next()
{
if (next == null)
throw new NoSuchElementException();
lastReturned = next;
next = successor (next);
return lastReturned.element;
} // method next
/**
* Removes the element returned by the most recent call to this TreeIterator
* object's next() method.
* The worstTime(n) is O(n) and averageTime(n) is constant.
*
* @throws IllegalStateException - if this TreeIterator's next() method was not
* called before this call, or if this TreeIterator's remove() method was
* called between the call to the next() method and this call.
*
*/
public void remove()
{
if (lastReturned == null)
throw new IllegalStateException();
if (lastReturned.left != null && lastReturned.right != null)
next = lastReturned;
deleteEntry(lastReturned);
lastReturned = null;
} // method remove
} // class TreeIterator
protected static class Entry<E>
{
protected E element;
protected Entry<E> left = null,
right = null,
parent;
/**
* Initializes this Entry object.
*
* This default constructor is defined for the sake of subclasses of
* the BinarySearchTree class.
*/
public Entry() { }
/**
* Initializes this Entry object from element and parent.
*
*/
public Entry (E element, Entry<E> parent)
{
this.element = element;
this.parent = parent;
} // constructor
} // class Entry
public int height()
{
return h(root);
}
protected int h (Entry<E> p)
{
if (p == null)
return -1;
else
return 1 + Math.max(h(p.left), h(p.right));
}
} // class BinarySearchTree