-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
400 lines (345 loc) · 9.91 KB
/
Copy pathBinarySearchTree.java
File metadata and controls
400 lines (345 loc) · 9.91 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
package edu.cofc.cs.csci230;
/**
*
* Binary search that does not allow two (or more) binary nodes
* in the search tree to have the same element value.
*
* @author CSCI 230: Data Structures and Algorithms Fall 2016
*
* @param <AnyType>
*/
public class BinarySearchTree <AnyType extends Comparable> {
// --------------------------------------
// instance variable
private BinaryNode<AnyType> root;
/**
* Constructor with one parameter that
* sets the root node of the BST.
*
* @param root
*/
public BinarySearchTree( AnyType rootElement ) throws NullBinaryNodeException {
if ( rootElement == null ) {
throw new NullBinaryNodeException();
}
this.root = new BinaryNode<AnyType>( rootElement ) ;
} // end constructor
/**
* If the BST does not have a root node, then the BST is empty.
*
* @return
*/
public boolean isEmpty() {
return ( root == null );
} // end isEmpty() method
/**
* Make the BST empty, i.e. a BST that has
* no nodes (including a root node).
*
*/
public void makeEmpty() {
root = null;
} // end makeEmpty() method
/**
* Method that adds a new node with the specified element
* value in the BST.
*
* This method has one parameter:
* 1) The element value to be added
*
* If the BST has an existing node with the same element
* value, throw an duplicate element exception.
*
* @param element
*/
public void add( AnyType element ) throws DuplicateElementException {
add(root,element);
/**
* ------------------------------------
* TODO: You complete the code.
*
*
*/
} // end add() method
/**
* Private recursive method that adds a new node (with the
* specified element value) in the BST.
*
* This method has two parameters:
* 1) The node visited while recursively walking the BST
* 2) The element value to be added
*
* If the BST has an existing node with the same element
* value, throw an duplicate element exception.
*
* @param node
* @param element
*/
private void add( BinaryNode<AnyType> node, AnyType element ) throws DuplicateElementException {
BinaryNode<AnyType> left = node.getLeft();
BinaryNode<AnyType> right = node.getRight();
//in the case that root node is null; set current node element to element
if(node.getElement() == null){
node.setElement(element);
}else if(element == node.getElement()){
throw new DuplicateElementException();
}else if(element.compareTo(node.getElement()) < 0){
if(left == null){
node.setLeft(new BinaryNode<AnyType>(element));
}else{
add(left,element);
}
}
else if(element.compareTo(node.getElement()) > 0){
if(right == null){
node.setRight(new BinaryNode<AnyType>(element));
}else{
add(right,element);
}
}
else{
throw new DuplicateElementException();
}
/**
* ------------------------------------
* TODO: You complete the code.
*
* Note: Your solution must use recursion
*
*/
} // end add() overloaded method
/**
* Method that determines if a node with the specified element value
* exists in the BST.
*
* This method has one parameter:
* 1) The element value that is being searched.
*
* If the BST is empty, throw an empty binary search tree
* exception.
*
* @param element
* @return
*/
public boolean hasElement( AnyType element ) throws EmptyBSTException {
if ( isEmpty() )
throw new EmptyBSTException();
return hasElement( root, element );
} // end hasElement() method
/**
* Private recursive method that determines if the element is
* currently stored in the BST.
*
* This method has two parameters:
* 1) The node visited while recursively walking the BST
* 2) The element value that is being searched.
*
* Hint: use the compareTo() method
*
* @param element
* @param node
* @return
*/
private boolean hasElement( BinaryNode<AnyType> node, AnyType element ) {
if(node.getElement() == element){
return true;
}
else{
return false;
}
/**
* ------------------------------------
* TODO: You complete the code.
*
* Note: Your solution must use recursion
*
*/
} // end hasElement() overloaded method
/**
* Find the node with the minimum element value in the BST.
*
* This method has no parameters.
*
* If the BST is empty, throw an empty binary search tree
* exception.
*
* @return
* @throws EmptyBSTException
*/
public AnyType findMin() throws EmptyBSTException {
if ( isEmpty() )
throw new EmptyBSTException();
return findMin( root ).getElement();
} // end findMin() method
/**
* Private recursive method that walks the BST searching
* for the node with the minimum element value.
*
* This method has one parameter:
* 1) The node visited while recursively walking the BST
*
* @param node
* @return
*/
private BinaryNode<AnyType> findMin( BinaryNode<AnyType> node ) {
if(node.getLeft() == null){
return node;
}else{
return findMin(node.getLeft());
}
/**
* ------------------------------------
* TODO: You complete the code.
*
* Note: Your solution must use recursion
*
*/
} // end findMin() method
/**
* Find the node with the maximum element value in the BST.
*
* This method has no parameters.
*
* If the BST is empty, throw an empty binary search tree
* exception.
*
* @return
* @throws EmptyBSTException
*/
public AnyType findMax() throws EmptyBSTException {
if ( isEmpty() )
throw new EmptyBSTException();
return findMax( root ).getElement();
} // end findMax() method
/**
* Private recursive method that walks the BST searching
* for the node with the maximum element value.
*
* This method has one parameter:
* 1) The node visited while recursively walking the BST
*
* @param node
* @return
*/
private BinaryNode<AnyType> findMax( BinaryNode<AnyType> node ) {
if(node.getRight() == null){
return node;
}else{
return findMax(node.getRight());
}
/**
* ------------------------------------
* TODO: You complete the code.
*
* Note: Your solution must use recursion
*
*/
} // end findMax() method
/**
*
* @param args
* @throws DuplicateElementException
* @throws EmptyBSTException
*/
public static void main( String[] args ) throws DuplicateElementException, EmptyBSTException {
try {
System.out.println("tree 1");
BinarySearchTree<Integer> tree = new BinarySearchTree(new Integer(23));
tree.add(new Integer(1337));
tree.add(new Integer(300));
tree.add(new Integer(4));
tree.add(new Integer(69));
tree.add(new Integer(3));
tree.add(new Integer(42));
System.out.println("min : " + tree.findMin());
System.out.println("max : " + tree.findMax());
} catch (NullBinaryNodeException e) {
// TODO Auto-generated catch block
System.out.println(e);
} catch (DuplicateElementException d){
System.out.println(d);
} catch (EmptyBSTException ee){
System.out.println(ee);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
try {
System.out.println("tree 2");
BinarySearchTree<Integer> tree = new BinarySearchTree(new Integer(23));
tree.add(new Integer(10));
tree.add(new Integer(5));
tree.add(new Integer(15));
tree.add(new Integer(0));
tree.add(new Integer(25));
tree.add(new Integer(3));
System.out.println("min : " + tree.findMin());
System.out.println("max : " + tree.findMax());
} catch (NullBinaryNodeException e) {
// TODO Auto-generated catch block
System.out.println(e);
} catch (DuplicateElementException d){
System.out.println(d);
} catch (EmptyBSTException ee){
System.out.println(ee);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
try {
System.out.println("tree 3");
BinarySearchTree<Integer> tree = new BinarySearchTree(new Integer(23));
tree.add(new Integer(2));
tree.add(new Integer(1));
tree.add(new Integer(3));
tree.add(new Integer(4));
tree.add(new Integer(3));
tree.add(new Integer(3));
System.out.println("min : " + tree.findMin());
System.out.println("max : " + tree.findMax());
} catch (NullBinaryNodeException e) {
// TODO Auto-generated catch block
System.out.println(e);
} catch (DuplicateElementException d){
System.out.println(d);
} catch (EmptyBSTException ee){
System.out.println(ee);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
try {
System.out.println("tree 4");
BinarySearchTree<Integer> tree = new BinarySearchTree(new Integer(23));
tree.add(new Integer(1));
tree.add(new Integer(2));
tree.add(new Integer(3));
tree.add(new Integer(4));
tree.add(new Integer(5));
tree.add(new Integer(6));
System.out.println("min : " + tree.findMin());
System.out.println("max : " + tree.findMax());
} catch (NullBinaryNodeException e) {
// TODO Auto-generated catch block
System.out.println(e);
} catch (DuplicateElementException d){
System.out.println(d);
} catch (EmptyBSTException ee){
System.out.println(ee);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
try {
System.out.println("tree 5");
BinarySearchTree<Integer> tree = new BinarySearchTree(new Integer(23));
tree.add(new Integer(0));
tree.add(new Integer(5));
tree.add(new Integer(10));
tree.add(new Integer(50));
tree.add(new Integer(25));
tree.add(new Integer(3));
System.out.println("min : " + tree.findMin());
System.out.println("max : " + tree.findMax());
} catch (NullBinaryNodeException e) {
// TODO Auto-generated catch block
System.out.println(e);
} catch (DuplicateElementException d){
System.out.println(d);
} catch (EmptyBSTException ee){
System.out.println(ee);
}
} // end main method
} // end BinarySearchTree class