source: rtems/cpukit/score/src/rbtreeextract.c @ 8abbbdde

4.115
Last change on this file since 8abbbdde was 8abbbdde, checked in by Sebastian Huber <sebastian.huber@…>, on 07/21/14 at 16:29:00

rbtree: Do not set node off-tree in extract

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 *  Copyright (c) 2010 Gedare Bloom.
3 *
4 *  The license and distribution terms for this file may be
5 *  found in the file LICENSE in this distribution or at
6 *  http://www.rtems.org/license/LICENSE.
7 */
8
9#if HAVE_CONFIG_H
10#include "config.h"
11#endif
12
13#include <rtems/score/rbtreeimpl.h>
14
15/** @brief  Validate and fix-up tree properties after deleting a node
16 *
17 *  This routine is called on a black node, @a the_node, after its deletion.
18 *  This function maintains the properties of the red-black tree.
19 *
20 *  @note It does NOT disable interrupts to ensure the atomicity
21 *        of the extract operation.
22 */
23static void _RBTree_Extract_validate( RBTree_Node *the_node )
24{
25  RBTree_Node     *parent, *sibling;
26  RBTree_Direction dir;
27
28  parent = the_node->parent;
29
30  if ( !parent->parent )
31    return;
32
33  sibling = _RBTree_Sibling( the_node );
34
35  /* continue to correct tree as long as the_node is black and not the root */
36  while ( !_RBTree_Is_red( the_node ) && parent->parent ) {
37    /* if sibling is red, switch parent (black) and sibling colors,
38     * then rotate parent left, making the sibling be the_node's grandparent.
39     * Now the_node has a black sibling and red parent. After rotation,
40     * update sibling pointer.
41     */
42    if ( _RBTree_Is_red( sibling ) ) {
43      parent->color = RBT_RED;
44      sibling->color = RBT_BLACK;
45      dir = the_node != parent->child[ 0 ];
46      _RBTree_Rotate( parent, dir );
47      sibling = parent->child[ _RBTree_Opposite_direction( dir ) ];
48    }
49
50    /* sibling is black, see if both of its children are also black. */
51    if ( !_RBTree_Is_red( sibling->child[ RBT_RIGHT ] ) &&
52         !_RBTree_Is_red( sibling->child[ RBT_LEFT ] ) ) {
53      sibling->color = RBT_RED;
54
55      if ( _RBTree_Is_red( parent ) ) {
56        parent->color = RBT_BLACK;
57        break;
58      }
59
60      the_node = parent;   /* done if parent is red */
61      parent = the_node->parent;
62      sibling = _RBTree_Sibling( the_node );
63    } else {
64      /* at least one of sibling's children is red. we now proceed in two
65       * cases, either the_node is to the left or the right of the parent.
66       * In both cases, first check if one of sibling's children is black,
67       * and if so rotate in the proper direction and update sibling pointer.
68       * Then switch the sibling and parent colors, and rotate through parent.
69       */
70      dir = the_node != parent->child[ 0 ];
71
72      if (
73        !_RBTree_Is_red( sibling->child[ _RBTree_Opposite_direction( dir ) ] )
74      ) {
75        sibling->color = RBT_RED;
76        sibling->child[ dir ]->color = RBT_BLACK;
77        _RBTree_Rotate( sibling, _RBTree_Opposite_direction( dir ) );
78        sibling = parent->child[ _RBTree_Opposite_direction( dir ) ];
79      }
80
81      sibling->color = parent->color;
82      parent->color = RBT_BLACK;
83      sibling->child[ _RBTree_Opposite_direction( dir ) ]->color = RBT_BLACK;
84      _RBTree_Rotate( parent, dir );
85      break; /* done */
86    }
87  } /* while */
88
89  if ( !the_node->parent->parent )
90    the_node->color = RBT_BLACK;
91}
92
93void _RBTree_Extract(
94  RBTree_Control *the_rbtree,
95  RBTree_Node    *the_node
96)
97{
98  RBTree_Node     *leaf, *target;
99  RBTree_Color     victim_color;
100  RBTree_Direction dir;
101
102  /* check if min needs to be updated */
103  if ( the_node == the_rbtree->first[ RBT_LEFT ] ) {
104    RBTree_Node *next;
105    next = _RBTree_Successor( the_node );
106    the_rbtree->first[ RBT_LEFT ] = next;
107  }
108
109  /* Check if max needs to be updated. min=max for 1 element trees so
110   * do not use else if here. */
111  if ( the_node == the_rbtree->first[ RBT_RIGHT ] ) {
112    RBTree_Node *previous;
113    previous = _RBTree_Predecessor( the_node );
114    the_rbtree->first[ RBT_RIGHT ] = previous;
115  }
116
117  /* if the_node has at most one non-null child then it is safe to proceed
118   * check if both children are non-null, if so then we must find a target node
119   * either max in node->child[RBT_LEFT] or min in node->child[RBT_RIGHT],
120   * and replace the_node with the target node. This maintains the binary
121   * search tree property, but may violate the red-black properties.
122   */
123
124  if ( the_node->child[ RBT_LEFT ] && the_node->child[ RBT_RIGHT ] ) {
125    target = the_node->child[ RBT_LEFT ]; /* find max in node->child[RBT_LEFT] */
126
127    while ( target->child[ RBT_RIGHT ] )
128      target = target->child[ RBT_RIGHT ];
129
130    /* if the target node has a child, need to move it up the tree into
131     * target's position (target is the right child of target->parent)
132     * when target vacates it. if there is no child, then target->parent
133     * should become NULL. This may cause the coloring to be violated.
134     * For now we store the color of the node being deleted in victim_color.
135     */
136    leaf = target->child[ RBT_LEFT ];
137
138    if ( leaf ) {
139      leaf->parent = target->parent;
140    } else {
141      /* fix the tree here if the child is a null leaf. */
142      _RBTree_Extract_validate( target );
143    }
144
145    victim_color = target->color;
146    dir = target != target->parent->child[ 0 ];
147    target->parent->child[ dir ] = leaf;
148
149    /* now replace the_node with target */
150    dir = the_node != the_node->parent->child[ 0 ];
151    the_node->parent->child[ dir ] = target;
152
153    /* set target's new children to the original node's children */
154    target->child[ RBT_RIGHT ] = the_node->child[ RBT_RIGHT ];
155
156    if ( the_node->child[ RBT_RIGHT ] )
157      the_node->child[ RBT_RIGHT ]->parent = target;
158
159    target->child[ RBT_LEFT ] = the_node->child[ RBT_LEFT ];
160
161    if ( the_node->child[ RBT_LEFT ] )
162      the_node->child[ RBT_LEFT ]->parent = target;
163
164    /* finally, update the parent node and recolor. target has completely
165     * replaced the_node, and target's child has moved up the tree if needed.
166     * the_node is no longer part of the tree, although it has valid pointers
167     * still.
168     */
169    target->parent = the_node->parent;
170    target->color = the_node->color;
171  } else {
172    /* the_node has at most 1 non-null child. Move the child in to
173     * the_node's location in the tree. This may cause the coloring to be
174     * violated. We will fix it later.
175     * For now we store the color of the node being deleted in victim_color.
176     */
177    leaf = the_node->child[ RBT_LEFT ] ?
178           the_node->child[ RBT_LEFT ] : the_node->child[ RBT_RIGHT ];
179
180    if ( leaf ) {
181      leaf->parent = the_node->parent;
182    } else {
183      /* fix the tree here if the child is a null leaf. */
184      _RBTree_Extract_validate( the_node );
185    }
186
187    victim_color = the_node->color;
188
189    /* remove the_node from the tree */
190    dir = the_node != the_node->parent->child[ 0 ];
191    the_node->parent->child[ dir ] = leaf;
192  }
193
194  /* fix coloring. leaf has moved up the tree. The color of the deleted
195   * node is in victim_color. There are two cases:
196   *   1. Deleted a red node, its child must be black. Nothing must be done.
197   *   2. Deleted a black node, its child must be red. Paint child black.
198   */
199  if ( victim_color == RBT_BLACK ) { /* eliminate case 1 */
200    if ( leaf ) {
201      leaf->color = RBT_BLACK; /* case 2 */
202    }
203  }
204
205  /* set root to black, if it exists */
206  if ( the_rbtree->root )
207    the_rbtree->root->color = RBT_BLACK;
208}
Note: See TracBrowser for help on using the repository browser.