@@ -183,14 +183,133 @@ protected void rotateRight(TreeNode<E> currentNode) {
183
183
}
184
184
}
185
185
186
+ /**
187
+ * removes a specific element from the tree
188
+ *
189
+ * @param item
190
+ * the item to be removed
191
+ *
192
+ * @return true if element is removed
193
+ */
194
+ public boolean remove (E item , boolean destroy ) {
195
+ TreeNode <E > currentNode = findNode (item );
196
+ if (null == currentNode ) {
197
+ return false ;
198
+ }
199
+ TreeNode <E > tmp , ref = currentNode ;
200
+ boolean col = ref .getColor ();
201
+ if (null == currentNode .getLeft ()) {
202
+ tmp = currentNode .getRight ();
203
+ replace (currentNode , currentNode .getRight ());
204
+ } else if (null == currentNode .getRight ()) {
205
+ tmp = currentNode .getLeft ();
206
+ replace (currentNode , currentNode .getLeft ());
207
+ } else {
208
+ ref = findMinimum (currentNode .getRight ());
209
+ col = ref .getColor ();
210
+ tmp = ref .getRight ();
211
+ if (ref .getParent ().equals (currentNode )) {
212
+ tmp .setParent (ref , false );
213
+ } else {
214
+ replace (ref , ref .getRight ());
215
+ ref .setRight (currentNode .getRight (), false );
216
+ ref .getRight ().setParent (ref , false );
217
+ }
218
+ replace (currentNode , ref );
219
+ ref .setLeft (currentNode .getLeft (), false );
220
+ ref .getLeft ().setParent (ref , false );
221
+ ref .setColor (currentNode .getColor ());
222
+ }
223
+ if (col == BLACK && tmp != null ) {
224
+ fixDelete (tmp );
225
+ }
226
+ if (destroy ) {
227
+ currentNode .setLeft (null , false );
228
+ currentNode .setRight (null , false );
229
+ currentNode .setParent (null , false );
230
+ currentNode .destroy ();
231
+ }
232
+ return true ;
233
+ }
234
+
235
+ protected void fixDelete (TreeNode <E > currentNode ) {
236
+ while (!currentNode .equals (root ) && currentNode .getColor () == BLACK ) {
237
+ if (currentNode .equals (currentNode .getParent ().getLeft ())) {
238
+ TreeNode <E > other = currentNode .getParent ().getRight ();
239
+ if (other .getColor () == RED ) {
240
+ other .setColor (BLACK );
241
+ currentNode .getParent ().setColor (RED );
242
+ rotateLeft (currentNode .getParent ());
243
+ other = currentNode .getParent ().getRight ();
244
+ }
245
+ if (other .getLeft ().getColor () == BLACK && other .getRight ().getColor () == BLACK ) {
246
+ other .setColor (RED );
247
+ currentNode = currentNode .getParent ();
248
+ continue ;
249
+ } else if (other .getRight ().getColor () == BLACK ) {
250
+ other .getLeft ().setColor (BLACK );
251
+ other .setColor (RED );
252
+ rotateRight (other );
253
+ other = currentNode .getParent ().getRight ();
254
+ }
255
+ if (other .getRight ().getColor () == RED ) {
256
+ other .setColor (currentNode .getParent ().getColor ());
257
+ currentNode .getParent ().setColor (BLACK );
258
+ other .getRight ().setColor (BLACK );
259
+ rotateLeft (currentNode .getParent ());
260
+ currentNode = root ;
261
+ }
262
+ } else {
263
+ TreeNode other = currentNode .getParent ().getLeft ();
264
+ if (other .getColor () == RED ) {
265
+ other .setColor (BLACK );
266
+ currentNode .getParent ().setColor (RED );
267
+ rotateRight (currentNode .getParent ());
268
+ other = currentNode .getParent ().getLeft ();
269
+ }
270
+ if (other .getRight ().getColor () == BLACK && other .getLeft ().getColor () == BLACK ) {
271
+ other .setColor (RED );
272
+ currentNode = currentNode .getParent ();
273
+ continue ;
274
+ } else if (other .getLeft ().getColor () == BLACK ) {
275
+ other .getRight ().setColor (BLACK );
276
+ other .setColor (RED );
277
+ rotateLeft (other );
278
+ other = currentNode .getParent ().getLeft ();
279
+ }
280
+ if (other .getLeft ().getColor () == RED ) {
281
+ other .setColor (currentNode .getParent ().getColor ());
282
+ currentNode .getParent ().setColor (BLACK );
283
+ other .getLeft ().setColor (BLACK );
284
+ rotateRight (currentNode .getParent ());
285
+ currentNode = root ;
286
+ }
287
+ }
288
+ }
289
+ currentNode .setColor (BLACK );
290
+ }
291
+
292
+ protected void replace (TreeNode <E > first , TreeNode <E > second ) {
293
+ if (null == first .getParent ()) {
294
+ root = second ;
295
+ unsafe .putLong (holder .get (), root .getHandler ());
296
+ } else if (first .equals (first .getParent ().getLeft ())) {
297
+ first .getParent ().setLeft (second , false );
298
+ } else {
299
+ first .getParent ().setRight (second , false );
300
+ }
301
+ if (second != null ) {
302
+ second .setParent (first .getParent (), false );
303
+ }
304
+ }
186
305
187
306
/**
188
307
* checks if tree contains the specified element
189
308
*
190
309
* @param item
191
310
* the item to be set
192
311
*
193
- * @return tree if set contains the element
312
+ * @return true if tree contains the element
194
313
*/
195
314
public boolean contains (E item ) {
196
315
return findNode (item ) == null ? false : true ;
0 commit comments