-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathRedBlackTree.java
More file actions
308 lines (283 loc) · 9.11 KB
/
RedBlackTree.java
File metadata and controls
308 lines (283 loc) · 9.11 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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.orc.impl;
/**
* A memory efficient red-black tree that does not allocate any objects per
* an element. This class is abstract and assumes that the child class
* handles the key and comparisons with the key.
*/
abstract class RedBlackTree {
public static final int NULL = -1;
// Various values controlling the offset of the data within the array.
private static final int LEFT_OFFSET = 0;
private static final int RIGHT_OFFSET = 1;
private static final int ELEMENT_SIZE = 2;
protected int size = 0;
private final DynamicIntArray data;
protected int root = NULL;
protected int lastAdd = 0;
private boolean wasAdd = false;
/**
* Create a set with the given initial capacity.
*/
RedBlackTree(int initialCapacity) {
data = new DynamicIntArray(initialCapacity * ELEMENT_SIZE);
}
/**
* Insert a new node into the data array, growing the array as necessary.
*
* @return Returns the position of the new node.
*/
private int insert(int left, int right, boolean isRed) {
int position = size;
size += 1;
setLeft(position, left, isRed);
setRight(position, right);
return position;
}
/**
* Compare the value at the given position to the new value.
* @return 0 if the values are the same, -1 if the new value is smaller and
* 1 if the new value is larger.
*/
protected abstract int compareValue(int position);
/**
* Is the given node red as opposed to black? To prevent having an extra word
* in the data array, we just the low bit on the left child index.
*/
protected boolean isRed(int position) {
return position != NULL &&
(data.get(position * ELEMENT_SIZE + LEFT_OFFSET) & 1) == 1;
}
/**
* Set the red bit true or false.
*/
private void setRed(int position, boolean isRed) {
int offset = position * ELEMENT_SIZE + LEFT_OFFSET;
if (isRed) {
data.set(offset, data.get(offset) | 1);
} else {
data.set(offset, data.get(offset) & ~1);
}
}
/**
* Get the left field of the given position.
*/
protected int getLeft(int position) {
return data.get(position * ELEMENT_SIZE + LEFT_OFFSET) >> 1;
}
/**
* Get the right field of the given position.
*/
protected int getRight(int position) {
return data.get(position * ELEMENT_SIZE + RIGHT_OFFSET);
}
/**
* Set the left field of the given position.
* Note that we are storing the node color in the low bit of the left pointer.
*/
private void setLeft(int position, int left) {
int offset = position * ELEMENT_SIZE + LEFT_OFFSET;
data.set(offset, (left << 1) | (data.get(offset) & 1));
}
/**
* Set the left field of the given position.
* Note that we are storing the node color in the low bit of the left pointer.
*/
private void setLeft(int position, int left, boolean isRed) {
int offset = position * ELEMENT_SIZE + LEFT_OFFSET;
data.set(offset, (left << 1) | (isRed ? 1 : 0));
}
/**
* Set the right field of the given position.
*/
private void setRight(int position, int right) {
data.set(position * ELEMENT_SIZE + RIGHT_OFFSET, right);
}
/**
* Insert or find a given key in the tree and rebalance the tree correctly.
* Rebalancing restores the red-black aspect of the tree to maintain the
* invariants:
* 1. If a node is red, both of its children are black.
* 2. Each child of a node has the same black height (the number of black
* nodes between it and the leaves of the tree).
*
* Inserted nodes are at the leaves and are red, therefore there is at most a
* violation of rule 1 at the node we just put in. Instead of always keeping
* the parents, this routine passing down the context.
*
* The fix is broken down into 6 cases (1.{1,2,3} and 2.{1,2,3} that are
* left-right mirror images of each other). See Algorithms by Cormen,
* Leiserson, and Rivest for the explanation of the subcases.
*
* @param node The node that we are fixing right now.
* @param fromLeft Did we come down from the left?
* @param parent Nodes' parent
* @param grandparent Parent's parent
* @param greatGrandparent Grandparent's parent
* @return Does parent also need to be checked and/or fixed?
*/
private boolean add(int node, boolean fromLeft, int parent,
int grandparent, int greatGrandparent) {
if (node == NULL) {
if (root == NULL) {
lastAdd = insert(NULL, NULL, false);
root = lastAdd;
wasAdd = true;
return false;
} else {
lastAdd = insert(NULL, NULL, true);
node = lastAdd;
wasAdd = true;
// connect the new node into the tree
if (fromLeft) {
setLeft(parent, node);
} else {
setRight(parent, node);
}
}
} else {
int compare = compareValue(node);
boolean keepGoing;
// Recurse down to find where the node needs to be added
if (compare < 0) {
keepGoing = add(getLeft(node), true, node, parent, grandparent);
} else if (compare > 0) {
keepGoing = add(getRight(node), false, node, parent, grandparent);
} else {
lastAdd = node;
wasAdd = false;
return false;
}
// we don't need to fix the root (because it is always set to black)
if (node == root || !keepGoing) {
return false;
}
}
// Do we need to fix this node? Only if there are two reds right under each
// other.
if (isRed(node) && isRed(parent)) {
if (parent == getLeft(grandparent)) {
int uncle = getRight(grandparent);
if (isRed(uncle)) {
// case 1.1
setRed(parent, false);
setRed(uncle, false);
setRed(grandparent, true);
return true;
} else {
if (node == getRight(parent)) {
// case 1.2
// swap node and parent
int tmp = node;
node = parent;
parent = tmp;
// left-rotate on node
setLeft(grandparent, parent);
setRight(node, getLeft(parent));
setLeft(parent, node);
}
// case 1.2 and 1.3
setRed(parent, false);
setRed(grandparent, true);
// right-rotate on grandparent
if (greatGrandparent == NULL) {
root = parent;
} else if (getLeft(greatGrandparent) == grandparent) {
setLeft(greatGrandparent, parent);
} else {
setRight(greatGrandparent, parent);
}
setLeft(grandparent, getRight(parent));
setRight(parent, grandparent);
return false;
}
} else {
int uncle = getLeft(grandparent);
if (isRed(uncle)) {
// case 2.1
setRed(parent, false);
setRed(uncle, false);
setRed(grandparent, true);
return true;
} else {
if (node == getLeft(parent)) {
// case 2.2
// swap node and parent
int tmp = node;
node = parent;
parent = tmp;
// right-rotate on node
setRight(grandparent, parent);
setLeft(node, getRight(parent));
setRight(parent, node);
}
// case 2.2 and 2.3
setRed(parent, false);
setRed(grandparent, true);
// left-rotate on grandparent
if (greatGrandparent == NULL) {
root = parent;
} else if (getRight(greatGrandparent) == grandparent) {
setRight(greatGrandparent, parent);
} else {
setLeft(greatGrandparent, parent);
}
setRight(grandparent, getLeft(parent));
setLeft(parent, grandparent);
return false;
}
}
} else {
return true;
}
}
/**
* Add the new key to the tree.
* @return true if the element is a new one.
*/
protected boolean add() {
add(root, false, NULL, NULL, NULL);
if (wasAdd) {
setRed(root, false);
return true;
} else {
return false;
}
}
/**
* Get the number of elements in the set.
*/
public int size() {
return size;
}
/**
* Reset the table to empty.
*/
public void clear() {
root = NULL;
size = 0;
data.clear();
}
/**
* Get the buffer size in bytes.
*/
public long getSizeInBytes() {
return data.getSizeInBytes();
}
}