bsalomon@google.com | 6034c50 | 2011-02-22 16:37:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2011 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef GrRedBlackTree_DEFINED |
| 18 | #define GrRedBlackTree_DEFINED |
| 19 | |
| 20 | #include "GrNoncopyable.h" |
| 21 | |
| 22 | template <typename T> |
| 23 | class GrLess { |
| 24 | public: |
| 25 | bool operator()(const T& a, const T& b) const { return a < b; } |
| 26 | }; |
| 27 | |
| 28 | template <typename T> |
| 29 | class GrLess<T*> { |
| 30 | public: |
| 31 | bool operator()(const T* a, const T* b) const { return *a < *b; } |
| 32 | }; |
| 33 | |
| 34 | /** |
| 35 | * In debug build this will cause full traversals of the tree when the validate |
| 36 | * is called on insert and remove. Useful for debugging but very slow. |
| 37 | */ |
| 38 | #define DEEP_VALIDATE 0 |
| 39 | |
| 40 | /** |
| 41 | * A sorted tree that uses the red-black tree algorithm. Allows duplicate |
| 42 | * entries. Data is of type T and is compared using functor C. A single C object |
| 43 | * will be created and used for all comparisons. |
| 44 | */ |
| 45 | template <typename T, typename C = GrLess<T> > |
| 46 | class GrRedBlackTree : public GrNoncopyable { |
| 47 | public: |
| 48 | /** |
| 49 | * Creates an empty tree. |
| 50 | */ |
| 51 | GrRedBlackTree(); |
| 52 | virtual ~GrRedBlackTree(); |
| 53 | |
| 54 | /** |
| 55 | * Class used to iterater through the tree. The valid range of the tree |
| 56 | * is given by [begin(), end()). It is legal to dereference begin() but not |
| 57 | * end(). The iterator has preincrement and predecrement operators, it is |
| 58 | * legal to decerement end() if the tree is not empty to get the last |
| 59 | * element. However, a last() helper is provided. |
| 60 | */ |
| 61 | class Iter; |
| 62 | |
| 63 | /** |
| 64 | * Add an element to the tree. Duplicates are allowed. |
| 65 | * @param t the item to add. |
| 66 | * @return an iterator to the item. |
| 67 | */ |
| 68 | Iter insert(const T& t); |
| 69 | |
| 70 | /** |
| 71 | * Removes all items in the tree. |
| 72 | */ |
| 73 | void reset(); |
| 74 | |
| 75 | /** |
| 76 | * @return true if there are no items in the tree, false otherwise. |
| 77 | */ |
| 78 | bool empty() const {return 0 == fCount;} |
| 79 | |
| 80 | /** |
| 81 | * @return the number of items in the tree. |
| 82 | */ |
| 83 | int count() const {return fCount;} |
| 84 | |
| 85 | /** |
| 86 | * @return an iterator to the first item in sorted order, or end() if empty |
| 87 | */ |
| 88 | Iter begin(); |
| 89 | /** |
| 90 | * Gets the last valid iterator. This is always valid, even on an empty. |
| 91 | * However, it can never be dereferenced. Useful as a loop terminator. |
| 92 | * @return an iterator that is just beyond the last item in sorted order. |
| 93 | */ |
| 94 | Iter end(); |
| 95 | /** |
| 96 | * @return an iterator that to the last item in sorted order, or end() if |
| 97 | * empty. |
| 98 | */ |
| 99 | Iter last(); |
| 100 | |
| 101 | /** |
| 102 | * Finds an occurrence of an item. |
| 103 | * @param t the item to find. |
| 104 | * @return an iterator to a tree element equal to t or end() if none exists. |
| 105 | */ |
| 106 | Iter find(const T& t); |
| 107 | /** |
| 108 | * Finds the first of an item in iterator order. |
| 109 | * @param t the item to find. |
| 110 | * @return an iterator to the first element equal to t or end() if |
| 111 | * none exists. |
| 112 | */ |
| 113 | Iter findFirst(const T& t); |
| 114 | /** |
| 115 | * Finds the last of an item in iterator order. |
| 116 | * @param t the item to find. |
| 117 | * @return an iterator to the last element equal to t or end() if |
| 118 | * none exists. |
| 119 | */ |
| 120 | Iter findLast(const T& t); |
| 121 | /** |
| 122 | * Gets the number of items in the tree equal to t. |
| 123 | * @param t the item to count. |
| 124 | * @return number of items equal to t in the tree |
| 125 | */ |
| 126 | int countOf(const T& t) const; |
| 127 | |
| 128 | /** |
| 129 | * Removes the item indicated by an iterator. The iterator will not be valid |
| 130 | * afterwards. |
| 131 | * |
| 132 | * @param iter iterator of item to remove. Must be valid (not end()). |
| 133 | */ |
| 134 | void remove(const Iter& iter) { deleteAtNode(iter.fN); } |
| 135 | |
| 136 | static void UnitTest(); |
| 137 | |
| 138 | private: |
| 139 | enum Color { |
| 140 | kRed_Color, |
| 141 | kBlack_Color |
| 142 | }; |
| 143 | |
| 144 | enum Child { |
| 145 | kLeft_Child = 0, |
| 146 | kRight_Child = 1 |
| 147 | }; |
| 148 | |
| 149 | struct Node { |
| 150 | T fItem; |
| 151 | Color fColor; |
| 152 | |
| 153 | Node* fParent; |
| 154 | Node* fChildren[2]; |
| 155 | }; |
| 156 | |
| 157 | void rotateRight(Node* n); |
| 158 | void rotateLeft(Node* n); |
| 159 | |
| 160 | static Node* SuccessorNode(Node* x); |
| 161 | static Node* PredecessorNode(Node* x); |
| 162 | |
| 163 | void deleteAtNode(Node* x); |
| 164 | static void RecursiveDelete(Node* x); |
| 165 | |
| 166 | int countOfHelper(const Node* n, const T& t) const; |
| 167 | |
| 168 | #if GR_DEBUG |
| 169 | void validate() const; |
| 170 | int checkNode(Node* n, int* blackHeight) const; |
| 171 | // checks relationship between a node and its children. allowRedRed means |
| 172 | // node may be in an intermediate state where a red parent has a red child. |
| 173 | bool validateChildRelations(const Node* n, bool allowRedRed) const; |
| 174 | // place to stick break point if validateChildRelations is failing. |
| 175 | bool validateChildRelationsFailed() const { return false; } |
| 176 | #else |
| 177 | void validate() const {} |
| 178 | #endif |
| 179 | |
| 180 | int fCount; |
| 181 | Node* fRoot; |
| 182 | Node* fFirst; |
| 183 | Node* fLast; |
| 184 | |
| 185 | const C fComp; |
| 186 | }; |
| 187 | |
| 188 | template <typename T, typename C> |
| 189 | class GrRedBlackTree<T,C>::Iter { |
| 190 | public: |
| 191 | Iter() {}; |
| 192 | Iter(const Iter& i) {fN = i.fN; fTree = i.fTree;} |
| 193 | Iter& operator =(const Iter& i) { |
| 194 | fN = i.fN; |
| 195 | fTree = i.fTree; |
| 196 | return *this; |
| 197 | } |
| 198 | // altering the sort value of the item using this method will cause |
| 199 | // errors. |
| 200 | T& operator *() const { return fN->fItem; } |
| 201 | bool operator ==(const Iter& i) const { |
| 202 | return fN == i.fN && fTree == i.fTree; |
| 203 | } |
| 204 | bool operator !=(const Iter& i) const { return !(*this == i); } |
| 205 | Iter& operator ++() { |
| 206 | GrAssert(*this != fTree->end()); |
| 207 | fN = SuccessorNode(fN); |
| 208 | return *this; |
| 209 | } |
| 210 | Iter& operator --() { |
| 211 | GrAssert(*this != fTree->begin()); |
| 212 | if (NULL != fN) { |
| 213 | fN = PredecessorNode(fN); |
| 214 | } else { |
| 215 | *this = fTree->last(); |
| 216 | } |
| 217 | return *this; |
| 218 | } |
| 219 | |
| 220 | private: |
| 221 | friend class GrRedBlackTree; |
| 222 | explicit Iter(Node* n, GrRedBlackTree* tree) { |
| 223 | fN = n; |
| 224 | fTree = tree; |
| 225 | } |
| 226 | Node* fN; |
| 227 | GrRedBlackTree* fTree; |
| 228 | }; |
| 229 | |
| 230 | template <typename T, typename C> |
| 231 | GrRedBlackTree<T,C>::GrRedBlackTree() : fComp() { |
| 232 | fRoot = NULL; |
| 233 | fFirst = NULL; |
| 234 | fLast = NULL; |
| 235 | fCount = 0; |
| 236 | validate(); |
| 237 | } |
| 238 | |
| 239 | template <typename T, typename C> |
| 240 | GrRedBlackTree<T,C>::~GrRedBlackTree() { |
| 241 | RecursiveDelete(fRoot); |
| 242 | } |
| 243 | |
| 244 | template <typename T, typename C> |
| 245 | typename GrRedBlackTree<T,C>::Iter GrRedBlackTree<T,C>::begin() { |
| 246 | return Iter(fFirst, this); |
| 247 | } |
| 248 | |
| 249 | template <typename T, typename C> |
| 250 | typename GrRedBlackTree<T,C>::Iter GrRedBlackTree<T,C>::end() { |
| 251 | return Iter(NULL, this); |
| 252 | } |
| 253 | |
| 254 | template <typename T, typename C> |
| 255 | typename GrRedBlackTree<T,C>::Iter GrRedBlackTree<T,C>::last() { |
| 256 | return Iter(fLast, this); |
| 257 | } |
| 258 | |
| 259 | template <typename T, typename C> |
| 260 | typename GrRedBlackTree<T,C>::Iter GrRedBlackTree<T,C>::find(const T& t) { |
| 261 | Node* n = fRoot; |
| 262 | while (NULL != n) { |
| 263 | if (fComp(t, n->fItem)) { |
| 264 | n = n->fChildren[kLeft_Child]; |
| 265 | } else { |
| 266 | if (!fComp(n->fItem, t)) { |
| 267 | return Iter(n, this); |
| 268 | } |
| 269 | n = n->fChildren[kRight_Child]; |
| 270 | } |
| 271 | } |
| 272 | return end(); |
| 273 | } |
| 274 | |
| 275 | template <typename T, typename C> |
| 276 | typename GrRedBlackTree<T,C>::Iter GrRedBlackTree<T,C>::findFirst(const T& t) { |
| 277 | Node* n = fRoot; |
| 278 | Node* leftMost = NULL; |
| 279 | while (NULL != n) { |
| 280 | if (fComp(t, n->fItem)) { |
| 281 | n = n->fChildren[kLeft_Child]; |
| 282 | } else { |
| 283 | if (!fComp(n->fItem, t)) { |
| 284 | // found one. check if another in left subtree. |
| 285 | leftMost = n; |
| 286 | n = n->fChildren[kLeft_Child]; |
| 287 | } else { |
| 288 | n = n->fChildren[kRight_Child]; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | return Iter(leftMost, this); |
| 293 | } |
| 294 | |
| 295 | template <typename T, typename C> |
| 296 | typename GrRedBlackTree<T,C>::Iter GrRedBlackTree<T,C>::findLast(const T& t) { |
| 297 | Node* n = fRoot; |
| 298 | Node* rightMost = NULL; |
| 299 | while (NULL != n) { |
| 300 | if (fComp(t, n->fItem)) { |
| 301 | n = n->fChildren[kLeft_Child]; |
| 302 | } else { |
| 303 | if (!fComp(n->fItem, t)) { |
| 304 | // found one. check if another in right subtree. |
| 305 | rightMost = n; |
| 306 | } |
| 307 | n = n->fChildren[kRight_Child]; |
| 308 | } |
| 309 | } |
| 310 | return Iter(rightMost, this); |
| 311 | } |
| 312 | |
| 313 | template <typename T, typename C> |
| 314 | int GrRedBlackTree<T,C>::countOf(const T& t) const { |
| 315 | return countOfHelper(fRoot, t); |
| 316 | } |
| 317 | |
| 318 | template <typename T, typename C> |
| 319 | int GrRedBlackTree<T,C>::countOfHelper(const Node* n, const T& t) const { |
| 320 | // this is count*log(n) :( |
| 321 | while (NULL != n) { |
| 322 | if (fComp(t, n->fItem)) { |
| 323 | n = n->fChildren[kLeft_Child]; |
| 324 | } else { |
| 325 | if (!fComp(n->fItem, t)) { |
| 326 | int count = 1; |
| 327 | count += countOfHelper(n->fChildren[kLeft_Child], t); |
| 328 | count += countOfHelper(n->fChildren[kRight_Child], t); |
| 329 | return count; |
| 330 | } |
| 331 | n = n->fChildren[kRight_Child]; |
| 332 | } |
| 333 | } |
| 334 | return 0; |
| 335 | |
| 336 | } |
| 337 | |
| 338 | template <typename T, typename C> |
| 339 | void GrRedBlackTree<T,C>::reset() { |
| 340 | RecursiveDelete(fRoot); |
| 341 | fRoot = NULL; |
| 342 | fFirst = NULL; |
| 343 | fLast = NULL; |
| 344 | fCount = 0; |
| 345 | } |
| 346 | |
| 347 | template <typename T, typename C> |
| 348 | typename GrRedBlackTree<T,C>::Iter GrRedBlackTree<T,C>::insert(const T& t) { |
| 349 | validate(); |
| 350 | |
| 351 | ++fCount; |
| 352 | |
| 353 | Node* x = new Node; |
| 354 | x->fChildren[kLeft_Child] = NULL; |
| 355 | x->fChildren[kRight_Child] = NULL; |
| 356 | x->fItem = t; |
| 357 | |
| 358 | Node* gp = NULL; |
| 359 | Node* p = NULL; |
| 360 | Node* n = fRoot; |
bsalomon@google.com | ba9d628 | 2011-02-22 19:45:21 +0000 | [diff] [blame^] | 361 | Child pc = kLeft_Child; // suppress uninit warning |
bsalomon@google.com | 6034c50 | 2011-02-22 16:37:47 +0000 | [diff] [blame] | 362 | Child gpc; |
| 363 | |
| 364 | bool first = true; |
| 365 | bool last = true; |
| 366 | while (NULL != n) { |
| 367 | gpc = pc; |
| 368 | pc = fComp(x->fItem, n->fItem) ? kLeft_Child : kRight_Child; |
| 369 | first = first && kLeft_Child == pc; |
| 370 | last = last && kRight_Child == pc; |
| 371 | gp = p; |
| 372 | p = n; |
| 373 | n = p->fChildren[pc]; |
| 374 | |
| 375 | } |
| 376 | if (last) { |
| 377 | fLast = x; |
| 378 | } |
| 379 | if (first) { |
| 380 | fFirst = x; |
| 381 | } |
| 382 | |
| 383 | if (NULL == p) { |
| 384 | fRoot = x; |
| 385 | x->fColor = kBlack_Color; |
| 386 | x->fParent = NULL; |
| 387 | GrAssert(1 == fCount); |
| 388 | return Iter(x, this); |
| 389 | } |
| 390 | p->fChildren[pc] = x; |
| 391 | x->fColor = kRed_Color; |
| 392 | x->fParent = p; |
| 393 | |
| 394 | do { |
| 395 | // assumptions at loop start. |
| 396 | GrAssert(NULL != x); |
| 397 | GrAssert(kRed_Color == x->fColor); |
| 398 | // can't have a grandparent but no parent. |
| 399 | GrAssert(!(NULL != gp && NULL == p)); |
| 400 | // make sure pc and gpc are correct |
| 401 | GrAssert(NULL == p || p->fChildren[pc] == x); |
| 402 | GrAssert(NULL == gp || gp->fChildren[gpc] == p); |
| 403 | |
| 404 | // if x's parent is black then we didn't violate any of the |
| 405 | // red/black properties when we added x as red. |
| 406 | if (kBlack_Color == p->fColor) { |
| 407 | return Iter(x, this); |
| 408 | } |
| 409 | // gp must be valid because if p was the root then it is black |
| 410 | GrAssert(NULL != gp); |
| 411 | // gp must be black since it's child, p, is red. |
| 412 | GrAssert(kBlack_Color == gp->fColor); |
| 413 | |
| 414 | |
| 415 | // x and its parent are red, violating red-black property. |
| 416 | Node* u = gp->fChildren[1-gpc]; |
| 417 | // if x's uncle (p's sibling) is also red then we can flip |
| 418 | // p and u to black and make gp red. But then we have to recurse |
| 419 | // up to gp since it's parent may also be red. |
| 420 | if (NULL != u && kRed_Color == u->fColor) { |
| 421 | p->fColor = kBlack_Color; |
| 422 | u->fColor = kBlack_Color; |
| 423 | gp->fColor = kRed_Color; |
| 424 | x = gp; |
| 425 | p = x->fParent; |
| 426 | if (NULL == p) { |
| 427 | // x (prev gp) is the root, color it black and be done. |
| 428 | GrAssert(fRoot == x); |
| 429 | x->fColor = kBlack_Color; |
| 430 | validate(); |
| 431 | return Iter(x, this); |
| 432 | } |
| 433 | gp = p->fParent; |
| 434 | pc = (p->fChildren[kLeft_Child] == x) ? kLeft_Child : |
| 435 | kRight_Child; |
| 436 | if (NULL != gp) { |
| 437 | gpc = (gp->fChildren[kLeft_Child] == p) ? kLeft_Child : |
| 438 | kRight_Child; |
| 439 | } |
| 440 | continue; |
| 441 | } break; |
| 442 | } while (true); |
| 443 | // Here p is red but u is black and we still have to resolve the fact |
| 444 | // that x and p are both red. |
| 445 | GrAssert(NULL == gp->fChildren[1-gpc] || kBlack_Color == gp->fChildren[1-gpc]->fColor); |
| 446 | GrAssert(kRed_Color == x->fColor); |
| 447 | GrAssert(kRed_Color == p->fColor); |
| 448 | GrAssert(kBlack_Color == gp->fColor); |
| 449 | |
| 450 | // make x be on the same side of p as p is of gp. If it isn't already |
| 451 | // the case then rotate x up to p and swap their labels. |
| 452 | if (pc != gpc) { |
| 453 | if (kRight_Child == pc) { |
| 454 | rotateLeft(p); |
| 455 | Node* temp = p; |
| 456 | p = x; |
| 457 | x = temp; |
| 458 | pc = kLeft_Child; |
| 459 | } else { |
| 460 | rotateRight(p); |
| 461 | Node* temp = p; |
| 462 | p = x; |
| 463 | x = temp; |
| 464 | pc = kRight_Child; |
| 465 | } |
| 466 | } |
| 467 | // we now rotate gp down, pulling up p to be it's new parent. |
| 468 | // gp's child, u, that is not affected we know to be black. gp's new |
| 469 | // child is p's previous child (x's pre-rotation sibling) which must be |
| 470 | // black since p is red. |
| 471 | GrAssert(NULL == p->fChildren[1-pc] || |
| 472 | kBlack_Color == p->fChildren[1-pc]->fColor); |
| 473 | // Since gp's two children are black it can become red if p is made |
| 474 | // black. This leaves the black-height of both of p's new subtrees |
| 475 | // preserved and removes the red/red parent child relationship. |
| 476 | p->fColor = kBlack_Color; |
| 477 | gp->fColor = kRed_Color; |
| 478 | if (kLeft_Child == pc) { |
| 479 | rotateRight(gp); |
| 480 | } else { |
| 481 | rotateLeft(gp); |
| 482 | } |
| 483 | validate(); |
| 484 | return Iter(x, this); |
| 485 | } |
| 486 | |
| 487 | |
| 488 | template <typename T, typename C> |
| 489 | void GrRedBlackTree<T,C>::rotateRight(Node* n) { |
| 490 | /* d? d? |
| 491 | * / / |
| 492 | * n s |
| 493 | * / \ ---> / \ |
| 494 | * s a? c? n |
| 495 | * / \ / \ |
| 496 | * c? b? b? a? |
| 497 | */ |
| 498 | Node* d = n->fParent; |
| 499 | Node* s = n->fChildren[kLeft_Child]; |
| 500 | GrAssert(NULL != s); |
| 501 | Node* b = s->fChildren[kRight_Child]; |
| 502 | |
| 503 | if (NULL != d) { |
| 504 | Child c = d->fChildren[kLeft_Child] == n ? kLeft_Child : |
| 505 | kRight_Child; |
| 506 | d->fChildren[c] = s; |
| 507 | } else { |
| 508 | GrAssert(fRoot == n); |
| 509 | fRoot = s; |
| 510 | } |
| 511 | s->fParent = d; |
| 512 | s->fChildren[kRight_Child] = n; |
| 513 | n->fParent = s; |
| 514 | n->fChildren[kLeft_Child] = b; |
| 515 | if (NULL != b) { |
| 516 | b->fParent = n; |
| 517 | } |
| 518 | |
| 519 | GR_DEBUGASSERT(validateChildRelations(d, true)); |
| 520 | GR_DEBUGASSERT(validateChildRelations(s, true)); |
| 521 | GR_DEBUGASSERT(validateChildRelations(n, false)); |
| 522 | GR_DEBUGASSERT(validateChildRelations(n->fChildren[kRight_Child], true)); |
| 523 | GR_DEBUGASSERT(validateChildRelations(b, true)); |
| 524 | GR_DEBUGASSERT(validateChildRelations(s->fChildren[kLeft_Child], true)); |
| 525 | } |
| 526 | |
| 527 | template <typename T, typename C> |
| 528 | void GrRedBlackTree<T,C>::rotateLeft(Node* n) { |
| 529 | |
| 530 | Node* d = n->fParent; |
| 531 | Node* s = n->fChildren[kRight_Child]; |
| 532 | GrAssert(NULL != s); |
| 533 | Node* b = s->fChildren[kLeft_Child]; |
| 534 | |
| 535 | if (NULL != d) { |
| 536 | Child c = d->fChildren[kRight_Child] == n ? kRight_Child : |
| 537 | kLeft_Child; |
| 538 | d->fChildren[c] = s; |
| 539 | } else { |
| 540 | GrAssert(fRoot == n); |
| 541 | fRoot = s; |
| 542 | } |
| 543 | s->fParent = d; |
| 544 | s->fChildren[kLeft_Child] = n; |
| 545 | n->fParent = s; |
| 546 | n->fChildren[kRight_Child] = b; |
| 547 | if (NULL != b) { |
| 548 | b->fParent = n; |
| 549 | } |
| 550 | |
| 551 | GR_DEBUGASSERT(validateChildRelations(d, true)); |
| 552 | GR_DEBUGASSERT(validateChildRelations(s, true)); |
| 553 | GR_DEBUGASSERT(validateChildRelations(n, true)); |
| 554 | GR_DEBUGASSERT(validateChildRelations(n->fChildren[kLeft_Child], true)); |
| 555 | GR_DEBUGASSERT(validateChildRelations(b, true)); |
| 556 | GR_DEBUGASSERT(validateChildRelations(s->fChildren[kRight_Child], true)); |
| 557 | } |
| 558 | |
| 559 | template <typename T, typename C> |
| 560 | typename GrRedBlackTree<T,C>::Node* GrRedBlackTree<T,C>::SuccessorNode(Node* x) { |
| 561 | GrAssert(NULL != x); |
| 562 | if (NULL != x->fChildren[kRight_Child]) { |
| 563 | x = x->fChildren[kRight_Child]; |
| 564 | while (NULL != x->fChildren[kLeft_Child]) { |
| 565 | x = x->fChildren[kLeft_Child]; |
| 566 | } |
| 567 | return x; |
| 568 | } |
| 569 | while (NULL != x->fParent && x == x->fParent->fChildren[kRight_Child]) { |
| 570 | x = x->fParent; |
| 571 | } |
| 572 | return x->fParent; |
| 573 | } |
| 574 | |
| 575 | template <typename T, typename C> |
| 576 | typename GrRedBlackTree<T,C>::Node* GrRedBlackTree<T,C>::PredecessorNode(Node* x) { |
| 577 | GrAssert(NULL != x); |
| 578 | if (NULL != x->fChildren[kLeft_Child]) { |
| 579 | x = x->fChildren[kLeft_Child]; |
| 580 | while (NULL != x->fChildren[kRight_Child]) { |
| 581 | x = x->fChildren[kRight_Child]; |
| 582 | } |
| 583 | return x; |
| 584 | } |
| 585 | while (NULL != x->fParent && x == x->fParent->fChildren[kLeft_Child]) { |
| 586 | x = x->fParent; |
| 587 | } |
| 588 | return x->fParent; |
| 589 | } |
| 590 | |
| 591 | template <typename T, typename C> |
| 592 | void GrRedBlackTree<T,C>::deleteAtNode(Node* x) { |
| 593 | GrAssert(NULL != x); |
| 594 | validate(); |
| 595 | --fCount; |
| 596 | |
| 597 | bool hasLeft = NULL != x->fChildren[kLeft_Child]; |
| 598 | bool hasRight = NULL != x->fChildren[kRight_Child]; |
| 599 | Child c = hasLeft ? kLeft_Child : kRight_Child; |
| 600 | |
| 601 | if (hasLeft && hasRight) { |
| 602 | // first and last can't have two children. |
| 603 | GrAssert(fFirst != x); |
| 604 | GrAssert(fLast != x); |
| 605 | // if x is an interior node then we find it's successor |
| 606 | // and swap them. |
| 607 | Node* s = x->fChildren[kRight_Child]; |
| 608 | while (NULL != s->fChildren[kLeft_Child]) { |
| 609 | s = s->fChildren[kLeft_Child]; |
| 610 | } |
| 611 | GrAssert(NULL != s); |
| 612 | // this might be expensive relative to swapping node ptrs around. |
| 613 | // depends on T. |
| 614 | x->fItem = s->fItem; |
| 615 | x = s; |
| 616 | c = kRight_Child; |
| 617 | } else if (NULL == x->fParent) { |
| 618 | // if x was the root we just replace it with its child and make |
| 619 | // the new root (if the tree is not empty) black. |
| 620 | GrAssert(fRoot == x); |
| 621 | fRoot = x->fChildren[c]; |
| 622 | if (NULL != fRoot) { |
| 623 | fRoot->fParent = NULL; |
| 624 | fRoot->fColor = kBlack_Color; |
| 625 | if (x == fLast) { |
| 626 | GrAssert(c == kLeft_Child); |
| 627 | fLast = fRoot; |
| 628 | } else if (x == fFirst) { |
| 629 | GrAssert(c == kRight_Child); |
| 630 | fFirst = fRoot; |
| 631 | } |
| 632 | } else { |
| 633 | GrAssert(fFirst == fLast && x == fFirst); |
| 634 | fFirst = NULL; |
| 635 | fLast = NULL; |
| 636 | GrAssert(0 == fCount); |
| 637 | } |
| 638 | delete x; |
| 639 | validate(); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | Child pc; |
| 644 | Node* p = x->fParent; |
| 645 | pc = p->fChildren[kLeft_Child] == x ? kLeft_Child : kRight_Child; |
| 646 | |
| 647 | if (NULL == x->fChildren[c]) { |
| 648 | if (fLast == x) { |
| 649 | fLast = p; |
| 650 | GrAssert(p == PredecessorNode(x)); |
| 651 | } else if (fFirst == x) { |
| 652 | fFirst = p; |
| 653 | GrAssert(p == SuccessorNode(x)); |
| 654 | } |
| 655 | // x has two implicit black children. |
| 656 | Color xcolor = x->fColor; |
| 657 | p->fChildren[pc] = NULL; |
| 658 | delete x; |
| 659 | // when x is red it can be with an implicit black leaf without |
| 660 | // violating any of the red-black tree properties. |
| 661 | if (kRed_Color == xcolor) { |
| 662 | validate(); |
| 663 | return; |
| 664 | } |
| 665 | // s is p's other child (x's sibling) |
| 666 | Node* s = p->fChildren[1-pc]; |
| 667 | |
| 668 | //s cannot be an implicit black node because the original |
| 669 | // black-height at x was >= 2 and s's black-height must equal the |
| 670 | // initial black height of x. |
| 671 | GrAssert(NULL != s); |
| 672 | GrAssert(p == s->fParent); |
| 673 | |
| 674 | // assigned in loop |
| 675 | Node* sl; |
| 676 | Node* sr; |
| 677 | bool slRed; |
| 678 | bool srRed; |
| 679 | |
| 680 | do { |
| 681 | // When we start this loop x may already be deleted it is/was |
| 682 | // p's child on its pc side. x's children are/were black. The |
| 683 | // first time through the loop they are implict children. |
| 684 | // On later passes we will be walking up the tree and they will |
| 685 | // be real nodes. |
| 686 | // The x side of p has a black-height that is one less than the |
| 687 | // s side. It must be rebalanced. |
| 688 | GrAssert(NULL != s); |
| 689 | GrAssert(p == s->fParent); |
| 690 | GrAssert(NULL == x || x->fParent == p); |
| 691 | |
| 692 | //sl and sr are s's children, which may be implicit. |
| 693 | sl = s->fChildren[kLeft_Child]; |
| 694 | sr = s->fChildren[kRight_Child]; |
| 695 | |
| 696 | // if the s is red we will rotate s and p, swap their colors so |
| 697 | // that x's new sibling is black |
| 698 | if (kRed_Color == s->fColor) { |
| 699 | // if s is red then it's parent must be black. |
| 700 | GrAssert(kBlack_Color == p->fColor); |
| 701 | // s's children must also be black since s is red. They can't |
| 702 | // be implicit since s is red and it's black-height is >= 2. |
| 703 | GrAssert(NULL != sl && kBlack_Color == sl->fColor); |
| 704 | GrAssert(NULL != sr && kBlack_Color == sr->fColor); |
| 705 | p->fColor = kRed_Color; |
| 706 | s->fColor = kBlack_Color; |
| 707 | if (kLeft_Child == pc) { |
| 708 | rotateLeft(p); |
| 709 | s = sl; |
| 710 | } else { |
| 711 | rotateRight(p); |
| 712 | s = sr; |
| 713 | } |
| 714 | sl = s->fChildren[kLeft_Child]; |
| 715 | sr = s->fChildren[kRight_Child]; |
| 716 | } |
| 717 | // x and s are now both black. |
| 718 | GrAssert(kBlack_Color == s->fColor); |
| 719 | GrAssert(kBlack_Color == x->fColor); |
| 720 | GrAssert(p == s->fParent); |
| 721 | GrAssert(p == x->fParent); |
| 722 | |
| 723 | // when x is deleted its subtree will have reduced black-height. |
| 724 | slRed = (NULL != sl && kRed_Color == sl->fColor); |
| 725 | srRed = (NULL != sr && kRed_Color == sr->fColor); |
| 726 | if (!slRed && !srRed) { |
| 727 | // if s can be made red that will balance out x's removal |
| 728 | // to make both subtrees of p have the same black-height. |
| 729 | if (kBlack_Color == p->fColor) { |
| 730 | s->fColor = kRed_Color; |
| 731 | // now subtree at p has black-height of one less than |
| 732 | // p's parent's other child's subtree. We move x up to |
| 733 | // p and go through the loop again. At the top of loop |
| 734 | // we assumed x and x's children are black, which holds |
| 735 | // by above ifs. |
| 736 | // if p is the root there is no other subtree to balance |
| 737 | // against. |
| 738 | x = p; |
| 739 | p = x->fParent; |
| 740 | if (NULL == p) { |
| 741 | GrAssert(fRoot == x); |
| 742 | validate(); |
| 743 | return; |
| 744 | } else { |
| 745 | pc = p->fChildren[kLeft_Child] == x ? kLeft_Child : |
| 746 | kRight_Child; |
| 747 | |
| 748 | } |
| 749 | s = p->fChildren[1-pc]; |
| 750 | GrAssert(NULL != s); |
| 751 | GrAssert(p == s->fParent); |
| 752 | continue; |
| 753 | } else if (kRed_Color == p->fColor) { |
| 754 | // we can make p black and s red. This balance out p's |
| 755 | // two subtrees and keep the same black-height as it was |
| 756 | // before the delete. |
| 757 | s->fColor = kRed_Color; |
| 758 | p->fColor = kBlack_Color; |
| 759 | validate(); |
| 760 | return; |
| 761 | } |
| 762 | } |
| 763 | break; |
| 764 | } while (true); |
| 765 | // if we made it here one or both of sl and sr is red. |
| 766 | // s and x are black. We make sure that a red child is on |
| 767 | // the same side of s as s is of p. |
| 768 | GrAssert(slRed || srRed); |
| 769 | if (kLeft_Child == pc && !srRed) { |
| 770 | s->fColor = kRed_Color; |
| 771 | sl->fColor = kBlack_Color; |
| 772 | rotateRight(s); |
| 773 | sr = s; |
| 774 | s = sl; |
| 775 | //sl = s->fChildren[kLeft_Child]; don't need this |
| 776 | } else if (kRight_Child == pc && !slRed) { |
| 777 | s->fColor = kRed_Color; |
| 778 | sr->fColor = kBlack_Color; |
| 779 | rotateLeft(s); |
| 780 | sl = s; |
| 781 | s = sr; |
| 782 | //sr = s->fChildren[kRight_Child]; don't need this |
| 783 | } |
| 784 | // now p is either red or black, x and s are red and s's 1-pc |
| 785 | // child is red. |
| 786 | // We rotate p towards x, pulling s up to replace p. We make |
| 787 | // p be black and s takes p's old color. |
| 788 | // Whether p was red or black, we've increased its pc subtree |
| 789 | // rooted at x by 1 (balancing the imbalance at the start) and |
| 790 | // we've also its subtree rooted at s's black-height by 1. This |
| 791 | // can be balanced by making s's red child be black. |
| 792 | s->fColor = p->fColor; |
| 793 | p->fColor = kBlack_Color; |
| 794 | if (kLeft_Child == pc) { |
| 795 | GrAssert(NULL != sr && kRed_Color == sr->fColor); |
| 796 | sr->fColor = kBlack_Color; |
| 797 | rotateLeft(p); |
| 798 | } else { |
| 799 | GrAssert(NULL != sl && kRed_Color == sl->fColor); |
| 800 | sl->fColor = kBlack_Color; |
| 801 | rotateRight(p); |
| 802 | } |
| 803 | } |
| 804 | else { |
| 805 | // x has exactly one implicit black child. x cannot be red. |
| 806 | // Proof by contradiction: Assume X is red. Let c0 be x's implicit |
| 807 | // child and c1 be its non-implicit child. c1 must be black because |
| 808 | // red nodes always have two black children. Then the two subtrees |
| 809 | // of x rooted at c0 and c1 will have different black-heights. |
| 810 | GrAssert(kBlack_Color == x->fColor); |
| 811 | // So we know x is black and has one implicit black child, c0. c1 |
| 812 | // must be red, otherwise the subtree at c1 will have a different |
| 813 | // black-height than the subtree rooted at c0. |
| 814 | GrAssert(kRed_Color == x->fChildren[c]->fColor); |
| 815 | // replace x with c1, making c1 black, preserves all red-black tree |
| 816 | // props. |
| 817 | Node* c1 = x->fChildren[c]; |
| 818 | if (x == fFirst) { |
| 819 | GrAssert(c == kRight_Child); |
| 820 | fFirst = c1; |
| 821 | while (NULL != fFirst->fChildren[kLeft_Child]) { |
| 822 | fFirst = fFirst->fChildren[kLeft_Child]; |
| 823 | } |
| 824 | GrAssert(fFirst == SuccessorNode(x)); |
| 825 | } else if (x == fLast) { |
| 826 | GrAssert(c == kLeft_Child); |
| 827 | fLast = c1; |
| 828 | while (NULL != fLast->fChildren[kRight_Child]) { |
| 829 | fLast = fLast->fChildren[kRight_Child]; |
| 830 | } |
| 831 | GrAssert(fLast == PredecessorNode(x)); |
| 832 | } |
| 833 | c1->fParent = p; |
| 834 | p->fChildren[pc] = c1; |
| 835 | c1->fColor = kBlack_Color; |
| 836 | delete x; |
| 837 | validate(); |
| 838 | } |
| 839 | validate(); |
| 840 | } |
| 841 | |
| 842 | template <typename T, typename C> |
| 843 | void GrRedBlackTree<T,C>::RecursiveDelete(Node* x) { |
| 844 | if (NULL != x) { |
| 845 | RecursiveDelete(x->fChildren[kLeft_Child]); |
| 846 | RecursiveDelete(x->fChildren[kRight_Child]); |
| 847 | delete x; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | #if GR_DEBUG |
| 852 | template <typename T, typename C> |
| 853 | void GrRedBlackTree<T,C>::validate() const { |
| 854 | if (fCount) { |
| 855 | GrAssert(NULL == fRoot->fParent); |
| 856 | GrAssert(NULL != fFirst); |
| 857 | GrAssert(NULL != fLast); |
| 858 | |
| 859 | GrAssert(kBlack_Color == fRoot->fColor); |
| 860 | if (1 == fCount) { |
| 861 | GrAssert(fFirst == fRoot); |
| 862 | GrAssert(fLast == fRoot); |
| 863 | GrAssert(0 == fRoot->fChildren[kLeft_Child]); |
| 864 | GrAssert(0 == fRoot->fChildren[kRight_Child]); |
| 865 | } |
| 866 | } else { |
| 867 | GrAssert(NULL == fRoot); |
| 868 | GrAssert(NULL == fFirst); |
| 869 | GrAssert(NULL == fLast); |
| 870 | } |
| 871 | #if DEEP_VALIDATE |
| 872 | int bh; |
| 873 | int count = checkNode(fRoot, &bh); |
| 874 | GrAssert(count == fCount); |
| 875 | #endif |
| 876 | } |
| 877 | |
| 878 | template <typename T, typename C> |
| 879 | int GrRedBlackTree<T,C>::checkNode(Node* n, int* bh) const { |
| 880 | if (NULL != n) { |
| 881 | GrAssert(validateChildRelations(n, false)); |
| 882 | if (kBlack_Color == n->fColor) { |
| 883 | *bh += 1; |
| 884 | } |
| 885 | GrAssert(!fComp(n->fItem, fFirst->fItem)); |
| 886 | GrAssert(!fComp(fLast->fItem, n->fItem)); |
| 887 | int leftBh = *bh; |
| 888 | int rightBh = *bh; |
| 889 | int cl = checkNode(n->fChildren[kLeft_Child], &leftBh); |
| 890 | int cr = checkNode(n->fChildren[kRight_Child], &rightBh); |
| 891 | GrAssert(leftBh == rightBh); |
| 892 | *bh = leftBh; |
| 893 | return 1 + cl + cr; |
| 894 | } |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | template <typename T, typename C> |
| 899 | bool GrRedBlackTree<T,C>::validateChildRelations(const Node* n, |
| 900 | bool allowRedRed) const { |
| 901 | if (NULL != n) { |
| 902 | if (NULL != n->fChildren[kLeft_Child] || |
| 903 | NULL != n->fChildren[kRight_Child]) { |
| 904 | if (n->fChildren[kLeft_Child] == n->fChildren[kRight_Child]) { |
| 905 | return validateChildRelationsFailed(); |
| 906 | } |
| 907 | if (n->fChildren[kLeft_Child] == n->fParent && |
| 908 | NULL != n->fParent) { |
| 909 | return validateChildRelationsFailed(); |
| 910 | } |
| 911 | if (n->fChildren[kRight_Child] == n->fParent && |
| 912 | NULL != n->fParent) { |
| 913 | return validateChildRelationsFailed(); |
| 914 | } |
| 915 | if (NULL != n->fChildren[kLeft_Child]) { |
| 916 | if (!allowRedRed && |
| 917 | kRed_Color == n->fChildren[kLeft_Child]->fColor && |
| 918 | kRed_Color == n->fColor) { |
| 919 | return validateChildRelationsFailed(); |
| 920 | } |
| 921 | if (n->fChildren[kLeft_Child]->fParent != n) { |
| 922 | return validateChildRelationsFailed(); |
| 923 | } |
| 924 | if (!(fComp(n->fChildren[kLeft_Child]->fItem, n->fItem) || |
| 925 | (!fComp(n->fChildren[kLeft_Child]->fItem, n->fItem) && |
| 926 | !fComp(n->fItem, n->fChildren[kLeft_Child]->fItem)))) { |
| 927 | return validateChildRelationsFailed(); |
| 928 | } |
| 929 | } |
| 930 | if (NULL != n->fChildren[kRight_Child]) { |
| 931 | if (!allowRedRed && |
| 932 | kRed_Color == n->fChildren[kRight_Child]->fColor && |
| 933 | kRed_Color == n->fColor) { |
| 934 | return validateChildRelationsFailed(); |
| 935 | } |
| 936 | if (n->fChildren[kRight_Child]->fParent != n) { |
| 937 | return validateChildRelationsFailed(); |
| 938 | } |
| 939 | if (!(fComp(n->fItem, n->fChildren[kRight_Child]->fItem) || |
| 940 | (!fComp(n->fChildren[kRight_Child]->fItem, n->fItem) && |
| 941 | !fComp(n->fItem, n->fChildren[kRight_Child]->fItem)))) { |
| 942 | return validateChildRelationsFailed(); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | return true; |
| 948 | } |
| 949 | #endif |
| 950 | |
| 951 | #include "GrRandom.h" |
| 952 | |
| 953 | template <typename T, typename C> |
| 954 | void GrRedBlackTree<T,C>::UnitTest() { |
| 955 | GrRedBlackTree<int> tree; |
| 956 | typedef GrRedBlackTree<int>::Iter iter; |
| 957 | |
| 958 | GrRandom r; |
| 959 | |
| 960 | int count[100] = {0}; |
| 961 | // add 10K ints |
| 962 | for (int i = 0; i < 10000; ++i) { |
| 963 | int x = r.nextU()%100; |
| 964 | Iter xi = tree.insert(x); |
| 965 | GrAssert(*xi == x); |
| 966 | ++count[x]; |
| 967 | } |
| 968 | |
| 969 | tree.insert(0); |
| 970 | ++count[0]; |
| 971 | tree.insert(99); |
| 972 | ++count[99]; |
| 973 | GrAssert(*tree.begin() == 0); |
| 974 | GrAssert(*tree.last() == 99); |
| 975 | GrAssert(--(++tree.begin()) == tree.begin()); |
| 976 | GrAssert(--tree.end() == tree.last()); |
| 977 | GrAssert(tree.count() == 10002); |
| 978 | |
| 979 | int c = 0; |
| 980 | // check that we iterate through the correct number of |
| 981 | // elements and they are properly sorted. |
| 982 | for (Iter a = tree.begin(); tree.end() != a; ++a) { |
| 983 | Iter b = a; |
| 984 | ++b; |
| 985 | ++c; |
| 986 | GrAssert(b == tree.end() || *a <= *b); |
| 987 | } |
| 988 | GrAssert(c == tree.count()); |
| 989 | |
| 990 | // check that the tree reports the correct number of each int |
| 991 | // and that we can iterate through them correctly both forward |
| 992 | // and backward. |
| 993 | for (int i = 0; i < 100; ++i) { |
| 994 | int c; |
| 995 | c = tree.countOf(i); |
| 996 | GrAssert(c == count[i]); |
| 997 | c = 0; |
| 998 | Iter iter = tree.findFirst(i); |
| 999 | while (iter != tree.end() && *iter == i) { |
| 1000 | ++c; |
| 1001 | ++iter; |
| 1002 | } |
| 1003 | GrAssert(count[i] == c); |
| 1004 | c = 0; |
| 1005 | iter = tree.findLast(i); |
| 1006 | if (iter != tree.end()) { |
| 1007 | do { |
| 1008 | if (*iter == i) { |
| 1009 | ++c; |
| 1010 | } else { |
| 1011 | break; |
| 1012 | } |
| 1013 | if (iter != tree.begin()) { |
| 1014 | --iter; |
| 1015 | } else { |
| 1016 | break; |
| 1017 | } |
| 1018 | } while (true); |
| 1019 | } |
| 1020 | GrAssert(c == count[i]); |
| 1021 | } |
| 1022 | // remove all the ints between 25 and 74. Randomly chose to remove |
| 1023 | // the first, last, or any entry for each. |
| 1024 | for (int i = 25; i < 75; ++i) { |
| 1025 | while (0 != tree.countOf(i)) { |
| 1026 | --count[i]; |
| 1027 | int x = r.nextU() % 3; |
| 1028 | Iter iter; |
| 1029 | switch (x) { |
| 1030 | case 0: |
| 1031 | iter = tree.findFirst(i); |
| 1032 | break; |
| 1033 | case 1: |
| 1034 | iter = tree.findLast(i); |
| 1035 | break; |
| 1036 | case 2: |
| 1037 | default: |
| 1038 | iter = tree.find(i); |
| 1039 | break; |
| 1040 | } |
| 1041 | tree.remove(iter); |
| 1042 | } |
| 1043 | GrAssert(0 == count[i]); |
| 1044 | GrAssert(tree.findFirst(i) == tree.end()); |
| 1045 | GrAssert(tree.findLast(i) == tree.end()); |
| 1046 | GrAssert(tree.find(i) == tree.end()); |
| 1047 | } |
| 1048 | // remove all of the 0 entries. (tests removing begin()) |
| 1049 | GrAssert(*tree.begin() == 0); |
| 1050 | GrAssert(*(--tree.end()) == 99); |
| 1051 | while (0 != tree.countOf(0)) { |
| 1052 | --count[0]; |
| 1053 | tree.remove(tree.find(0)); |
| 1054 | } |
| 1055 | GrAssert(0 == count[0]); |
| 1056 | GrAssert(tree.findFirst(0) == tree.end()); |
| 1057 | GrAssert(tree.findLast(0) == tree.end()); |
| 1058 | GrAssert(tree.find(0) == tree.end()); |
| 1059 | GrAssert(0 < *tree.begin()); |
| 1060 | |
| 1061 | // remove all the 99 entries (tests removing last()). |
| 1062 | while (0 != tree.countOf(99)) { |
| 1063 | --count[99]; |
| 1064 | tree.remove(tree.find(99)); |
| 1065 | } |
| 1066 | GrAssert(0 == count[99]); |
| 1067 | GrAssert(tree.findFirst(99) == tree.end()); |
| 1068 | GrAssert(tree.findLast(99) == tree.end()); |
| 1069 | GrAssert(tree.find(99) == tree.end()); |
| 1070 | GrAssert(99 > *(--tree.end())); |
| 1071 | GrAssert(tree.last() == --tree.end()); |
| 1072 | |
| 1073 | // Make sure iteration still goes through correct number of entries |
| 1074 | // and is still sorted correctly. |
| 1075 | c = 0; |
| 1076 | for (Iter a = tree.begin(); tree.end() != a; ++a) { |
| 1077 | Iter b = a; |
| 1078 | ++b; |
| 1079 | ++c; |
| 1080 | GrAssert(b == tree.end() || *a <= *b); |
| 1081 | } |
| 1082 | GrAssert(c == tree.count()); |
| 1083 | |
| 1084 | // repeat check that correct number of each entry is in the tree |
| 1085 | // and iterates correctly both forward and backward. |
| 1086 | for (int i = 0; i < 100; ++i) { |
| 1087 | GrAssert(tree.countOf(i) == count[i]); |
| 1088 | int c = 0; |
| 1089 | Iter iter = tree.findFirst(i); |
| 1090 | while (iter != tree.end() && *iter == i) { |
| 1091 | ++c; |
| 1092 | ++iter; |
| 1093 | } |
| 1094 | GrAssert(count[i] == c); |
| 1095 | c = 0; |
| 1096 | iter = tree.findLast(i); |
| 1097 | if (iter != tree.end()) { |
| 1098 | do { |
| 1099 | if (*iter == i) { |
| 1100 | ++c; |
| 1101 | } else { |
| 1102 | break; |
| 1103 | } |
| 1104 | if (iter != tree.begin()) { |
| 1105 | --iter; |
| 1106 | } else { |
| 1107 | break; |
| 1108 | } |
| 1109 | } while (true); |
| 1110 | } |
| 1111 | GrAssert(count[i] == c); |
| 1112 | } |
| 1113 | |
| 1114 | // remove all entries |
| 1115 | while (!tree.empty()) { |
| 1116 | tree.remove(tree.begin()); |
| 1117 | } |
| 1118 | |
| 1119 | // test reset on empty tree. |
| 1120 | tree.reset(); |
| 1121 | } |
| 1122 | |
| 1123 | #endif |