@@ -14,15 +14,18 @@ enum Color
1414 BLACK
1515};
1616
17- struct RBNode : public TreeNode
17+ template < class Comp = less< int > > struct RBNode : public TreeNode <Comp>
1818{
19+ typedef ft::value_compare<Comp> value_compare;
20+ typedef Comp comp;
21+
1922 RBNode* _parent;
2023 RBNode* _left;
2124 RBNode* _right;
2225 Color _color;
2326
2427 RBNode (int * _val, RBNode* _left = NUL, RBNode* _right = NUL, RBNode* _parent = NUL)
25- : TreeNode(_val), _parent(_parent), _left(_left), _right(_right),
28+ : TreeNode<Comp> (_val), _parent(_parent), _left(_left), _right(_right),
2629 _color (RED)
2730 {
2831 }
@@ -201,20 +204,30 @@ struct RBNode : public TreeNode
201204 }
202205};
203206
204- class RedBlackTree : public BinarySearchTree <RBNode>
207+ template <class Comp = less<int > >
208+ class RedBlackTree : public BinarySearchTree <Comp, RBNode<Comp> >
205209{
210+ protected:
211+ using typename BinarySearchTree<Comp, RBNode<Comp> >::pnode_t ;
212+ using typename BinarySearchTree<Comp, RBNode<Comp> >::node_t ;
213+
214+ public:
215+ using typename BinarySearchTree<Comp, RBNode<Comp> >::value_type;
216+ using typename BinarySearchTree<Comp, RBNode<Comp> >::size_type;
217+ using typename BinarySearchTree<Comp, RBNode<Comp> >::comp;
218+
206219public:
207220 RedBlackTree ()
208221 {
209222 }
210223
211224 void insert (const value_type& val)
212225 {
213- pnode_t node = BinarySearchTree::insert (val);
226+ pnode_t node = BinarySearchTree<Comp, RBNode<Comp> > ::insert (val);
214227 if (node != NUL)
215228 rebalanceInsertion (node);
216- if (hasRoot ())
217- ASSERT (getRoot ().is (BLACK));
229+ if (this -> hasRoot ())
230+ ASSERT (this -> getRoot ().is (BLACK));
218231 }
219232
220233 void rebalanceInsertion (pnode_t node)
@@ -245,8 +258,8 @@ class RedBlackTree : public BinarySearchTree<RBNode>
245258 break ;
246259 }
247260 }
248- getRoot ().set (BLACK);
249- ASSERT (getRoot ().is (BLACK));
261+ this -> getRoot ().set (BLACK);
262+ ASSERT (this -> getRoot ().is (BLACK));
250263 }
251264
252265 void handleCaseThreeInsertion (
@@ -305,7 +318,7 @@ class RedBlackTree : public BinarySearchTree<RBNode>
305318 // switch Y parent and X Parent
306319 pnode_t xParent = x->getParentAddr ();
307320 if (xParent == NUL)
308- setRoot (y);
321+ this -> setRoot (y);
309322 else if (x->getPosition () == LEFT)
310323 xParent->setLeft (y);
311324 else
@@ -334,7 +347,7 @@ class RedBlackTree : public BinarySearchTree<RBNode>
334347
335348 void erase (const value_type& val)
336349 {
337- pnode_t p = find (val);
350+ pnode_t p = this -> find (val);
338351 pnode_t x = NUL;
339352 pnode_t pa = NUL;
340353 Position pos;
@@ -349,9 +362,9 @@ class RedBlackTree : public BinarySearchTree<RBNode>
349362 else
350363 handleCaseThreeDeletion (p, pa, x, pos);
351364
352- setSize (size () - 1 );
365+ this -> setSize (this -> size () - 1 );
353366 rebalanceDeletion (p->getColor (), pa, x, pos);
354- deleteNode (p);
367+ this -> deleteNode (p);
355368 }
356369
357370 void rebalanceDeletion (Color clr, pnode_t pa, pnode_t x, Position pos)
@@ -453,7 +466,7 @@ class RedBlackTree : public BinarySearchTree<RBNode>
453466 ASSERT_NOT_NUL (node);
454467 ASSERT (node->hasRight ());
455468 ASSERT (node->getRight ().hasLeft ());
456- pnode_t succ = upper_bound (node);
469+ pnode_t succ = this -> upper_bound (node);
457470 ASSERT (node != succ);
458471
459472 pnode_t parent = node->getParentAddr ();
@@ -462,7 +475,7 @@ class RedBlackTree : public BinarySearchTree<RBNode>
462475 ASSERT (succ != right);
463476
464477 if (parent == NUL)
465- setRoot (succ);
478+ this -> setRoot (succ);
466479 else if (node->getPosition () == LEFT)
467480 parent->setLeft (succ);
468481 else
@@ -500,7 +513,7 @@ class RedBlackTree : public BinarySearchTree<RBNode>
500513 ASSERT_NOT_NUL (right);
501514
502515 if (parent == NUL)
503- setRoot (right);
516+ this -> setRoot (right);
504517 else if (node->getPosition () == LEFT)
505518 parent->setLeft (right);
506519 else
@@ -528,7 +541,7 @@ class RedBlackTree : public BinarySearchTree<RBNode>
528541
529542 pos = LEFT;
530543 if (parent == NUL)
531- setRoot (left);
544+ this -> setRoot (left);
532545 else if (node->getPosition () == LEFT)
533546 {
534547 parent->setLeft (left);
0 commit comments