-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.java
484 lines (418 loc) · 11.1 KB
/
BST.java
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
package finalTrial;
import firstTrial.StackQueue;
import firstTrial.tNode;
public class BST<T extends Comparable<T>> {
Tnode<T> root; // point at null at first
// Add to the tree (A binary tree)
// helper method
public void insert(T data) {
Tnode newnode = new Tnode(data);
if (root == null) {
root = newnode;
} else {
insert(root, data);
}
}
// recursive method
public void insert(Tnode node, T data) {
if ((node.getLeft() == null && node.getData().compareTo(data) > 0)) {
Tnode newnode = new Tnode(data);
node.setLeft(newnode);
}
if (node.getRight() == null && node.getData().compareTo(data) < 0) {
Tnode newnode = new Tnode(data);
node.setRight(newnode);
}
if (node.getData().compareTo(data) > 0 && node.getLeft() != null) {
insert(node.getLeft(), data);
}
if (node.getData().compareTo(data) < 0 && node.getRight() != null) {
insert(node.getRight(), data);
}
}
public void traverseInOrder(Tnode node) {
if (node != null) {
traverseInOrder(node.getLeft());
System.out.println(node + " ");
traverseInOrder(node.getRight());
}
}
public void traverseInOrderNoRecursion(Tnode root) {
LStack stack = new LStack(); // linkedlist stack
Tnode curr = root;
while (curr != null || !stack.isEmpty()) {
while (curr != null) {
stack.push((Comparable) curr);
curr = curr.getLeft();
}
// now pop the element where their order going to be from left most and up
T element = (T) stack.pop();
System.out.println(element);
// resent curr to the right
// curr= root.getRight();
// start from the right of the left most
curr = curr.getRight();
}
}
// node left right
public void traversePreOrderNoRec(Tnode root) {
LStack stack = new LStack();
stack.push((Comparable) root);
while(!stack.isEmpty()) {
Tnode curr = (Tnode) stack.pop();
System.out.println(curr.getData());
if(curr.hasRight()) {
stack.push((Comparable) curr.getRight());
}
if(curr.hasLeft()) {
stack.push((Comparable) curr.getLeft());
}
}
}
//left right node
public void traversePostOrderNoRec(Tnode root) {
LStack preorderstack = new LStack();
LStack postorderstack = new LStack();
//apply the pre order node left right
preorderstack.push((Comparable) root);
while(!preorderstack.isEmpty()) {
Tnode curr = (Tnode) preorderstack.pop();
postorderstack.push((Comparable) curr);
if(curr.hasRight()) {
preorderstack.push((Comparable) curr.getRight());
}
if(curr.hasLeft()) {
preorderstack.push((Comparable) curr.getLeft());
}
}
// pop form the second stack to get the order reversed
while(!postorderstack.isEmpty()) {
Tnode curr = (Tnode) postorderstack.pop();
System.out.println(curr.getData());
}
}
// delete Node iteratively
public Tnode delete(T data) {
Tnode curr = root;
Tnode parent = root;
boolean isLeftChild = false;
if (root == null) {
return null; // tree is empty
}
// find the node to del and them check which case and apply
while (curr != null && !curr.getData().equals(data)) {
parent = curr;
if (data.compareTo((T) curr.getData()) < 0) {
curr = curr.getLeft();
isLeftChild = true;
} else {
curr = curr.getRight();
isLeftChild = false;
}
if (curr == null) {
return null; // the element to del was not found
}
}
//case1: leaf
if (!curr.hasLeft() && !curr.hasRight()) {
if (curr == root) {
root = null;
} else {
if (isLeftChild) {
parent.setLeft(null);
} else {
parent.setRight(null);
}
}
}
// case2: has 1 child
else if (curr.hasLeft() && !curr.hasRight()) {
if (curr == root) {
root = curr.getLeft();
} else if (isLeftChild) {
parent.setLeft(curr.getLeft());
} else {
parent.setRight(curr.getLeft());
}
} else if (curr.hasRight() && !curr.hasLeft()) {
if (curr == root) {
root = curr.getRight();
} else if (isLeftChild) {
parent.setLeft(curr.getRight());
} else {
parent.setRight(curr.getRight());
}
}
// case3: has two children
else {
Tnode succ = getSuccessor(curr);
if (curr == root) {
root = succ;
} else if (isLeftChild) {
parent.setLeft(succ);
} else {
parent.setRight(succ);
}
succ.setLeft(curr.getLeft());
}
return curr;
}
// succesor
public Tnode getSuccessor(Tnode node) {
Tnode parentSucc = node;
Tnode succ = node;
Tnode curr = node.getRight();
while (curr != null) {
parentSucc = succ;
succ = curr;
curr = curr.getLeft();
}
if (succ != node.getRight()) {
parentSucc.setLeft(succ.getRight());
succ.setRight(node.getRight());
}
return succ;
}
public void traversePreOrder(Tnode node) {
if (node != null) {
System.out.println(node.getData());
if (node.getData() instanceof BST) {
// Assuming traversePreOrder is a method in your BST class
((BST) node.getData()).traversePreOrder(((BST) node.getData()).root);
}
traversePreOrder(node.getLeft());
traversePreOrder(node.getRight());
}
}
// left right node
public void traversePostOrder(Tnode node) {
if (node != null) {
traversePostOrder(node.getLeft());
traversePostOrder(node.getRight());
System.out.println(node);
}
}
public int size() {
return (size(root));
}
public int size(Tnode node) {
if (node == null)
return 0;
if (node.isLeaf())
return 1;
int left = 0;
int right = 0;
if (node.hasLeft())
left = size(node.getLeft());
if (node.hasRight())
right = size(node.getRight());
return left + right + 1;
}
public int height() {
return (height(root));
}
public int height(Tnode node) {
if (node == null)
return 0;
if (node.isLeaf())
return 1;
int left = 0;
int right = 0;
if (node.hasLeft())
left = height(node.getLeft());
if (node.hasRight())
right = height(node.getRight());
if (left > right) {
return left + 1;
} else
return right + 1;
}
public Tnode findMin(Tnode node) {
while (node.getLeft() != null) {
node = node.getLeft();
}
return node;
}
public Tnode findMinRec(Tnode node) {
if (node != null) {
if (node.hasLeft()) {
return findMinRec(node.getLeft());
}
return node;
}
return null; // node does not exist
}
public Tnode findMax(Tnode node) {
while (node.getRight() != null) {
node = node.getRight();
}
return node;
}
public Tnode findMaxRec(Tnode node) {
if (node != null) {
if (node.hasRight()) {
return findMaxRec(node.getRight());
}
return node;
}
return null;
}
public T find(T data) {
if (root != null) {
return find(root, data);
}
return null; // tree is not created
}
public T find(Tnode node, T data) {
if (node != null) {
if (node.getData().compareTo(data) == 0) {
return (T) node.getData(); // Return the data when found
} else if (node.getData().compareTo(data) > 0 && node.hasLeft()) {
return find(node.getLeft(), data);
} else if (node.getData().compareTo(data) < 0 && node.hasRight()) {
return find(node.getRight(), data);
}
}
return null; // was not found
}
// either has two children or (leaf)
public boolean isFull(tNode node) {
if (node == null) {
return true; // it is empty
}
if (node.hasLeft() && !node.hasRight()) {
return false;
}
if (node.hasRight() && !node.hasLeft()) {
return false;
} else
return isFull(node.getLeft()) && isFull(node.getRight());
}
public void levelTraversalFinal() {
StackQueue qu = new StackQueue();
qu.enqueue((Comparable) root);
while(!qu.isEmpty()) {
Tnode t = (Tnode) qu.dequeue();
System.out.println(t.getData());
if(t.hasLeft()) {
qu.enqueue((Comparable) t.getLeft());
}
if(t.hasRight()) {
qu.enqueue((Comparable) t.getRight());
}
}
}
//the level traversal concept is important in figure out of a BST compleet or not
//in any level if a leaf is found and then it has one child after it then the tree is not complete
public boolean isComplete() {
StackQueue qu = new StackQueue();
qu.enqueue((Comparable) root);
//you must use this to identify when left is allowed to empty and when it is not
boolean allow = false ; //default and it become true when node does not has left
while(!qu.isEmpty()) {
Tnode t = (Tnode) qu.dequeue();
if (t.hasLeft()) {
if(allow==true) return false;
qu.enqueue((Comparable) t.getLeft());
}
else {
allow =true; //means that one child is missing so ir is not complete
}
if(t.hasRight()) {
if(allow==true)return false;
qu.enqueue((Comparable) t.getRight());
}
else
allow = true;
}
return true; //if no false statments were returned
}
public boolean isFull() {
StackQueue qu = new StackQueue();
qu.enqueue((Comparable) root);
while(!qu.isEmpty()) {
Tnode t = (Tnode) qu.dequeue();
if (t.hasLeft()) {
qu.enqueue((Comparable) t.getLeft());
}
else {
return false; //means that one child is missing so ir is not complete
}
if(t.hasRight()) {
qu.enqueue((Comparable) t.getRight());
}
else
return false;
}
return true; //if no false statments were returned
}
public int CountNodes() {
StackQueue qu = new StackQueue();
qu.enqueue((Comparable) root);
int count = 0;
while(!qu.isEmpty()) {
Tnode t = (Tnode) qu.dequeue();
if (t.hasLeft()) {
qu.enqueue((Comparable) t.getLeft());
}
if(t.hasRight()) {
qu.enqueue((Comparable) t.getRight());
}
count ++ ;
}
return count; //if no false statments were returned
}
public int countLevels() {
int levels = 0;
StackQueue qu = new StackQueue();
qu.enqueue((Comparable) root);
int currentLevelNode=1;
while(!qu.isEmpty()) {
int newlevelnode = 0;
while(currentLevelNode>0) {
Tnode t = (Tnode) qu.dequeue();
currentLevelNode--;
if(t.hasLeft()) {
qu.enqueue((Comparable) t.getLeft());
newlevelnode++;
}
if(t.hasRight()) {
qu.enqueue((Comparable) t.getRight());
newlevelnode++;
}
}
levels++;
currentLevelNode=newlevelnode;
}
return levels;
}
// level traversal using stack or queue
public void levelTraversal() {
StackQueue qu = new StackQueue();
if (root != null) {
levelTraversal(root, height(), qu);
printPop(qu);
}
}
public void printPop(StackQueue qu) {
while (!qu.isEmpty()) {
System.out.println(qu.dequeue());
}
}
public void levelTraversal(Tnode node, int h, StackQueue qu) {
if (h == 1) {
qu.enqueue(node.getData());
}
else {
if (node != null && node.hasLeft()) {
qu.enqueue(node.getData());
levelTraversal(node.getLeft(), h - 1, qu);// the recursion call is befor the addition to avoid
// repetition
}
if (node != null && node.hasRight()) {
qu.enqueue(node.getData());
levelTraversal(node.getRight(), h - 1, qu);
}
}
}
}