-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTree.java
More file actions
518 lines (455 loc) · 13.2 KB
/
Copy pathBSTree.java
File metadata and controls
518 lines (455 loc) · 13.2 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//Farica Zhuang
import java.util.LinkedList;
import java.util.*;
public class BSTree<T extends Comparable<? super T>>{
private int nelems;
private BSTNode root;
private BSTNode currNode;
private boolean found;
private BSTNode foundKey;
protected class BSTNode{
T key;
LinkedList<T> relatedInfo;
BSTNode left;
BSTNode right;
/**
* A constructor that initializes the BSTNode instance variables
* @param left is the left child
* @param right is the right child
* @param relatedInfo is the list of related informations
* @param key is the element of this node
*/
public BSTNode(BSTNode left, BSTNode right, LinkedList<T> relatedInfo, T key) {
this.left = left;
this.right = right;
//instantiate the list
setRelatedInfo(relatedInfo);
this.key = key;
}
/**
* A constructor that initializes the BSTNode variables
* @param left is the left node
* @param right is the right node
* @param key is the element of this node
*/
public BSTNode(BSTNode left, BSTNode right, T key) {
this.left = left;
this.right = right;
this.key = key;
this.relatedInfo = new LinkedList<T>();
}
/**
* Getter method for the key
* @return the key
*/
public T getKey() {
return this.key;
}
/**
* Getter method for the left child of the node
* @return the left node
*/
public BSTNode getLeft() {
return this.left;
}
/**
* Getter method for the right node
* @return the right node
*/
public BSTNode getRight() {
return this.right;
}
/**
* Getter method for the LinkedList of the node
* @return the LinkedList
*/
public LinkedList<T> getRelatedInfo() {
return this.relatedInfo;
}
/**
* Setter method for the left pointer of the node
* @param the new child that the left pointer points to
*/
public void setLeft(BSTNode newLeft) {
this.left = newLeft;
}
/**
* Setter method for the right pointer of the node
* @param the new child that the right pointer points to
*/
public void setRight(BSTNode newRight) {
this.right = newRight;
}
/**
* Setter method for the LinkedList of the node
* @param the new LinkedList of the node
*/
public void setRelatedInfo(LinkedList<T> newInfo) {
this.relatedInfo = newInfo;
}
/**
* Method that appends new info to the end of the existing LinkedList of the
* node
* @param the new info to be added to the list
*/
public void addNewInfo(T info) {
if(info != null) {
getRelatedInfo().addLast(info);
}
}
/**
* Method that removes 'info' from the LinkedList of the node and return true
* If the LinkedList does not contain the value 'info', return false
* @param info to be removed from the list
* @return false if info is not in the list, true if info has been successfully
* removed from the list
*/
public boolean removeInfo(T info) {
return relatedInfo.remove(info);
}
}
//BSTree methods here
/**
* A 0-arg constructor that initializes root to null and nelems to 0
*/
public BSTree() {
this.root = null;
this.nelems = 0;
this.currNode = null;
this.found = false;
this.foundKey = null;
}
/**
* A getter method for the root, returns null if empty
* @return the roof of the BSTree
*/
public BSTNode getRoot() {
//if the tree is not empty, return the root
if(this.nelems > 0) {
return this.root;
}
//if the tree is empty, return null
return null;
}
/**
* Getter for the size of the BSTree
* @return nelems
*/
public int getSize() {
return this.nelems;
}
/**
* Method that inserts a key into the BST by inserting a node with the key and
* an empty LinkedList into the tree
* @param the key to be inserted
* @throws NullPointerException if key is null
*/
public void insert(T key) {
//if key is null, throw an excpetion
if(key == null) {
throw new NullPointerException();
}
//if the tree is empty, the node will be the root node
if(getSize() == 0) {
//create an empty linkedlist
LinkedList<T> list = new LinkedList<T>();
//create the node with the key and the empty linkedlist
BSTNode node = new BSTNode(null, null, list, key);
this.root = node;
this.currNode = root;
//increment nelems
this.nelems += 1;
}
//otherwise, insert the node into the tree by finding its place recursively
else {
//if currNode is smaller than to key, go to the right node
if(this.currNode.getKey().compareTo(key) < 0) {
//if the right node is not null, set the currNode to the right node and
//do recursion
if(this.currNode.getRight() != null) {
currNode = currNode.getRight();
insert(key);
}
//if this node has not right node, then set the key to be the right
//node and we are done
else {
//create an empty linkedlist
LinkedList<T> list = new LinkedList<T>();
//create the node with the key and the empty linkedlist
BSTNode node = new BSTNode(null, null, list, key);
this.currNode.setRight(node);
//increment nelem
this.nelems += 1;
//reset currNode to root
this.currNode = root;
}
}
//else if currNode is greater than key, go to the left node
else {
//if left node is not null, set currNode to this left node and do
//recursion
if(this.currNode.getLeft() != null) {
currNode = currNode.getLeft();
insert(key);
}
//else if the left node is null, set key as the left node and we are done
else {
//create an empty linkedlist
LinkedList<T> list = new LinkedList<T>();
//create the node with the key and the empty LinkedList
BSTNode node = new BSTNode(null, null, list, key);
this.currNode.setLeft(node);
//increment number of elements
this.nelems += 1;
//reset the currNode to the root
this.currNode = root;
}
}
}
}
/**
* This method finds the key in the tree
* @param the key to be searched
* @return true if the key is found and false otherwise
*/
public boolean findKey(T key) {
//if tree is empty, return false because there is no node
if(getSize() == 0) {
this.found = false;
}
//if currNode is not equal to key, check
else if(currNode.getKey().compareTo(key) != 0) {
//if currNode is smaller than key, check the left node
if(currNode.getKey().compareTo(key) < 0) {
//if left node is not null, set currNode to its left node and do
//recursion
if(currNode.getRight() != null) {
currNode = currNode.getRight();
findKey(key);
}
//if the right node is null, then key is not found so return false
else {
//reset currNode to root
this.currNode = root;
this.found = false;
}
}
//if currNode is greater than key, check the right node
else {
if(currNode.getLeft() != null) {
currNode = currNode.getLeft();
findKey(key);
}
//else if the right node is null, then key is not found so return false
else {
//reset currNode to root
this.currNode = root;
this.found = false;;
}
}
}
//else, currNode is equal to key, we have found the key so return true
else {
//set foundKey to be this
this.foundKey = currNode;
//reset currNode to root
this.currNode = root;
this.found = true;
}
return this.found;
}
/**
* This method inserts 'info' into the LinkedList of the node whose key is 'key'
* @param key is the node whose key is this
* @param info is the info to be inserted in the key specified
* @throws NullPointerException if 'key' or 'info' is null
* @throws IllegalArgumentExcpetion if 'key' if not found
*/
public void insertInformation(T key, T info) {
//check for exceptions
//throw exception if key or info is null
if(key == null || info == null) {
throw new NullPointerException();
}
//throw exception if key is not found
if(!findKey(key)) {
throw new IllegalArgumentException();
}
//since we have already called findKey() method and it returned true, we can
//access foundKey whose key is 'key' and insert the info into its list
foundKey.addNewInfo(info);
}
/**
* This method returns the LinkedLIst of the node with key value 'key'
* @param key is the key whose LinkedList the method will return
* @return the LinkedList of the node with key value 'key'
* @throw NullPointerException if key is null
* @throw IllegalArgumentException if 'key' is not found in the BST
*/
public LinkedList<T> findMoreInformation(T key) {
//if key is not found, throw an exception
if(!findKey(key)) {
throw new IllegalArgumentException();
}
//if key is null, throw an exception
if(key == null) {
throw new NullPointerException();
}
//since we have called findKey(), the node foundKey is already set to key
//now return foundkey's LinkedList
return this.foundKey.getRelatedInfo();
}
/**
* This method calculates the height of the tree, the length of the longest
* downward path to a leaf from the root
* @return the longest downward path to a leaf from the root
*/
public int findHeight() {
return findHeight(getRoot());
}
/**
* A private helper method to be recursively called to calculate the height of
* the tree
* @param the starting node
* @return the height of the tree
*/
private int findHeight(BSTNode node) {
if (node == null) {
return -1;
}
int leftHeight = findHeight(node.getLeft());
int rightHeight = findHeight(node.getRight());
if (leftHeight > rightHeight) {
return leftHeight + 1;
}
else {
return rightHeight + 1;
}
}
/**
* This method counts the number of leaf nodes in the tree
* @return the number of leaf nodes in the tree
*/
public int leafCount() {
return leafCount(getRoot());
}
/**
* private helper method that counts the number of leaf nodes in the tree recursively
* @return the number of leaf node in the tree
*/
private int leafCount(BSTNode node) {
if( node == null ) {
return 0;
}
if( node.getLeft() == null && node.getRight() == null ) {
return 1;
}
else {
return leafCount(node.getLeft()) + leafCount(node.getRight());
}
}
/**
* This method creates a new iterator and returns it
* @return the BSTree_Iterator
*/
public Iterator<T> iterator() {
return new BSTree_Iterator();
}
/**
* Extra credit method that returns the intersection of two BSTree
* @param iter1 is one of the iterators of the two trees to be checks
* @param iter2 is the second iterator of the second tree to be checked
* @return the arraylist containing the intersection of two BSTrees
*
*/
public ArrayList<T> intersection(Iterator<T> iter1, Iterator<T> iter2) {
ArrayList<T> list1 = new ArrayList<T>();
//populate list1 with elements from iter1
while(iter1.hasNext()) {
list1.add(iter1.next());
}
ArrayList<T> list2 = new ArrayList<T>();
//populate list2 with elements from iter1
while(iter2.hasNext()) {
list2.add(iter2.next());
}
//retain the intersection
list1.retainAll(list2);
//return the list containing the intersection
return list1;
}
/**
* Extra credit method that returns the number of nodes at a given level
* @param the level to be checked
* @return the number of nodes at a given leve
*
*/
public int levelCount(int level) {
//if level = 0, and tree is not emtpy, return 1 because this is the root
if(level == 0 && getSize() > 0) {
return 1;
}
//if level is out of bounds, return -1
else if(level >= findHeight()) {
return -1;
}
return 0;
}
//BSTree_Iterator here
public class BSTree_Iterator implements Iterator<T> {
Stack<BSTNode> stack;
BSTNode thisNode;
/**
* Constructor that initializes the stack with the leftPath of the root
*/
public BSTree_Iterator() {
stack = new Stack<BSTNode>();
//set currNode to root
thisNode = getRoot();
//initializes the stack with the leftPath of the root
if(thisNode != null) {
stack.push(thisNode);
while(thisNode.getLeft() != null) {
thisNode = thisNode.getLeft();
stack.push(thisNode);
}
}
}
/**
* Method that checks if stack is not empty
* @return false if the Stack is empty and true otherwise
*/
public boolean hasNext() {
return !stack.isEmpty();
}
/**
* This method returns the next item in the BST
* @return the next item in the BST
* @throws NoSuchElementException if there is no next item
*/
public T next() {
//if there is no next item, throw an exception
if(!hasNext()) {
throw new NoSuchElementException();
}
BSTNode toReturn = stack.pop();
thisNode = toReturn;
//update the stack
if(thisNode.getRight() != null) {
thisNode = thisNode.getRight();
stack.push(thisNode);
while(thisNode.getLeft() != null) {
thisNode = thisNode.getLeft();
stack.push(thisNode);
}
}
return toReturn.getKey();
}
/**
* This method returns a new BSTree_Iterator
*/
public BSTree_Iterator iterator() {
return new BSTree_Iterator();
}
}
}