-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyBinarySearchTreeImpl.java
More file actions
518 lines (450 loc) · 19.4 KB
/
myBinarySearchTreeImpl.java
File metadata and controls
518 lines (450 loc) · 19.4 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
public class myBinarySearchTreeImpl<T1 extends Comparable<? super T1>, T2> implements myBinarySearchTree<T1, T2> {
//--------------------------------------------------
// Attributes
//--------------------------------------------------
protected myBinarySearchNode<T1, T2> root;
//-------------------------------------------------------------------
// 1. Basic Operation --> Create a non-empty myBinarySearchTree from a myBinarySearchNode: create_from_binary_search_node()
//-------------------------------------------------------------------
//public myBinarySearchTree create_from_element_and_trees(myBinarySearchNode<T> n); --> Java does not support constructors in interfaces
public myBinarySearchTreeImpl(myBinarySearchNode<T1, T2> node) throws myException{
if (node == null)
this.root = null;
else
this.root = node;
//2. However, there is a detail. Now we have to check that the tree we are constructing is ordered as well.
if (this.root != null){
//2.1. If the left subtree is not empty
if (this.my_left_tree().my_is_empty() == false){
//2.1.1. We get the maximum element from the left subtree
myBinarySearchNode<T1, T2> newNode = this.my_left_tree().my_maximum();
//2.1.2. We compare the root element to it, to ensure it is bigger
if ((newNode.getKey()).compareTo(this.my_root().getKey()) >= 0)
throw new myException("The tree being constructed is not ordered, as the root is smaller than the elements of its left subtree");
}
//2.2. If the right subtree is not empty
if (this.my_right_tree().my_is_empty() == false){
//2.2.1. We get the minimum element from the right subtree
myBinarySearchNode<T1, T2> newNode = this.my_right_tree().my_minimum();
//2.2.2. We compare the root element to it, to ensure it is smaller
if ((newNode.getKey()).compareTo(this.my_root().getKey()) <= 0)
throw new myException("The tree being constructed is not ordered, as the root is bigger than the elements of its right subtree");
}
}
}
//-------------------------------------------------------------------
// 2. Basic Operation --> Check if myBinarySearchTree is empty: my_is_empty
//-------------------------------------------------------------------
public boolean my_is_empty(){
return (this.root == null);
}
//-------------------------------------------------------------------
// 3. Basic Operation --> Get root of myBinarySearchTree: my_root
//-------------------------------------------------------------------
public myBinarySearchNode<T1, T2> my_root() {
return this.root;
}
//-------------------------------------------------------------------
// 4. Basic Operation --> Get left tree of myBinarySearchTree: my_left_tree
//-------------------------------------------------------------------
public myBinarySearchTree<T1, T2> my_left_tree() throws myException{
if (this.root != null){
myBinarySearchTree<T1, T2> res = new myBinarySearchTreeImpl<T1, T2>(this.root.getLeft());
return res;
}
else
throw new myException("The tree is empty, so there is no left subtree");
}
//-------------------------------------------------------------------
// 5. Basic Operation --> Get left tree of myBinarySearchTree: my_right_tree
//-------------------------------------------------------------------
public myBinarySearchTree<T1, T2> my_right_tree() throws myException{
if (this.root != null){
myBinarySearchTree<T1, T2> res = new myBinarySearchTreeImpl<T1, T2>(this.root.getRight());
return res;
}
else
throw new myException("The tree is empty, so there is no left subtree");
}
//-------------------------------------------------------------------
// 6. Basic Operation --> Find element at myBinarySearchTree: my_find
//-------------------------------------------------------------------
public myBinarySearchNode<T1,T2> my_find(T1 key){
myBinarySearchNode<T1, T2> res;
//1.1 If the tree is empty, then the element is not found
if (this.my_is_empty() == true)
res = null;
//1.2. If it is not empty
else{
//1.2.1. We compare the element to the root of the tree
int k = key.compareTo(this.my_root().getKey());
//Case A: If they are equal, is because we have found the node
if (k == 0)
res = this.my_root();
//Otherwise, we have to keep looking
else
//Case B: If the element is smaller than the root, we keep looking in the left subtree.
if (k < 0)
res = this.my_left_tree().my_find(key);
//Case C: If the element is bigger than the root, we keep looking in the right subtree.
else
res = this.my_right_tree().my_find(key);
}
//2. We return res
return res;
}
//-------------------------------------------------------------------
// 7. Basic Operation --> Insert element at myBinarySearchTree: my_insert
//-------------------------------------------------------------------
public myBinarySearchTree<T1, T2> my_insert(T1 key, T2 info){
myBinarySearchTree<T1, T2> res;
//1.1 If the tree is empty, then we insert the element by creating a new tree just containing this element
if (this.my_is_empty() == true){
//1.1.1. We create a new node with the element
myBinarySearchNode<T1, T2> node = new myBinarySearchNode<T1, T2>(key, info);
//1.1.2. We link the root of the current tree to the new node just created
this.root = node;
}
//1.2. If the tree is not empty
else{
//1.2.1. We compare the element to the root of the tree
int k = key.compareTo(this.my_root().getKey());
//Case A: If they are equal, we do nothing as the node is already in the three
if (k == 0){
this.my_root().setInfo(info);
}
//Otherwise, we have to keep looking
else{
//Case B: If the element is smaller than the root, we insert the node in the left subtree.
if (k < 0){
myBinarySearchTree<T1, T2> myTree1 = this.my_left_tree().my_insert(key, info);
this.root.setLeft(myTree1.my_root());
}
//Case C: If the element is bigger than the root, we insert the node in the right subtree.
else{
myBinarySearchTree<T1, T2> myTree1 = this.my_right_tree().my_insert(key, info);
this.root.setRight(myTree1.my_root());
}
}
}
//2 We make res to point to the root of the tree and we return it
res = new myBinarySearchTreeImpl<T1, T2>(this.my_root());
return res;
}
//-------------------------------------------------------------------
// 8. Basic Operation --> Remove element at myBinarySearchTree: my_remove
//-------------------------------------------------------------------
public myBinarySearchTree<T1, T2> my_remove(T1 key){
myBinarySearchTree<T1, T2> res;
//1. If the tree is non-empty
if (this.my_is_empty() == false){
//1.1. We compare the element to the root of the tree
int k = key.compareTo(this.my_root().getKey());
//Case A: If they are equal, is because we have found the node
if (k == 0){
boolean b1 = this.my_left_tree().my_is_empty();
boolean b2 = this.my_right_tree().my_is_empty();
//Case A1: If both subtrees are non-empty: We create a new tree with minimum of right subtree as root. Also, we remove the minimum of right subtree from the proper right subtree
if ((b1 == false) && (b2 == false)){
//A1.1. We get the minimum from the right subtree
myBinarySearchNode<T1, T2> min = this.my_right_tree().my_minimum();
//A1.2. We create our new tree
myBinarySearchNode<T1, T2> node = new myBinarySearchNode<T1, T2>(min.getKey(), min.getInfo());
node.setLeft(this.my_left_tree().my_root());
myBinarySearchTree<T1, T2> myNewRightSubtree = this.my_right_tree().my_remove(min.getKey());
node.setRight(myNewRightSubtree.my_root());
//A1.4. We link the root of the current tree to the root of the new tree
this.root = node;
}
//Case A2: If left subtree is empty, the resulting tree will be the right subtree
if ((b1 == true) && (b2 == false))
this.root = this.my_right_tree().my_root();
//Case A3: If right subtree is empty, the resulting tree will be the left subtree
if ((b1 == false) && (b2 == true))
this.root = this.my_left_tree().my_root();
//Case A4: If both subtrees are empty, the resulting tree will be an empty tree
if ((b1 == true) && (b2 == true))
this.root = null;
}
//Otherwise, we have to keep removing it in the subtrees
else{
//Case B: If the element is smaller than the root, we remove the node in the left subtree.
if (k < 0){
myBinarySearchTree<T1, T2> myTree1 = this.my_left_tree().my_remove(key);
this.root.setLeft(myTree1.my_root());
}
//Case C: If the element is bigger than the root, we remove the node in the right subtree.
else{
myBinarySearchTree<T1, T2> myTree1 = this.my_right_tree().my_remove(key);
this.root.setRight(myTree1.my_root());
}
}
}
//2 We make res to point to the root of the tree and we return it
res = new myBinarySearchTreeImpl<T1, T2>(this.my_root());
return res;
}
//-------------------------------------------------------------------
// 9. Additional Operation --> Check the max number of levels of myBinarySearchTree: my_length
//-------------------------------------------------------------------
public int my_length(){
int num;
if (this.my_is_empty() == true)
num = 0;
else{
int l1 = this.my_left_tree().my_length();
int l2 = this.my_right_tree().my_length();
num = Math.max(l1,l2) + 1;
}
return num;
}
//-------------------------------------------------------------------
// 10. Additional Operation --> Count the amount of nodes of myBinarySearchTree: my_node_count
//-------------------------------------------------------------------
public int my_node_count(){
int num;
if (this.my_is_empty() == true)
num = 0;
else{
int c1 = this.my_left_tree().my_node_count();
int c2 = this.my_right_tree().my_node_count();
num = c1 + c2 + 1;
}
return num;
}
//-------------------------------------------------------------------
// 11. Additional Operation --> Count the amount of leaf nodes of myBinarySearchTree: my_leaf_count
//-------------------------------------------------------------------
public int my_leaf_count(){
int num;
if (this.my_is_empty() == true)
num = 0;
else{
if ((this.my_left_tree().my_is_empty() == true) && (this.my_right_tree().my_is_empty() == true))
num = 1;
else{
int l1 = this.my_left_tree().my_leaf_count();
int l2 = this.my_right_tree().my_leaf_count();
num = l1 + l2;
}
}
return num;
}
//-------------------------------------------------------------------
// 12. Traversal Operation --> Traverse myBinarySearchTree in in-order: my_inorder
//-------------------------------------------------------------------
public myList<T2> my_inorder(){
//1. We create the empty list
myList<T2> list = new myListArrayList<T2>();
//2. If the root node is not empty
if (this.my_is_empty() == false){
//2.1. We create the list of traversing the left and right sub-trees
myList<T2> list1 = this.my_left_tree().my_inorder();
myList<T2> list2 = this.my_right_tree().my_inorder();
//2.2. We add the elements of list1 to list
int k1 = list1.my_get_length();
for (int i = 0; i < k1; i++)
list.my_add_element(i, list1.my_get_element(i));
//2.3. We add the root element to list
list.my_add_element(k1, this.my_root().getInfo());
//2.4. We add the elements of list2 to list
int k2 = list2.my_get_length();
for (int i = 0; i < k2; i++)
list.my_add_element((k1+1+i), list2.my_get_element(i));
}
//3. We return the list
return list;
}
//-------------------------------------------------------------------
// 13. Traversal Operation --> Traverse myBinarySearchTree in pre-order: my_preorder
//-------------------------------------------------------------------
public myList<T2> my_preorder(){
//1. We create the empty list
myList<T2> list = new myListArrayList<T2>();
//2. If the root node is not empty
if (this.my_is_empty() == false){
//2.1. We create the list of traversing the left and right sub-trees
myList<T2> list1 = this.my_left_tree().my_preorder();
myList<T2> list2 = this.my_right_tree().my_preorder();
//2.2. We add the root element to list
list.my_add_element(0, this.my_root().getInfo());
//2.3. We add the elements of list1 to list
int k1 = list1.my_get_length();
for (int i = 0; i < k1; i++)
list.my_add_element(i+1, list1.my_get_element(i));
//2.4. We add the elements of list2 to list
int k2 = list2.my_get_length();
for (int i = 0; i < k2; i++)
list.my_add_element((k1+1+i), list2.my_get_element(i));
}
//3. We return the list
return list;
}
//-------------------------------------------------------------------
// 14. Traversal Operation --> Traverse myBinarySearchTree in post-order: my_postorder
//-------------------------------------------------------------------
public myList<T2> my_postorder(){
//1. We create the empty list
myList<T2> list = new myListArrayList<T2>();
//2. If the root node is not empty
if (this.my_is_empty() == false){
//2.1. We create the list of traversing the left and right sub-trees
myList<T2> list1 = this.my_left_tree().my_postorder();
myList<T2> list2 = this.my_right_tree().my_postorder();
//2.2. We add the elements of list1 to list
int k1 = list1.my_get_length();
for (int i = 0; i < k1; i++)
list.my_add_element(i, list1.my_get_element(i));
//2.3. We add the elements of list2 to list
int k2 = list2.my_get_length();
for (int i = 0; i < k2; i++)
list.my_add_element((k1+i), list2.my_get_element(i));
//2.4. We add the root element to list
list.my_add_element(k1+k2, this.my_root().getInfo());
}
//3. We return the list
return list;
}
//-------------------------------------------------------------------
// 15. Additional Operation --> Get maximum element at myBinarySearchTree: my_maximum
//-------------------------------------------------------------------
public myBinarySearchNode<T1, T2> my_maximum() throws myException{
myBinarySearchNode<T1, T2> res;
//1.1 If the tree is empty, then there is no minimum, so an exception is to be raised
if (this.my_is_empty() == true)
throw new myException("The tree is empty, so it has no maximum");
//1.2. If it is not empty
else{
//1.2.1. We get the right subtree
myBinarySearchTree<T1, T2> r = this.my_right_tree();
//1.2.1. If the right tree is empty, then the maximum is the root node
if (r.my_is_empty() == true)
res = this.my_root();
//1.2.2. If the right tree is non-empty, we keep looking for the maximum in the right subtree.
else
res = r.my_maximum();
}
//2. We return res
return res;
}
//-------------------------------------------------------------------
// 16. Additional Operation --> Get minimum element at myBinarySearchTree: my_minimum
//-------------------------------------------------------------------
public myBinarySearchNode<T1, T2> my_minimum() throws myException{
myBinarySearchNode<T1, T2> res;
//1.1 If the tree is empty, then there is no minimum, so an exception is to be raised
if (this.my_is_empty() == true)
throw new myException("The tree is empty, so it has no minimum");
//1.2. If it is not empty
else{
//1.2.1. We get the left subtree
myBinarySearchTree<T1, T2> l = this.my_left_tree();
//1.2.1. If the left tree is empty, then the minimum is the root node
if (l.my_is_empty() == true)
res = this.my_root();
//1.2.2. If the left tree is non-empty, we keep looking for the minimum in the left subtree.
else
res = l.my_minimum();
}
//2. We return res
return res;
}
//-------------------------------------------------------------------
// 17. Assignment 2 - Operation 1 --> Count how many nodes are there at level k of myBinarySearchTree: my_count_at_level
//-------------------------------------------------------------------
public int my_count_at_level(int level){
int nodeCount = 0;
if(level > 0 && !my_is_empty()) {
if(level == 1){
nodeCount++;
}
nodeCount += my_left_tree().my_count_at_level(level-1);
nodeCount += my_right_tree().my_count_at_level(level-1);
}
return nodeCount;
}
//-------------------------------------------------------------------
// 18. Assignment 2 - Operation 2 --> Check if myBinarySearchTree is balanced: my_is_balanced
//-------------------------------------------------------------------
public boolean my_is_balanced(){
boolean balanced = false;
int treesBalanced = 0;
if(!my_is_empty()){
if(!(my_left_tree().my_is_empty() && my_right_tree().my_is_empty())){
if(my_left_tree().my_is_balanced()){
treesBalanced++;
}
if(my_right_tree().my_is_balanced()){
treesBalanced++;
}
if(treesBalanced == 0 || treesBalanced == 2){
balanced = true;
}
}else{
balanced = true;
}
}
return balanced;
}
//-------------------------------------------------------------------
// 19. Assignment 2 - Operation 3 --> Count how many nodes are smaller in myBinarySearchTree: my_count_smaller_nodes
//-------------------------------------------------------------------
public int my_count_smaller_nodes(T1 key){
int nodeCount = 0;
if(!my_is_empty()){
if(!my_left_tree().my_is_empty()){
if(my_left_tree().my_root().getKey().compareTo(key) == -1){
nodeCount++;
}
nodeCount += my_left_tree().my_count_smaller_nodes(key);
}
if(!my_right_tree().my_is_empty()){
if(my_right_tree().my_root().getKey().compareTo(key) == -1){
nodeCount++;
}
nodeCount += my_right_tree().my_count_smaller_nodes(key);
}
}else{
nodeCount = 0;
}
return nodeCount;
}
//-------------------------------------------------------------------
// 20. Assignment 2 - Operation 4 --> Return the level in myBinarySearchTree were the node is located : my_find_node_at_level
//-------------------------------------------------------------------
public int my_find_node_at_level(T1 key){
int level = 0;
if(!my_is_empty()){
switch (key.compareTo(my_root().getKey())) {
case 1:
if(level == 0){
if (!my_right_tree().my_is_empty()) {
level += my_right_tree().my_find_node_at_level(key);
if(level > 0){
level++;
}
}
}else{
level++;
}
break;
case -1:
if(level == 0) {
if (!my_left_tree().my_is_empty()) {
level += my_left_tree().my_find_node_at_level(key);
if(level > 0){
level++;
}
}
}else{
level++;
}
break;
case 0:
level++;
break;
}
}
return level;
}
}