BFLibCPP 0.1
CPP Library
Loading...
Searching...
No Matches
rbtree.hpp
Go to the documentation of this file.
1
6#ifndef RBTREE_HPP
7#define RBTREE_HPP
8
9#include "access.hpp"
10#include "bintree.hpp"
11#include "delete.hpp"
12#include <iostream>
13
15#define kRBTreeNodeColorRed 0x00
16#define kRBTreeNodeColorBlack 0x01
17
21#define kRBTreeNodeMaskColor 0x0F
22
26#define kRBTreeNodeMaskColorCount 0xF0
27
28namespace BF {
29
35template <typename T, typename S = int> class RBTree : public BinTree<T,S> {
36public:
37 // TODO: rename rbnode to node
38 class RBNode : public BinTree<T,S>::BinNode {
39 friend class RBTree<T,S>;
40
41 public:
42 RBNode() : BinTree<T,S>::BinNode() {
43 this->_colorSpace = 0;
44
46 this->setColorCount(1);
47 }
48
49 virtual ~RBNode() {}
50
51 virtual bool isNull() const { return false; }
52 virtual const RBNode * left() const { return (const RBNode *) this->BinTree<T,S>::BinNode::left(); }
53 virtual const RBNode * right() const { return (const RBNode *) this->BinTree<T,S>::BinNode::right(); }
54
55 protected:
56
57 virtual void printObject() const { std::cout << this->_obj; }
58
59 virtual RBNode * left() { return (RBNode *) this->BinTree<T,S>::BinNode::left(); }
60 virtual RBNode * right() { return (RBNode *) this->BinTree<T,S>::BinNode::right(); }
61
62 virtual void setLeft(typename BinTree<T,S>::BinNode * left) { this->BinTree<T,S>::BinNode::setLeft(left); }
63 virtual void setRight(typename BinTree<T,S>::BinNode * right) { this->BinTree<T,S>::BinNode::setRight(right); }
64
65 void print() const {
66 int lvl = this->level();
67
68 for (int i = 1; i <= lvl; i++) {
69 if (i == lvl) {
70 if (!this->isRoot()) std::cout << " |";
71 std::cout << "----";
72 } else {
73 if (!this->isRoot()) {
74 if (this->childType() != 0) std::cout << " |";
75 else std::cout << " ";
76 }
77 std::cout << " ";
78 }
79 }
80
81 if (this->color() == kRBTreeNodeColorRed) std::cout << "\033[0;31m";
82
83 for (int i = 0; i < this->colorCount(); i++) std::cout << "[";
84 this->printObject();
85 for (int i = 0; i < this->colorCount(); i++) std::cout << "]";
86
87 if (this->color() == kRBTreeNodeColorRed) std::cout << "\033[0m";
88 std::cout << std::endl;
89 }
90
91 virtual unsigned char childCount() const {
92 RBNode * l = (RBNode *) this->left(),
93 * r = (RBNode *) this->right();
94
95 if (l->isNull() && r->isNull()) {
96 return 0;
97 } else if (!l->isNull() && !r->isNull()) {
98 return 2;
99 } else return 1;
100 }
101
102 virtual RBNode * clone() {
103 return new RBNode(*this);
104 }
105
106 virtual RBNode * grandParent() {
108 }
109
110 virtual RBNode * pibling() {
111 return (RBNode *) this->BinTree<T,S>::BinNode::pibling();
112 }
113
114 virtual RBNode * sibling() {
115 return (RBNode *) this->BinTree<T,S>::BinNode::sibling();
116 }
117
118 char color() const {
119 return this->_colorSpace & kRBTreeNodeMaskColor;
120 }
121
122 void setColor(char color) {
123 this->_colorSpace = (this->_colorSpace & ~kRBTreeNodeMaskColor) | (color & kRBTreeNodeMaskColor);
124 }
125
126 unsigned char colorCount() const {
127 return (this->_colorSpace & kRBTreeNodeMaskColorCount) >> 4;
128 }
129
130 void setColorCount(unsigned char count) {
131 this->_colorSpace = (this->_colorSpace & ~kRBTreeNodeMaskColorCount) | ((count << 4) & kRBTreeNodeMaskColorCount);
132 }
133
134 private:
135
139 unsigned char _colorSpace;
140 };
141
145 class RBNodeNull : public RBNode {
146 public:
150
151 virtual ~RBNodeNull() {}
152
153 virtual bool isNull() const { return true; }
154
155 virtual void printObject() const { std::cout << "_"; }
156
157 virtual RBNodeNull * clone() {
158 return new RBNodeNull(*this);
159 }
160 };
161
162 class RBNodeNonnull : public RBNode {
163 public:
166
167 this->setLeft(new RBNodeNull);
168 this->setRight(new RBNodeNull);
169 }
170
171 virtual ~RBNodeNonnull() {}
172
173 // Identifies if RBNodeNonnull is a null type node
174 virtual bool isNull() const { return false; }
175
176 virtual RBNodeNonnull * clone() {
177 return new RBNodeNonnull(*this);
178 }
179
180 protected:
181
182 virtual void setLeft(typename BinTree<T,S>::BinNode * left) {
183 if (left) this->RBNode::setLeft(left);
184 else {
185 this->RBNode::setLeft(new RBNodeNull);
186 this->left()->_location = this->leftAddr();
187 this->left()->_parent = this;
188 }
189 }
190
191 virtual void setRight(typename BinTree<T,S>::BinNode * right) {
192 if (right) this->RBNode::setRight(right);
193 else {
194 this->RBNode::setRight(new RBNodeNull);
195 this->right()->_location = this->rightAddr();
196 this->right()->_parent = this;
197 }
198 }
199 };
200
201 class Iterator : public BinTree<T,S>::Iterator {
202 friend class RBTree<T,S>;
203 protected:
204 Iterator() : BinTree<T,S>::Iterator() {}
205
209 int setCurrent(typename BinTree<T,S>::BinNode * bnode) {
210 RBNode * node = (RBNode *) bnode;
211 if (!node) return 1;
212 else if (node->isNull()) return 0;
213 else {
214 int result = this->_st.push(node);
215
216 if (!result && !node->left()->isNull())
217 return this->setCurrent(node->left());
218 else return result;
219 }
220 }
221 };
222
223 RBTree() : BinTree<T,S>() {}
224
225 virtual ~RBTree() {}
226
227 virtual const RBNode * root() const { return (const RBNode *) this->BinTree<T,S>::root(); }
228
229 virtual const RBNode * getNodeForObject(T obj) {
230 return (const RBNode *) this->BinTree<T,S>::getNodeForObject(obj, this->root());
231 }
232
236 virtual int insert(T obj) {
237 RBNode * newNode = (RBNode *) this->createNode();
238 newNode->_obj = obj;
239
240 if (this->root()) {
241 return this->insert(newNode, this->root());
242 } else {
244 this->setRoot(newNode);
245 this->root()->_location = this->rootAddr();
246 this->setNodeLocation(this->root(), this->rootAddr());
247 this->_count++;
248 return 0;
249 }
250 }
251
260 virtual int remove(T obj) {
261 RBNode * node = (RBNode *) this->BinTree<T,S>::getNodeForObject(obj, (typename BinTree<T,S>::BinNode *) this->root());
262
263 if (!node) return 101;
264 else return this->deleteNode(node);
265 }
266
267 virtual void print() {
268 this->print(false);
269 }
270
274 virtual void print(bool printNullNodes) {
275 std::cout << std::endl;
276 this->print(this->root(), printNullNodes);
277 std::cout << std::endl;
278 }
279
285 int deleteNode(const RBNode * node) {
286 return this->deleteNode((typename BinTree<T,S>::BinNode *) node);
287 }
288
289 virtual const RBNode * minNode() const {
290 return (const RBNode *) this->minNode(this->root());
291 }
292
293 virtual const RBNode * maxNode() const {
294 return (const RBNode *) this->maxNode(this->root());
295 }
296
297 virtual int createIterator(Iterator ** itr) {
298 int result = 0;
299 if (!itr) return 9;
300 else {
301 *itr = new RBTree<T,S>::Iterator;
302 result = (*itr)->setCurrent(this->root());
303 }
304 return result;
305 }
306
307private:
308
309 virtual RBNode * root() { return (RBNode *) this->BinTree<T,S>::root(); }
310
311 virtual void * createNode() {
312 return new RBNodeNonnull;
313 }
314
318 virtual int deleteNode(typename BinTree<T,S>::BinNode * bnode) {
319 RBNode * node = (RBNode *) bnode;
320 bool isRoot = node->isRoot();
321
322 // The dbLocation will have the node we will need to interrogate if it
323 // has a double black node
324 RBNode * max = (RBNode *) this->maxNode(node->left());
325
326 // If max has a child, it would only have a right child
327 RBNode * dbNode = (RBNode *) max->left();
328 if (max->isNull()) {
329 dbNode = max;
330 }
331
332 int result = this->removeNode(node);
333
334 if (result == 0) {
335 // We should have at least a null node object
336 if (!dbNode) result = 107;
337 else {
338 if (dbNode->colorCount() == 2) {
339 result = this->balanceRemoval(dbNode);
340 }
341 }
342 }
343
344 // If we are deleting the root node then we need to make
345 // sure the root remains black
346 if (result == 0) {
347 if (isRoot) {
348 if (RBTree<T,S>::isNodeRed(this->root())) {
350 }
351 }
352 }
353
354 // Delete null nodes if any
355 if (node->left())
356 if (node->left()->isNull()) {
357 RBNode * l = (RBNode *) node->left();
358 Delete(l);
359 }
360
361 if (node->right())
362 if ((node->right())->isNull()) {
363 RBNode * r = (RBNode *) node->right();
364 Delete(r);
365 }
366
367 // Delete node
368 Delete(node);
369
370 if (result == 0) {
371 this->_count--;
372 }
373
374 return result;
375 }
376
377 static bool isNodeBlack(const RBNode * node) {
378 if (!node) return true;
379 else if (node->isNull()) return true;
380 else return node->color() == kRBTreeNodeColorBlack;
381 }
382
383 static bool isNodeRed(const RBNode * node) {
384 if (!node) return false;
385 else if (node->isNull()) return false;
386 else return node->color() == kRBTreeNodeColorRed;
387 }
388
397 int insert(RBNode * newNode, RBNode * parent) {
398 int result = this->BinTree<T,S>::insert(newNode, parent);
399 if (result == 0) result = this->balanceInsertion(newNode);
400 return result;
401 }
402
403 virtual bool canNewNodeTakeNewLocation(typename BinTree<T,S>::BinNode ** newLocation) {
404 RBNode ** rbLocation = (RBNode **) newLocation;
405
406 if (!(*rbLocation)->isNull()) return false;
407 else {
408 Delete(*rbLocation);
409 return true;
410 }
411 }
412
416 int balanceInsertion(RBNode * node) {
417 RBNode * tmp = NULL;
418
419 if (!node) {
420 return 110;
421 } else if (RBTree<T>::isNodeBlack(node)) {
422 return 0;
423 } else if (RBTree<T>::isNodeBlack((RBNode *) node->_parent)) {
424 return 0;
425 } else if (RBTree<T>::isNodeRed((RBNode *) node->_parent)) {
426 // Involves rotations
427 if (RBTree<T>::isNodeBlack(node->pibling())) {
428 RBNode * parent = (RBNode *) node->_parent;
429 unsigned char rcase = this->rotationCase(node);
430
431 if ((rcase == ROTATION_CASE_RR) || (rcase == ROTATION_CASE_LL)) {
432 if ((tmp = (RBNode *) node->_parent)) {
433 tmp->setColor(kRBTreeNodeColorBlack);
434 }
435
436 if ((tmp = node->grandParent())) {
437 tmp->setColor(kRBTreeNodeColorRed);
438 }
439 }
440
441 int result = this->rotate(node, rcase);
442
443 if (result == 0) {
444 if ((rcase == ROTATION_CASE_RL) || (rcase == ROTATION_CASE_LR)) {
445 if ((tmp = (RBNode *) parent->_parent)) {
446 tmp->setColor(kRBTreeNodeColorBlack);
447 }
448
449 if ((tmp = parent->grandParent())) {
450 tmp->setColor(kRBTreeNodeColorRed);
451 }
452 }
453
454 // lr and rl need to handle ll and rr respectively after
455 // their rotation case
456 if (rcase == ROTATION_CASE_LR) {
457 result = this->rotate(parent, ROTATION_CASE_LL);
458 } else if (rcase == ROTATION_CASE_RL) {
459 result = this->rotate(parent, ROTATION_CASE_RR);
460 }
461 }
462
463 return result;
464
465 } else if (RBTree<T>::isNodeRed(node->pibling())) {
466 if (node->_parent) {
467 ((RBNode *) node->_parent)->setColor(kRBTreeNodeColorBlack);
468 }
469
470 if ((tmp = node->pibling()) != NULL) {
471 tmp->setColor(kRBTreeNodeColorBlack);
472 }
473
474 if ((tmp = node->grandParent()) != NULL) {
475 if (!tmp->isRoot()) tmp->setColor(kRBTreeNodeColorRed);
476 }
477
478 return this->balanceInsertion(tmp);
479 } else {
480 return 113;
481 }
482 } else {
483 return 114;
484 }
485 }
486
494 int balanceRemoval(RBNode * node) {
495 int result = 0;
496 RBNode * sibling = node->sibling();
497 RBNode * parent = (RBNode *) node->_parent;
498 bool doCaseSix = false;
499
500 if ((node->colorCount() < 2) && !RBTree<T>::isNodeBlack(node)) {
501 result = 122;
502 }
503
504 if (result == 0) {
505 // Case 1
506 if (node->isRoot()) {
507 node->setColorCount(1);
508
509 // Case 3, 4, 5, 6
510 } else if (RBTree<T>::isNodeBlack(sibling)) {
511 unsigned char rcase = 0;
512 RBNode * childOne = NULL, * childTwo = NULL;
513 char sibChildType = sibling->childType();
514 switch (sibChildType) {
515 case -1:
516 childOne = (RBNode *) sibling->right();
517 childTwo = (RBNode *) sibling->left();
518 break;
519 case 1:
520 childOne = (RBNode *) sibling->left();
521 childTwo = (RBNode *) sibling->right();
522 break;
523 default:
524 result = 124;
525 break;
526 }
527
528 if (result == 0) {
529 // Case 3, 5
530 if (RBTree<T>::isNodeBlack(parent)) {
531 // 5 (Will result in case 6)
532 if (RBTree<T>::isNodeRed(childOne) && RBTree<T>::isNodeBlack(childTwo)) {
533 switch (childOne->childType()) {
534 case -1:
535 rcase = ROTATION_CASE_RL;
536 break;
537 case 1:
538 rcase = ROTATION_CASE_LR;
539 break;
540 default:
541 break;
542 }
543
544 sibling->setColor(kRBTreeNodeColorRed);
545 sibling->left()->setColor(kRBTreeNodeColorBlack);
546 sibling->right()->setColor(kRBTreeNodeColorBlack);
547
548 result = this->rotate(childOne, rcase);
549
550 if (result == 0) {
551 result = this->balanceRemoval(node);
552 }
553
554 // 6
555 } else if (RBTree<T>::isNodeRed(childTwo)) {
556 doCaseSix = true;
557 // 3
558 } else {
559 node->setColorCount(1);
560 parent->setColorCount(2);
561 sibling->setColor(kRBTreeNodeColorRed);
562
563 result = this->balanceRemoval(parent);
564 }
565
566 // Case 4, 6
567 } else if (RBTree<T>::isNodeRed(parent)) {
568 // Case 4
569 if (RBTree<T>::isNodeBlack(childOne) && RBTree<T>::isNodeBlack(childTwo)) {
570 sibling->setColor(kRBTreeNodeColorRed);
571 parent->setColor(kRBTreeNodeColorBlack);
572 node->setColorCount(1);
573
574 // Case 6
575 } else if (RBTree<T>::isNodeRed(childTwo)) {
576 doCaseSix = true;
577 } else {
578 // We should never be in this case
579 result = 150;
580 }
581 }
582 }
583
584 // Case 6
585 if (!result && doCaseSix) {
586 switch (childTwo->childType()) {
587 case -1:
588 rcase = ROTATION_CASE_LL;
589 break;
590 case 1:
591 rcase = ROTATION_CASE_RR;
592 break;
593 default:
594 break;
595 }
596
597 childTwo->setColor(kRBTreeNodeColorBlack);
598 sibling->setColor(parent->color());
599 parent->setColor(kRBTreeNodeColorBlack);
600 node->setColorCount(1);
601
602 result = this->rotate(childTwo, rcase);
603 }
604
605 // Case 2
606 } else if (RBTree<T>::isNodeRed(sibling)) {
607 if (RBTree<T>::isNodeBlack(sibling->left()) && RBTree<T>::isNodeBlack(sibling->right())) {
608 unsigned char rcase = 0;
609 RBNode * child = NULL;
610 switch (sibling->childType()) {
611 case -1:
612 rcase = ROTATION_CASE_LL;
613 child = sibling->left();
614 break;
615 case 1:
616 rcase = ROTATION_CASE_RR;
617 child = sibling->right();
618 break;
619 default:
620 result = 123;
621 }
622
623 if (result == 0) {
624 parent->setColor(kRBTreeNodeColorRed);
625 sibling->setColor(kRBTreeNodeColorBlack);
626 result = this->rotate((RBNode *) child, rcase);
627 }
628
629 if (result == 0) {
630 result = this->balanceRemoval(node);
631 }
632 } else {
633 result = 133;
634 }
635 }
636 }
637
638 return result;
639 }
640
644 virtual int replaceNodeWithTheOnlyChild(typename BinTree<T,S>::BinNode * bnode) {
645 int result = 0;
646 typename BinTree<T,S>::BinNode * rep = NULL;
647 RBNode * node = (RBNode *) bnode;
648
649 if (node->childCount() != 1) {
650 result = 3;
651 }
652
653 if (result == 0) {
654 if (!((RBNode *) node->left())->isNull()) {
655 rep = node->left();
656 node->setLeft(NULL);
657 } else if (!((RBNode *) node->right())->isNull()) {
658 rep = node->right();
659 node->setRight(NULL);
660 } else {
661 result = 2;
662 }
663 }
664
665 if (result == 0) {
666 // result = node->replaceWithNode(rep);
667 result = this->replaceNodeWithNode(node, rep);
668 }
669
670 return result;
671 }
672
676 virtual int replaceNodeWithNode(typename BinTree<T,S>::BinNode * bnode, typename BinTree<T,S>::BinNode * breplacement) {
677 RBNode * node = (RBNode *) bnode;
678 RBNode * replacement = (RBNode *) breplacement;
679
680 if (!replacement) {
681 replacement = new RBNodeNull;
682 }
683
684 int result = this->BinTree<T,S>::replaceNodeWithNode(node, replacement);
685
686 if (result == 0) {
687 if ((node->color() == kRBTreeNodeColorBlack) && (replacement->color() == kRBTreeNodeColorBlack)) {
688 unsigned char currCount = replacement->colorCount();
689 replacement->setColorCount(++currCount);
690 } else {
691 replacement->setColor(kRBTreeNodeColorBlack);
692 }
693 }
694
695 return result;
696 }
697
698 static const unsigned char ROTATION_CASE_LL = 1;
699 static const unsigned char ROTATION_CASE_LR = 2;
700 static const unsigned char ROTATION_CASE_RR = 3;
701 static const unsigned char ROTATION_CASE_RL = 4;
702
712 unsigned char rotationCase(RBNode * node) {
713 if (!node) return 0;
714 else if (RBTree<T>::isNodeBlack(node)) return 0;
715 else if (node->isRoot()) return 0;
716 else if (node->childType() == 0) return 0;
717 else {
718 switch (this->getNodeParent(node)->childType()) {
719 case -1:
720 switch (node->childType()) {
721 case -1:
722 //return 1;
723 return ROTATION_CASE_LL;
724 case 1:
725 //return 2;
726 return ROTATION_CASE_LR;
727 default:
728 return 0;
729 }
730 case 1:
731 switch (node->childType()) {
732 case -1:
733 //return 4;
734 return ROTATION_CASE_RL;
735 case 1:
736 //return 3;
737 return ROTATION_CASE_RR;
738 default:
739 return 0;
740 }
741 default:
742 return 0;
743 }
744 }
745 }
746
754 int rotate(RBNode * node, unsigned char rotationCase) {
755 int result = 0;
756 // RBNode * parent = (RBNode *) node->_parent;
757 RBNode * parent = (RBNode *) this->getNodeParent(node);
758 RBNode * grandparent = node->grandParent();
759 RBNode * tmp = NULL;
760 const unsigned char ll = ROTATION_CASE_LL;
761 const unsigned char lr = ROTATION_CASE_LR;
762 const unsigned char rr = ROTATION_CASE_RR;
763 const unsigned char rl = ROTATION_CASE_RL;
764 unsigned char rcase = rotationCase;
765 RBNode ** newLocation = NULL;
766 char childType = 0;
767
768 // Figure out who we are replacing
769 if ((rcase == ll) || (rcase == rr)) {
770 childType = parent->childType();
771 tmp = grandparent;
772 } else if ((rcase == lr) || (rcase == rl)) {
773 childType = node->childType();
774 tmp = parent;
775 }
776
777 // tmp will be replaced by the node whose spot we are
778 // nulling out
779 switch (childType) {
780 case -1:
781 tmp->setLeft(new RBNodeNull);
782 //tmp->left()->_location = tmp->left()->_location;
783 this->setNodeLocation(tmp->left(), tmp->leftAddr());
784 // tmp->left()->_parent = tmp;
785 this->setNodeParent(tmp->left(), (typename BinTree<T,S>::BinNode *) tmp);
786 break;
787 case 1:
788 tmp->setRight(new RBNodeNull);
789 // tmp->right()->_location = tmp->right()->_location;
790 this->setNodeLocation(tmp->right(), tmp->rightAddr());
791 // tmp->right()->_parent = tmp;
792 this->setNodeParent(tmp->right(), (typename BinTree<T,S>::BinNode *) tmp);
793 break;
794 default:
795 result = 111;
796 break;
797 }
798
799 if (result == 0) {
800 tmp = NULL;
801 if ((rcase == ll) || (rcase == rr)) {
802 // Put parent in grandparent's location
803 // *grandparent->_location = parent;
804 *this->getNodeLocation(grandparent) = parent;
805 // *parent->_location = new RBNodeNull; // make sure gp has a null node to replace parent
806 *this->getNodeLocation(parent) = new RBNodeNull;
807 // (*parent->_location)->_parent = grandparent;
808 this->setNodeParent(
809 *this->getNodeLocation(parent),
810 (typename BinTree<T,S>::BinNode *) grandparent
811 );
812 // (*parent->_location)->_location = parent->_location;
813 this->setNodeLocation(
814 *this->getNodeLocation(parent),
815 this->getNodeLocation(parent)
816 );
817 // parent->_location = grandparent->_location;
818 this->setNodeLocation(parent, this->getNodeLocation((typename BinTree<T,S>::BinNode *) grandparent));
819 // parent->_parent = grandparent->_parent; // should work if gp is root
820 this->setNodeParent(parent, this->getNodeParent((typename BinTree<T,S>::BinNode *) grandparent));
821
822 // Clear grandparent's position memory
823 // grandparent->_parent = NULL;
824 // grandparent->_location = NULL;
825 this->setNodeParent(grandparent, NULL);
826 this->setNodeLocation(grandparent, NULL);
827
828 // Find new spot for grandparent
829 if (rcase == ll) {
830 tmp = (RBNode *) parent->right();
831 newLocation = (RBNode **) parent->rightAddr();
832 } else {
833 tmp = (RBNode *) parent->left();
834 newLocation = (RBNode **) parent->leftAddr();
835 }
836
837 // Do the replacement
838 // grandparent->_parent = parent;
839 this->setNodeParent(grandparent, (typename BinTree<T,S>::BinNode *) parent);
840 *newLocation = grandparent;
841 // grandparent->_location = (typename BinTree<T,S>::BinNode **) newLocation;
842 this->setNodeLocation(grandparent, (typename BinTree<T,S>::BinNode **) newLocation);
843
844 // Figuring out new spot for the node
845 // grandparent is replacing
846 if (!tmp->isNull()) {
847 // tmp->_parent = grandparent;
848 this->setNodeParent(tmp, (typename BinTree<T,S>::BinNode *) grandparent);
849 if (rcase == ll) {
850 newLocation = (RBNode **) grandparent->leftAddr();
851 } else {
852 newLocation = (RBNode **) grandparent->rightAddr();
853 }
854 }
855 } else if ((rcase == lr) || (rcase == rl)) {
856 // Put node in parent's position
857 // node->_parent = parent->_parent;
858 this->setNodeParent(node, this->getNodeParent(parent));
859 // *node->_location = new RBNodeNull; // Make sure parent has a null node we node used to be
860 *this->getNodeLocation(node) = new RBNodeNull;
861 // (*node->_location)->_parent = parent;
862 this->setNodeParent(
863 *this->getNodeLocation(node),
864 this->getNodeParent(node)
865 );
866 // (*node->_location)->_location = node->_location;
867 this->setNodeLocation(
868 *this->getNodeLocation(node),
869 this->getNodeLocation(node)
870 );
871 // node->_location = parent->_location;
872 this->setNodeLocation(node, parent->_location);
873 // *node->_location = node;
874 *this->getNodeLocation(node) = node;
875 //parent->_parent = NULL;
876 this->setNodeParent((typename BinTree<T,S>::BinNode *) parent, NULL);
877 // parent->_location = NULL;
878 this->setNodeLocation((typename BinTree<T,S>::BinNode *) parent, NULL);
879
880 // Find where the parent will be next
881 if (rcase == lr) {
882 tmp = (RBNode *) node->left();
883 newLocation = (RBNode **) node->leftAddr();
884 } else {
885 tmp = (RBNode *) node->right();
886 newLocation = (RBNode **) node->rightAddr();
887 }
888
889 // Do the replacement
890 // parent->_parent = (typename BinTree<T,S>::BinNode *) node;
891 this->setNodeParent(parent, (typename BinTree<T,S>::BinNode *) node);
892 *newLocation = parent;
893 //parent->_location = (typename BinTree<T,S>::BinNode **) newLocation;
894 this->setNodeLocation(parent, (typename BinTree<T,S>::BinNode **) newLocation);
895
896 // Find the new spot for the node
897 // parent is replacing
898 if (!tmp->isNull()) {
899 //tmp->_parent = parent;
900 this->setNodeParent(tmp, (typename BinTree<T,S>::BinNode *) parent);
901 if (rcase == lr) {
902 newLocation = (RBNode **) parent->rightAddr();
903 } else {
904 newLocation = (RBNode **) parent->leftAddr();
905 }
906 }
907
908 // no rotation case
909 } else {
910 result = 117;
911 }
912 }
913
914 // If the rotation orphaned a node (tmp), then
915 // we need to put it in the newLocation
916 if (result == 0) {
917 if (!tmp->isNull()) {
918 if (!(*newLocation)->isNull()) result = this->insert(tmp, *newLocation);
919 else {
920 // IF newLocation was holding a null node, we need
921 // to delete the null node memory
922 Delete(*newLocation);
923
924 *newLocation = tmp;
925 //tmp->_location = (typename BinTree<T,S>::BinNode **) newLocation;
926 this->setNodeLocation((typename BinTree<T,S>::BinNode *) tmp, (typename BinTree<T,S>::BinNode **) newLocation);
927 }
928 }
929 }
930
931 return result;
932 }
933
934 virtual const typename BinTree<T,S>::BinNode * maxNode(const typename BinTree<T,S>::BinNode * node) const {
935 const RBNode * rnode = (const RBNode *) node;
936 if (!rnode) return NULL;
937 else if (rnode->isNull()) return rnode;
938 else if (!rnode->right()->isNull()) return this->maxNode(rnode->right());
939 else return rnode;
940 }
941
942 virtual const typename BinTree<T,S>::BinNode * minNode(const typename BinTree<T,S>::BinNode * node) const {
943 const RBNode * rnode = (const RBNode *) node;
944 if (!rnode) return NULL;
945 else if (rnode->isNull()) return rnode;
946 else if (!rnode->left()->isNull()) return this->minNode(rnode->left());
947 else return rnode;
948 }
949
953 virtual void print(RBNode * rnode, bool printNullNodes) {
954 if (rnode->right()) {
955 bool isNull = rnode->right()->isNull();
956 if (!isNull || (printNullNodes && isNull))
957 this->print((RBNode *) rnode->right(), printNullNodes);
958 }
959
960 rnode->print();
961
962 if (rnode->left()) {
963 bool isNull = rnode->left()->isNull();
964 if (!isNull || (printNullNodes && isNull))
965 this->print((RBNode *) rnode->left(), printNullNodes);
966 }
967 }
968
972 virtual int locateLeafNodes(List<const typename BinTree<T,S>::BinNode *> * outList, const typename BinTree<T,S>::BinNode * node) {
973 int result = 0;
974 RBNode * rnode = (RBNode *) node;
975
976 if (!rnode) return 114;
977 else if (rnode->childCount() > 0) {
978 if (!rnode->left()->isNull()) result = this->locateLeafNodes(outList, node->left());
979
980 if (result == 0)
981 if (!rnode->right()->isNull()) result = this->locateLeafNodes(outList, node->right());
982
983 return result;
984 } else {
985 result = outList->add(node);
986 return result;
987 }
988 }
989};
990
991} // namespace BF
992
993#endif // RBTREE_HPP
994
Definition bintree.hpp:26
T _obj
Definition bintree.hpp:187
virtual void setLeft(BinNode *left)
Definition bintree.hpp:155
int level() const
Definition bintree.hpp:126
virtual const BinNode * left() const
Definition bintree.hpp:33
char childType() const
Definition bintree.hpp:55
virtual BinNode * sibling()
Definition bintree.hpp:104
BinNode ** leftAddr()
Definition bintree.hpp:173
BinNode ** _location
Definition bintree.hpp:184
virtual BinNode * grandParent()
Definition bintree.hpp:86
bool isRoot() const
Definition bintree.hpp:43
virtual BinNode * pibling()
Definition bintree.hpp:92
virtual const BinNode * right() const
Definition bintree.hpp:34
BinNode * _parent
Definition bintree.hpp:179
BinNode ** rightAddr()
Definition bintree.hpp:176
virtual void setRight(BinNode *right)
Definition bintree.hpp:164
Stack< BinNode * > _st
Definition bintree.hpp:262
Definition bintree.hpp:23
int insert(T obj)
Definition bintree.hpp:292
BinNode * getNodeParent(const BinNode *node)
Definition bintree.hpp:497
T max() const
Definition bintree.hpp:327
BinNode ** rootAddr()
Definition bintree.hpp:473
void setNodeParent(BinNode *node, BinNode *parent)
Definition bintree.hpp:498
void setNodeLocation(BinNode *node, BinNode **location)
Definition bintree.hpp:495
void setRoot(BinNode *node)
Definition bintree.hpp:474
int count() const
Definition bintree.hpp:306
int _count
Definition bintree.hpp:482
int removeNode(BinNode *node)
Definition bintree.hpp:639
virtual const BinNode * getNodeForObject(T obj)
Definition bintree.hpp:424
BinNode ** getNodeLocation(const BinNode *node)
Definition bintree.hpp:494
virtual const BinNode * root() const
Definition bintree.hpp:275
virtual int replaceNodeWithNode(BinNode *node, BinNode *replacement)
Definition bintree.hpp:589
Definition list.hpp:41
Definition rbtree.hpp:201
int setCurrent(typename BinTree< T, S >::BinNode *bnode)
Definition rbtree.hpp:209
Iterator()
Definition rbtree.hpp:204
Definition rbtree.hpp:162
virtual void setRight(typename BinTree< T, S >::BinNode *right)
Definition rbtree.hpp:191
virtual RBNodeNonnull * clone()
Definition rbtree.hpp:176
virtual bool isNull() const
Definition rbtree.hpp:174
virtual ~RBNodeNonnull()
Definition rbtree.hpp:171
virtual void setLeft(typename BinTree< T, S >::BinNode *left)
Definition rbtree.hpp:182
RBNodeNonnull()
Definition rbtree.hpp:164
Definition rbtree.hpp:145
RBNodeNull()
Definition rbtree.hpp:147
virtual bool isNull() const
Definition rbtree.hpp:153
virtual void printObject() const
Definition rbtree.hpp:155
virtual RBNodeNull * clone()
Definition rbtree.hpp:157
virtual ~RBNodeNull()
Definition rbtree.hpp:151
Definition rbtree.hpp:38
RBNode()
Definition rbtree.hpp:42
virtual RBNode * right()
Definition rbtree.hpp:60
void setColorCount(unsigned char count)
Definition rbtree.hpp:130
virtual void setRight(typename BinTree< T, S >::BinNode *right)
Definition rbtree.hpp:63
virtual ~RBNode()
Definition rbtree.hpp:49
virtual RBNode * left()
Definition rbtree.hpp:59
virtual void printObject() const
Definition rbtree.hpp:57
virtual const RBNode * left() const
Definition rbtree.hpp:52
virtual bool isNull() const
Definition rbtree.hpp:51
char color() const
Definition rbtree.hpp:118
virtual void setLeft(typename BinTree< T, S >::BinNode *left)
Definition rbtree.hpp:62
void setColor(char color)
Definition rbtree.hpp:122
void print() const
Definition rbtree.hpp:65
virtual RBNode * grandParent()
Definition rbtree.hpp:106
virtual RBNode * pibling()
Definition rbtree.hpp:110
virtual RBNode * sibling()
Definition rbtree.hpp:114
virtual RBNode * clone()
Definition rbtree.hpp:102
unsigned char colorCount() const
Definition rbtree.hpp:126
virtual unsigned char childCount() const
Definition rbtree.hpp:91
virtual const RBNode * right() const
Definition rbtree.hpp:53
Definition rbtree.hpp:35
virtual void print(bool printNullNodes)
Definition rbtree.hpp:274
virtual int remove(T obj)
Definition rbtree.hpp:260
virtual int createIterator(Iterator **itr)
Definition rbtree.hpp:297
virtual void print()
Definition rbtree.hpp:267
int deleteNode(const RBNode *node)
Definition rbtree.hpp:285
virtual const RBNode * getNodeForObject(T obj)
Definition rbtree.hpp:229
virtual const RBNode * root() const
Definition rbtree.hpp:227
virtual ~RBTree()
Definition rbtree.hpp:225
virtual const RBNode * maxNode() const
Definition rbtree.hpp:293
virtual const RBNode * minNode() const
Definition rbtree.hpp:289
RBTree()
Definition rbtree.hpp:223
virtual int insert(T obj)
Definition rbtree.hpp:236
#define Delete(x)
Definition delete.hpp:9
Definition array.hpp:18
#define kRBTreeNodeColorBlack
Definition rbtree.hpp:16
#define kRBTreeNodeMaskColorCount
Definition rbtree.hpp:26
#define kRBTreeNodeColorRed
Node colors.
Definition rbtree.hpp:15
#define kRBTreeNodeMaskColor
Definition rbtree.hpp:21