-
Notifications
You must be signed in to change notification settings - Fork 0
/
23trees.java
350 lines (341 loc) · 10.4 KB
/
23trees.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
package com.company;
import java.util.*;
public class Main {
static boolean checkOrder(List<Integer> l){
for(int i = 1; i < l.size(); ++i){
if(l.get(i) < l.get(i - 1)) return false;
}
return true;
}
public static void main(String[] args) {
RedBlackTree<Integer, Integer> rb = new RedBlackTree<>();
RandomInt rnd = new RandomInt();
ArrayList<Integer> ints = new ArrayList<>();
for(int i = 0; i <= 1000; ++i){
ints.add(rnd.getNext(0, 100));
}
for(int i = 0; i <= 1000; ++i){
rb.put(ints.get(i), 0);
}
boolean ok = true;
while(!rb.isEmpty()){
rb.delete(rnd.getNext(0, 100));
ok &= rb.isBalanced() & rb.isRedBlack();
}
System.out.println("Is ok? : " + ok);
System.out.println("Is empty? : " + rb.isEmpty());
}
}
class RandomString{
private final int MIN = Character.MIN_VALUE;
private final int MAX = Character.MAX_VALUE;
private RandomInt rnd = new RandomInt();
public String getNext(int size){
StringBuilder sb = new StringBuilder(size);
for(int i = 0; i < size; ++i){
sb.append(rnd.getNext(MIN, MAX));
}
return sb.toString();
}
}
// returns an element from a (inclusive) to b (inclusive as well)
class RandomInt{
private Random rnd = new Random(System.currentTimeMillis());
public int getNext(int from, int to){
return (int)(rnd.nextDouble() * (to - from + 1)) + from;
}
}
class RedBlackTree<Key extends Comparable<Key>, Value>{
private final boolean RED = true;
private final boolean BLACK = false;
private class Node{
Key key;
Value val;
Node left, right;
boolean color;
int size;
public Node(Key key, Value val, int size, boolean color){
this.key = key;
this.val = val;
this.size = size;
this.color = color;
}
}
private Node root;
public void put(Key key, Value val){
root = put(root, key, val);
root.color = BLACK;
}
private Node put(Node h, Key key, Value val){
if(h == null) return new Node(key, val, 1, RED);
int cmp = key.compareTo(h.key);
if(cmp < 0) h.left = put(h.left, key, val);
else if(cmp > 0) h.right = put(h.right, key, val);
else h.val = val;
//if( isRED(node.left) && isRED(node.left.left)) node = rotateRight(node);
//if(!isRED(node.left) && isRED(node.right)) node = rotateLeft(node);
//if( isRED(node.left) && isRED(node.right)) flipColors(node);
if(isRED(h.right) && !isRED(h.left)) h = rotateLeft(h);
if(isRED(h.left) && isRED(h.left.left)) h = rotateRight(h);
if(isRED(h.left) && isRED(h.right)) flipColors(h);
h.size = size(h.left) + size(h.right) + 1;
return h;
}
private boolean isRED(Node node){
if(node == null) return false;
return node.color == RED;
}
private int size(Node node){
if(node == null) return 0;
return node.size;
}
private void flipColors(Node node){
node.color = RED;
node.left.color = BLACK;
node.right.color = BLACK;
}
private Node rotateLeft(Node node){
Node x = node.right;
node.right = x.left;
x.left = node;
x.color = node.color;
node.color = RED;
x.size = node.size;
node.size = size(node.left) + size(node.right) + 1;
return x;
}
private Node rotateRight(Node node){
Node x = node.left;
node.left = x.right;
x.right = node;
x.color = node.color;
node.color = RED;
x.size = node.size;
node.size = size(node.left) + size(node.right) + 1;
return x;
}
public void deleteMin(){
root = deleteMin(root);
if(isRED(root)) root.color = BLACK;
}
private boolean is4Node(Node h){
return isRED(h.left) && isRED(h.right);
}
private boolean case1Min(Node h){
if( !isRED(h.left) &&
h.right != null &&
isRED(h.right.left) &&
h.left != null &&
!isRED(h.left.left)
)
return true;
return false;
}
private boolean case2Min(Node h) {
if (!isRED(h.left) &&
h.left != null &&
h.right != null &&
!isRED(h.left.left)
)
return true;
return false;
}
private boolean case1Max(Node h){
if( !isRED(h.right) &&
h.left != null &&
(isRED(h.left.left)) &&
h.right != null &&
!isRED(h.right.left))
return true;
return false;
}
private boolean case2Max(Node h){
if( !isRED(h.right) &&
h.left != null &&
h.right != null &&
!isRED(h.right.left)
)
return true;
return false;
}
private Node handleCase1Max(Node h){
h = rotateRight(h);
h.left.color = BLACK;
h.right.color = BLACK;
h.right.right.color = RED;
return h;
}
private Node handleCase2Max(Node h){
h.color = !(h.left.color = h.right.color = RED);
return h;
}
private Node handleCase1Min(Node h){
h.right = rotateRight(h.right);
h = rotateLeft(h);
h.right.color = BLACK; //TODO: remove if anything
h.left.left.color = RED; //TODO: remove if anything
h.left.color = BLACK; // TODO: remove if anything
return h;
}
private Node handleCase2Min(Node h){
h.color = !(h.left.color = h.right.color = RED);
return h;
}
private Node deleteMin(Node h){
if(h == root){ // at root
if(case1Min(h)){
h = handleCase1Min(h);
} else if(case2Min(h)){
h = handleCase2Min(h);
}
} else {
if(case1Min(h)){
h = handleCase1Min(h);
} else if(case2Min(h)){
h = handleCase2Min(h);
}
}
if(h.left == null) return null;
h.left = deleteMin(h.left);
return balance(h);
}
private Node balance(Node h){
if(isRED(h.left) && isRED(h.left.left)){
//System.out.println("Balance case 1, element : " + h.key);
h = rotateRight(h);
}
if(!isRED(h.left) && isRED(h.right)){
//System.out.println("Balance case 2, element : " + h.key);
h = rotateLeft(h);
}
if(isRED(h.left) && isRED(h.right)){
//System.out.println("Balance case 3, element : " + h.key);
flipColors(h);
}
return h;
}
public void deleteMax(){
root = deleteMax(root);
if(isRED(root)) root.color = BLACK;
}
private Node deleteMax(Node h){
if(isRED(h.left)){ // GOOD MOVE
h = rotateRight(h);
}
if(case1Max(h)){
//System.out.println("Case 1 ( " + h.key + " )");
h = handleCase1Max(h);
} else if(case2Max(h)) {
//System.out.println("Case 2 ( " + h.key + " )");
h = handleCase2Max(h);
}
if(h.right == null) return null;
h.right = deleteMax(h.right);
return balance(h);
}
public void delete(Key key){
root = delete(root, key);
if(isRED(root)) root.color = BLACK;
}
private Node delete(Node h, Key key){
if(h == null) return null;
if(key.compareTo(h.key) < 0){
if(case1Min(h)){
h = handleCase1Min(h);
} else if(case2Min(h)){
h = handleCase2Min(h);
}
h.left = delete(h.left, key);
} else {
if(isRED(h.left)){
h = rotateRight(h);
}
if(case1Max(h)){
h = handleCase1Max(h);
} else if(case2Max(h)){
h = handleCase2Max(h);
}
if(key.compareTo(h.key) == 0 && h.right == null) return null;
if(key.compareTo(h.key) == 0) {
Node temp = min(h.right);
h.key = temp.key; h.val = temp.val;
h.right = deleteMin(h.right);
}
if(key.compareTo(h.key) > 0){
h.right = delete(h.right, key);
}
}
return balance(h);
}
public boolean isEmpty(){
return size(root) == 0;
}
public boolean isRedBlack(){
return isRedBlack(root);
}
private Node min(Node h){
if(h == null) return null;
while(h.left != null){
h = h.left;
}
return h;
}
private boolean isRedBlack(Node h){
if(h == null) return true;
if(isRED(h.right)) return false;
if(h != root && isRED(h) && isRED(h.left)) return false;
return isRedBlack(h.left) && isRedBlack(h.right);
}
public boolean isBalanced(){
Node x = root;
int black = 0;
while(x != null){
if(!isRED(x)) black++;
x = x.left;
}
return isBalanced(root, black);
}
private boolean isBalanced(Node h, int black){
if(h == null) return black == 0;
if(!isRED(h)) black--;
return isBalanced(h.left, black) && isBalanced(h.right, black);
}
public int size(){
return size(root);
}
public Key min(){
if(isEmpty()) throw new NoSuchElementException("Empty tree");
Node x = root;
while(x.left != null){
x = x.left;
}
return x.key;
}
public Key max(){
if(isEmpty()) throw new NoSuchElementException("Empty tree");
Node x = root;
while(x.right != null){
x = x.right;
}
return x.key;
}
public Iterable<Key> keys(){
throw new UnsupportedOperationException();
}
public void print(){
print(root, 0);
}
private void print(Node x, int l){
if(x == null) return;
print(x.right, l + 1);
if(l != 0) {
for (int i = 0; i < l - 1; ++i) {
System.out.print("|\t");
}
System.out.println("|---" + x.key + (isRED(x) ?"r":""));
} else {
System.out.println(x.key);
}
print(x.left, l + 1);
}
}