-
Notifications
You must be signed in to change notification settings - Fork 368
/
RedBlackTree.java
428 lines (375 loc) · 8.76 KB
/
RedBlackTree.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
//Program to implement Red-Black Tree.
import java.util.Scanner;
public class RedBlackTree{
static class Node{
int data; //data value.
Node left; //points to left child
Node right; //points to right child.
Node parent; //points to parent node.
char color; //color of the node.
public Node(int data, char color){
this.data = data;
this.left = null;
this.right = null;
this.parent = null;
this.color = color;
}
}
static Node root; //root of the tree.
//function to create a Node.
public static Node makeNode(int data){
Node node = new Node(data,'R');
node.left = new Node(-1,'B');
node.right = new Node(-1,'B');
return node;
}
//function to add elements in the tree.
public static void add(int data){
System.out.println("Inserting data : " + data);
Node node = makeNode(data);
if(root == null){
root = node;
root.color = 'B';
return;
}
Node temp = root;
while(temp!=null){
if(temp.data>data){
if(temp.left.data==-1){
temp.left = node;
node.parent = temp;
balance(node); //balance the tree.
return;
}
temp = temp.left;
continue;
}
if(temp.data<data){
if(temp.right.data==-1){
temp.right = node;
node.parent = temp;
balance(node); //balance the tree.
return;
}
temp = temp.right;
}
}
}
//function to remove elements from the tree.
public static void remove(int data){
System.out.println("Remove data : " + data);
if(root==null){
return;
}
//search for the given element in the tree.
Node temp = root;
while(temp.data!=-1){
if(temp.data==data){
break;
}
if(temp.data>data){
temp = temp.left;
continue;
}
if(temp.data<data){
temp = temp.right;
continue;
}
}
//if not found. then return.
if(temp.data==-1){
return;
}
//find the next greater number than the given data.
Node next = findNext(temp);
//swap the data values of given node and next greater number.
int t = temp.data;
temp.data = next.data;
next.data = t;
//remove the next node.
Node parent = next.parent;
if(parent==null){
if(next.left.data==-1){
root = null;
}
else{
root = next.left;
next.parent = null;
root.color = 'B';
}
return;
}
if(parent.right==next){
parent.right = next.left;
}
else{
parent.left = next.left;
}
next.left.parent = parent;
String color = Character.toString(next.left.color) + Character.toString(next.color);
balance(next.left,color); //balance the tree.
}
//function to balance the tree IN CASE OF DELETION.
private static void balance(Node node, String color){
System.out.println("Balancing Node : " + node.data + " Color : " + color);
//if node is Red-Black.
if(node.parent==null || color.equals("BR") || color.equals("RB")){
node.color = 'B';
return;
}
Node parent = node.parent;
//get sibling of the given node.
Node sibling = null;
if(parent.left==node){
sibling = parent.right;
}
else{
sibling = parent.left;
}
Node sibleft = sibling.left; //sibling's left node.
Node sibright = sibling.right; //siblings right node.
//sibling sibleft and sibright all are balck.
if(sibling.color=='B' && sibleft.color=='B' && sibright.color=='B'){
sibling.color = 'R';
node.color = 'B';
String c = Character.toString(parent.color) + Character.toString('B');
balance(parent,c);
return;
}
//sibling is red.
if(sibling.color=='R'){
if(parent.right==sibling){
leftRotate(sibling);
}
else{
rightRotate(sibling);
}
balance(node,color);
return;
}
//sibling is red but one of its children are red.
if(parent.left==sibling){
if(sibleft.color =='R'){
rightRotate(sibling);
sibleft.color = 'B';
}
else{
leftRotate(sibright);
rightRotate(sibright);
sibright.left.color = 'B';
}
return;
}
else{
if(sibright.color == 'R'){
leftRotate(sibling);
sibright.color = 'B';
}
else{
rightRotate(sibleft);
leftRotate(sibleft);
sibleft.right.color= 'B';
}
return;
}
}
//function to balance the tree IN CASE OF INSERTION>
public static void balance(Node node){
System.out.println("Balancing Node : " + node.data);
//if given node is root node.
if(node.parent == null){
root = node;
root.color = 'B';
return;
}
//if node's parent color is black.
if(node.parent.color=='B'){
return;
}
//get the node's parent's sibling node.
Node sibling = null;
if(node.parent.parent.left == node.parent){
sibling = node.parent.parent.right;
}
else{
sibling = node.parent.parent.left;
}
//if sibling color is red.
if(sibling.color == 'R'){
node.parent.color = 'B';
sibling.color = 'B';
node.parent.parent.color = 'R';
balance(node.parent.parent);
return;
}
//if sibling color is black.
else{
if(node.parent.left == node && node.parent.parent.left == node.parent){
rightRotate(node.parent);
balance(node.parent);
return;
}
if(node.parent.right == node && node.parent.parent.right == node.parent){
leftRotate(node.parent);
balance(node.parent);
return;
}
if(node.parent.right == node && node.parent.parent.left == node.parent){
leftRotate(node);
rightRotate(node);
balance(node);
return;
}
if(node.parent.left == node && node.parent.parent.right == node.parent){
rightRotate(node);
leftRotate(node);
balance(node);
return;
}
}
}
//function to find the next greater element than the given node.
private static Node findNext(Node node){
Node next = node.right;
if(next.data==-1){
return node;
}
while(next.left.data!=-1){
next = next.left;
}
return next;
}
//function to perform Left Rotation.
private static void leftRotate(Node node){
System.out.println("Rotating left : " + node.data);
Node parent = node.parent;
Node left = node.left;
node.left = parent;
parent.right = left;
if(left!=null){
left.parent = parent;
}
char c = parent.color;
parent.color = node.color;
node.color = c;
Node gp = parent.parent;
parent.parent = node;
node.parent = gp;
if(gp==null){
root = node;
return;
}
else{
if(gp.left == parent){
gp.left = node;
}
else{
gp.right = node;
}
}
}
//function to perform Right Rotation.
private static void rightRotate(Node node){
System.out.println("Rotating right : " + node.data);
Node parent = node.parent;
Node right = node.right;
node.right = parent;
parent.left = right;
if(right!=null){
right.parent = parent;
}
char c = parent.color;
parent.color = node.color;
node.color = c;
Node gp = parent.parent;
parent.parent = node;
node.parent = gp;
if(gp==null){
root = node;
return;
}
else{
if(gp.left == parent){
gp.left = node;
}
else{
gp.right = node;
}
}
}
//function for PreOrder Traversal of the tree.
private static void preOrder(Node node){
if(node.data==-1){
return;
}
System.out.print(node.data + "-" + node.color + " ");
preOrder(node.left);
preOrder(node.right);
}
//function to display the tree.
public static void display(){
if(root == null){
System.out.println("Empty Tree");
return;
}
System.out.print("Tree's PreOrder Traversal : ");
preOrder(root);
System.out.println();
}
//main function to run the program.
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter data to add");
int data1 = input.nextInt();
add(data1);
display();
System.out.println("Enter data to add");
int data2 = input.nextInt();
add(data2);
display();
System.out.println("Enter data to add");
int data3 = input.nextInt();
add(data3);
display();
System.out.println("Enter data to add");
int data4 = input.nextInt();
add(data4);
display();
System.out.println("Enter data to add");
int data5 = input.nextInt();
add(data5);
display();
System.out.println("Enter data to add");
int data6 = input.nextInt();
add(data6);
display();
System.out.println("Enter data to add");
int data7 = input.nextInt();
add(data7);
display();
System.out.println("Enter data to add");
int data8 = input.nextInt();
add(data8);
display();
System.out.println("Enter data to add");
int data9 = input.nextInt();
add(data9);
display();
System.out.println("Enter data to remove");
int data_rm1 = input.nextInt();
remove(data_rm1);
display();
System.out.println("Enter data to remove");
int data_rm2 = input.nextInt();
remove(data_rm2);
display();
System.out.println("Enter data to remove");
int data_rm3 = input.nextInt();
remove(data_rm3);
display();
System.out.println("Enter data to remove");
int data_rm4 = input.nextInt();
remove(data_rm4);
display();
}
}