-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTree.java
450 lines (414 loc) · 12.6 KB
/
BSTree.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
// Class: Implementation of BST in A2
// Implement the following functions according to the specifications provided in Tree.java
public class BSTree extends Tree {
private BSTree left, right; // Children.
private BSTree parent; // Parent pointer.
public BSTree(){
super();
// This acts as a sentinel root node
// How to identify a sentinel node: A node with parent == null is SENTINEL NODE
// The actual tree starts from one of the child of the sentinel node!.
// CONVENTION: Assume right child of the sentinel node holds the actual root! and left child will always be null.
}
public BSTree(int address, int size, int key){
super(address, size, key);
}
public BSTree Insert(int address, int size, int key)
{
BSTree newNode = new BSTree(address, size, key);
// first move to the root node
BSTree root = this;
while(root.parent != null) {
root = root.parent;
}
//Now root contains sentinel node
//If tree is empty --> right of sentinel is empty
if(root.right == null) {
root.right = newNode;
newNode.parent = root;
return newNode;
}
root = root.right;
// now root is the root of the tree
BSTree temp = null;
while (root != null) {
temp = root;
if (key > root.key) {
root = root.right;
}
else if( key < root.key){
root = root.left;
}
// root.key == key
else{
while(root != null && root.key == key ) {
temp = root;
if(address <= root.address) {
root = root.left;
}
else {
root = root.right;
}
}
if(root == null) {
if(temp.left == null && temp.right == null) {
if(address < temp.address) {
temp.left = newNode;
newNode.parent = temp;
return newNode;
}
else {
temp.right = newNode;
newNode.parent = temp;
return newNode;
}
}
else if ( temp.left == null) {
temp.left = newNode;
newNode.parent = temp;
return newNode;
}
else {//temp.right == null
temp.right = newNode;
newNode.parent = temp;
return newNode;
}
}
//root != null -> we have to insert newNode inbetween temp and root;
if(temp.right == root) {
temp.right = newNode;
newNode.parent = temp;
newNode.right = root;
root.parent = newNode;
return newNode;
}
if(temp.left == root ) {
temp.left = newNode;
newNode.parent = temp;
newNode.left = root;
root.parent = newNode;
return newNode;
}
}
}
//now temp contains the right place next to which newNode should be inserted
if(key>temp.key) {
temp.right = newNode;
newNode.parent = temp;
return newNode;
}
else {
temp.left = newNode;
newNode.parent = temp;
return newNode;
}
}
public boolean Delete(Dictionary e)
{
// Searches for the e.key in the subtree--- can be called on any node so first go to root
// Deletes the element it is found in the subtree and returns true.
Dictionary t =e;
if(t==null) {
return false;
}
BSTree temp = this;
while(temp.parent != null) {
temp = temp.parent;
}
//Now temp contains sentinel node
//If tree is empty --> right of sentinel is empty
if(temp.right == null) {
return false;
}
temp = temp.right;
while(temp != null){
if(temp.key == t.key ) {
while(temp!=null && temp.key == t.key && temp.address != t.address) {
if(t.address > temp.address) {
temp = temp.right;
}
else {
temp = temp.left;
}
}
if(temp!=null && temp.address == t.address && temp.size == t.size){
// Case 1 temp has two children --> successor will not be null i.e. successor exist
if(temp.left !=null && temp.right != null) {
BSTree successor = temp.getNext();
temp.address = successor.address;
temp.size = successor.size;
temp.key = successor.key;
temp = successor;
}
// Case 2 temp is a leaf node
if(temp.left == null && temp.right ==null) {
//temp is the left child of its parent
if(temp.parent.left==temp) {
temp.parent.left = null;
temp =null;
return true;
}
else {
temp.parent.right = null;
temp = null;
return true;
}
}
//Case 3 temp has only one child
if(temp.left == null || temp.right == null) {
if(temp.right == null) {
//if temp is right child of its parent
if( temp.parent.right == temp) {
temp.parent.right = temp.left;
temp.left.parent = temp.parent;
temp = null;
return true;
}
//temp is left child of its parent
else {
temp.parent.left = temp.left;
temp.left.parent = temp.parent;
temp = null;
return true;
}
}
// temp.left == null;
else {
//if temp is right child of its parent
if(temp.parent.right == temp) {
temp.parent.right = temp.right;
temp.right.parent = temp.parent;
temp = null;
return true;
}
//temp is left child of its parent
else {
temp.parent.left = temp.right;
temp.right.parent = temp.parent;
temp = null;
return true;
}
}
}
}
}
else if(t.key > temp.key) {
temp = temp.right;
}
else {
temp = temp.left;
}
}
return false;
}
public BSTree Find(int key, boolean exact)
{
BSTree temp = this;
while(temp.parent != null) {
temp = temp.parent;
}
//Now temp contains sentinel node
// tree is empty
if(temp.right == null) {
return null;
}
temp = temp.right;
//temp is now the root of the tree
if(exact == true) {
while(temp != null ) {
if(temp.key == key) {
//there are no other element with (key,address) smaller than temp
if(temp.left == null ) {
return temp;
}
else if(temp.left.key == key ) {
temp = temp.left;
}
else {
return temp;
}
}
else if(key > temp.key ) {
temp = temp.right;
}
else {
temp = temp.left;
}
}
return null;
}
else if(exact == false) {
// returns the element with SMALLEST key such that key >= k in the subtree. Returns null in case no such element found.
// let's store the last found key in tempfound
BSTree tempfound = null;
while(temp != null || tempfound != null) {
if(tempfound != null && tempfound.key >= key) {
if(tempfound.left == null && tempfound.right == null) {
return tempfound;
}
else if(tempfound.left != null && tempfound.left.key >= key ) {
tempfound = tempfound.left;
temp = tempfound.left;
}
else {
return tempfound;
}
}
else if(temp.key < key) {
temp = temp.right;
}else {
tempfound = temp;
temp = temp.left;
}
}
return null;
}
return null;
}
public BSTree getFirst()
{
// The getFirst() returns the first (smallest) element of the BST subtree and null if the subtree is empty
BSTree subtree = this;
//if called on sentinel node then temp is sentinel node;
if(subtree.parent == null ) {
subtree = subtree.right;
//now subtree is root
}
//if called on empty tree
if(subtree == null ) {
return null;
}
//if the left subtree of "subtree" is empty and also if subtree is leaf--> return the leaf node itself
if(subtree.left == null ) {
return subtree;
}
// Case -1 If left subtree if not null, then its smallest element will be the leftmost child of the left-subtree
subtree = subtree.left;
while(subtree.left != null) {
subtree = subtree.left;
}
return subtree;
}
public BSTree getNext()
{
// The getNext() returns the next element in the (inorder traversal, ordered by key) of the BST
BSTree temp = this;
//if this is sentinel node -> on sentinel return smallest
if(temp.parent == null) {//return the smallest element of the tree
return temp.getFirst();
}
/* Case-1
If right subtree of node is not null, then successor(next largest element in the inorder traversal) lies in right subtree.
Do the following.
Go to right subtree and return the node with minimum key value in the right subtree.
*/
if(temp.right != null) {
return temp.right.getFirst();
}
/* Case - 2
If right subtree of node is null, then succ is one of the ancestors. Do the following.
Travel up using the parent pointer until you see a node which is left child of its parent. The parent of such a node is the succ.
*/
BSTree p = temp.parent;
while (p != null && temp == p.right) {
temp = p;
p = p.parent;
}
return p;
}
public boolean sanity()
{
BSTree current = this;
//if current is sentinel node --> left node of sentinel node must be null
if(current.parent == null) {
if(current.left != null) {
return false;
}
}
if(current.parent != null) {
//if current is left child of its parent
if(current.parent.left == null && current.parent.right == null) {
return false;
}
if(current.parent.left == null) {
if(current.parent.right != current) {
return false;
}
}
if(current.parent.right == null) {
if(current.parent.left != current ) {
return false;
}
}
}
if(current.left != null) {
if(current.left.parent != current) {
return false;
}
if(current.key < current.left.key) {
return false;
}
}
if(current.right != null) {
if(current.right.parent != current) {
return false;
}
if(current.key > current.right.key) {
return false;
}
}
while(current.parent !=null) {
current = current.parent;
}
current = current.right;
//now current is root of the tree
if( isBST(current) != true ) {
return false;
}
if(checkBST(current,Integer.MIN_VALUE,Integer.MAX_VALUE) != true) {
return false;
}
return false;
}
private boolean isBST(BSTree root) {
if(root == null) {//empty tree
return true;
}
if(root.left == null && root.right == null) {//only one element
return true;
}
/*
* We are doing inorder traversal of the tree
* and if in the inorder traversal any property violates
* then it is not a BST
*/
BSTree first = root.getFirst();
BSTree next = root.getFirst().getNext();
while(first!=null && next != null) {
if(first.key == next.key ) {
if(first.address>next.address) {
return false;
}
}
else if(first.key > next.key ) {
return false;
}
first = next;
next = first.getNext();
}
return true;
}
private boolean checkBST(BSTree node,int min , int max) {
//since in our case address is unique we will check with the help of address
if (node == null) {
return true;
}
if(node.address <= min || node.address >= max ) {
return false;
}
if(!checkBST(node.left,min,node.address) || !checkBST(node.right,node.address,max)) {
return false;
}
return true;
}
}