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
|
/** @file
@section license License
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.
*/
#include "ts/RbTree.h"
namespace ts
{
namespace detail
{
/// Equality.
/// @note If @a n is @c NULL it is treated as having the color @c BLACK.
/// @return @c true if @a c and the color of @a n are the same.
inline bool
operator==(RBNode *n, RBNode::Color c)
{
return c == (n ? n->getColor() : RBNode::BLACK);
}
/// Equality.
/// @note If @a n is @c NULL it is treated as having the color @c BLACK.
/// @return @c true if @a c and the color of @a n are the same.
inline bool
operator==(RBNode::Color c, RBNode *n)
{
return n == c;
}
RBNode *
RBNode::getChild(Direction d) const
{
return d == RIGHT ? _right : d == LEFT ? _left : 0;
}
RBNode *
RBNode::rotate(Direction d)
{
self *parent = _parent; // Cache because it can change before we use it.
Direction child_dir = _parent ? _parent->getChildDirection(this) : NONE;
Direction other_dir = this->flip(d);
self *child = this;
if (d != NONE && this->getChild(other_dir)) {
child = this->getChild(other_dir);
this->clearChild(other_dir);
this->setChild(child->getChild(d), other_dir);
child->clearChild(d);
child->setChild(this, d);
child->structureFixup();
this->structureFixup();
if (parent) {
parent->clearChild(child_dir);
parent->setChild(child, child_dir);
} else {
child->_parent = 0;
}
}
return child;
}
RBNode *
RBNode::setChild(self *n, Direction d)
{
if (n)
n->_parent = this;
if (d == RIGHT)
_right = n;
else if (d == LEFT)
_left = n;
return n;
}
// Returns the root node
RBNode *
RBNode::rippleStructureFixup()
{
self *root = this; // last node seen, root node at the end
self *p = this;
while (p) {
p->structureFixup();
root = p;
p = root->_parent;
}
return root;
}
void
RBNode::replaceWith(self *n)
{
n->_color = _color;
if (_parent) {
Direction d = _parent->getChildDirection(this);
_parent->setChild(0, d);
if (_parent != n)
_parent->setChild(n, d);
} else {
n->_parent = 0;
}
n->_left = n->_right = 0;
if (_left && _left != n)
n->setChild(_left, LEFT);
if (_right && _right != n)
n->setChild(_right, RIGHT);
_left = _right = 0;
}
/* Rebalance the tree. This node is the unbalanced node. */
RBNode *
RBNode::rebalanceAfterInsert()
{
self *x(this); // the node with the imbalance
while (x && x->_parent == RED) {
Direction child_dir = NONE;
if (x->_parent->_parent)
child_dir = x->_parent->_parent->getChildDirection(x->_parent);
else
break;
Direction other_dir(flip(child_dir));
self *y = x->_parent->_parent->getChild(other_dir);
if (y == RED) {
x->_parent->_color = BLACK;
y->_color = BLACK;
x = x->_parent->_parent;
x->_color = RED;
} else {
if (x->_parent->getChild(other_dir) == x) {
x = x->_parent;
x->rotate(child_dir);
}
// Note setting the parent color to BLACK causes the loop to exit.
x->_parent->_color = BLACK;
x->_parent->_parent->_color = RED;
x->_parent->_parent->rotate(other_dir);
}
}
// every node above this one has a subtree structure change,
// so notify it. serendipitously, this makes it easy to return
// the new root node.
self *root = this->rippleStructureFixup();
root->_color = BLACK;
return root;
}
// Returns new root node
RBNode *
RBNode::remove()
{
self *root = 0; // new root node, returned to caller
/* Handle two special cases first.
- This is the only node in the tree, return a new root of NIL
- This is the root node with only one child, return that child as new root
*/
if (!_parent && !(_left && _right)) {
if (_left) {
_left->_parent = 0;
root = _left;
root->_color = BLACK;
} else if (_right) {
_right->_parent = 0;
root = _right;
root->_color = BLACK;
} // else that was the only node, so leave @a root @c NULL.
return root;
}
/* The node to be removed from the tree.
If @c this (the target node) has both children, we remove
its successor, which cannot have a left child and
put that node in place of the target node. Otherwise this
node has at most one child, so we can remove it.
Note that the successor of a node with a right child is always
a right descendant of the node. Therefore, remove_node
is an element of the tree rooted at this node.
Because of the initial special case checks, we know
that remove_node is @b not the root node.
*/
self *remove_node(_left && _right ? _right->leftmostDescendant() : this);
// This is the color of the node physically removed from the tree.
// Normally this is the color of @a remove_node
Color remove_color = remove_node->_color;
// Need to remember the direction from @a remove_node to @a splice_node
Direction d(NONE);
// The child node that will be promoted to replace the removed node.
// The choice of left or right is irrelevant, as remove_node has at
// most one child (and splice_node may be NIL if remove_node has no
// children).
self *splice_node(remove_node->_left ? remove_node->_left : remove_node->_right);
if (splice_node) {
// @c replace_with copies color so in this case the actual color
// lost is that of the splice_node.
remove_color = splice_node->_color;
remove_node->replaceWith(splice_node);
} else {
// No children on remove node so we can just clip it off the tree
// We update splice_node to maintain the invariant that it is
// the node where the physical removal occurred.
splice_node = remove_node->_parent;
// Keep @a d up to date.
d = splice_node->getChildDirection(remove_node);
splice_node->setChild(0, d);
}
// If the node to pull out of the tree isn't this one,
// then replace this node in the tree with that removed
// node in liu of copying the data over.
if (remove_node != this) {
// Don't leave @a splice_node referring to a removed node
if (splice_node == this)
splice_node = remove_node;
this->replaceWith(remove_node);
}
root = splice_node->rebalanceAfterRemove(remove_color, d);
root->_color = BLACK;
return root;
}
/**
* Rebalance tree after a deletion
* Called on the spliced in node or its parent, whichever is not NIL.
* This modifies the tree structure only if @a c is @c BLACK.
*/
RBNode *
RBNode::rebalanceAfterRemove(Color c, //!< The color of the removed node
Direction d //!< Direction of removed node from its parent
)
{
self *root;
if (BLACK == c) { // only rebalance if too much black
self *n = this;
self *parent = n->_parent;
// If @a direction is set, then we need to start at a leaf psuedo-node.
// This is why we need @a parent, otherwise we could just use @a n.
if (NONE != d) {
parent = n;
n = 0;
}
while (parent) { // @a n is not the root
// If the current node is RED, we can just recolor and be done
if (n == RED) {
n->_color = BLACK;
break;
} else {
// Parameterizing the rebalance logic on the directions. We
// write for the left child case and flip directions for the
// right child case
Direction near(LEFT), far(RIGHT);
if ((NONE == d && parent->getChildDirection(n) == RIGHT) || RIGHT == d) {
near = RIGHT;
far = LEFT;
}
self *w = parent->getChild(far); // sibling(n)
if (w->_color == RED) {
w->_color = BLACK;
parent->_color = RED;
parent->rotate(near);
w = parent->getChild(far);
}
self *wfc = w->getChild(far);
if (w->getChild(near) == BLACK && wfc == BLACK) {
w->_color = RED;
n = parent;
parent = n->_parent;
d = NONE; // Cancel any leaf node logic
} else {
if (wfc->_color == BLACK) {
w->getChild(near)->_color = BLACK;
w->_color = RED;
w->rotate(far);
w = parent->getChild(far);
wfc = w->getChild(far); // w changed, update far child cache.
}
w->_color = parent->_color;
parent->_color = BLACK;
wfc->_color = BLACK;
parent->rotate(near);
break;
}
}
}
}
root = this->rippleStructureFixup();
return root;
}
/** Ensure that the local information associated with each node is
correct globally This should only be called on debug builds as it
breaks any efficiencies we have gained from our tree structure.
*/
int
RBNode::validate()
{
#if 0
int black_ht = 0;
int black_ht1, black_ht2;
if (_left) {
black_ht1 = _left->validate();
}
else
black_ht1 = 1;
if (black_ht1 > 0 && _right)
black_ht2 = _right->validate();
else
black_ht2 = 1;
if (black_ht1 == black_ht2) {
black_ht = black_ht1;
if (this->_color == BLACK)
++black_ht;
else { // No red-red
if (_left == RED)
black_ht = 0;
else if (_right == RED)
black_ht = 0;
if (black_ht == 0)
std::cout << "Red-red child\n";
}
} else {
std::cout << "Height mismatch " << black_ht1 << " " << black_ht2 << "\n";
}
if (black_ht > 0 && !this->structureValidate())
black_ht = 0;
return black_ht;
#else
return 0;
#endif
}
}
}
|