summaryrefslogtreecommitdiff
path: root/ace/RB_Tree.cpp
blob: dd36270cb7f3e9c3e69582e7e3ead6078b50c920 (plain)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
// $Id$

// RB_Tree.cpp

#ifndef ACE_RB_TREE_C
#define ACE_RB_TREE_C

#include "ace/RB_Tree.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#if !defined (__ACE_INLINE__)
#include "ace/RB_Tree.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(ace, RB_Tree, "$Id$")

/////////////////////////////////////////////
// template class ACE_RB_Tree_Node<KEY, T> //
/////////////////////////////////////////////


// Constructor.

template <class KEY, class T>
ACE_RB_Tree_Node<KEY, T>::ACE_RB_Tree_Node (const KEY &k, const T &t)
  : k_ (k)
  , t_ (t)
  , color_ (RED)
  , parent_ (0)
  , left_ (0)
  , right_ (0)
{
}


// Destructor.

template <class KEY, class T>
ACE_RB_Tree_Node<KEY, T>::~ACE_RB_Tree_Node ()
{
  // Delete left sub-tree.
  delete left_;

  // Delete right sub_tree.
  delete right_;
}



////////////////////////////////////////
// template class ACE_RB_Tree<KEY, T> //
////////////////////////////////////////

// Constructor.

template <class KEY, class T>
ACE_RB_Tree<KEY, T>::ACE_RB_Tree (
  ACE_Const_Binary_Functor_Base<KEY, KEY> *less_than_functor,
  int free_functor)
  : root_ (0),
    less_than_functor_ (less_than_functor),
    free_functor_ (free_functor)
{
  if (less_than_functor_ == 0)
    {
      less_than_functor_ = new ACE_Less_Than_Functor<KEY, KEY>;
      free_functor_ = 1;
    }
}


// Copy constructor.

template <class KEY, class T>
ACE_RB_Tree<KEY, T>::ACE_RB_Tree (const ACE_RB_Tree<KEY, T> &rbt)
  : root_ (0)
{
  // Make a copy of the comparison functor.
  less_than_functor_ = (rbt.less_than_functor_ == 0)
                       ? 0 : rbt.less_than_functor_->clone ();
  free_functor_ = 1;

  // Make a deep copy of the passed tree.
  ACE_RB_Tree_Iterator<KEY, T> iter(rbt);
  for (iter.first (); iter.is_done () == 0; iter.next ())
  {
    insert (*(iter.key ()), *(iter.item ()));
  }
}


// Destructor.

template <class KEY, class T>
ACE_RB_Tree<KEY, T>::~ACE_RB_Tree ()
{
  // Free the comparison functor if needed.
  if (free_functor_)
    {
      delete less_than_functor_;
    }

  // Clear away all nodes in the tree.
  clear ();
}


// Assignment operator.

template <class KEY, class T> void
ACE_RB_Tree<KEY, T>::operator = (const ACE_RB_Tree<KEY, T> &rbt)
{
  // Free the comparison functor if needed.
  if (free_functor_)
    {
      delete less_than_functor_;
    }

  // Make a copy of the passed tree's comparison functor.
  less_than_functor_ = (rbt.less_than_functor_ == 0)
                       ? 0 : rbt.less_than_functor_->clone ();
  free_functor_ = 1;

  // Clear out the existing tree.
  clear ();

  // Make a deep copy of the passed tree.
  ACE_RB_Tree_Iterator<KEY, T> iter(rbt);
  for (iter.first (); iter.is_done () == 0; iter.next ())
  {
    insert (*(iter.key ()), *(iter.item ()));
  }
}

// Less than comparison function for keys, default
// functor implementation returns 1 if k1 < k2, 0 otherwise.

template <class KEY, class T> int
ACE_RB_Tree<KEY, T>::lessthan (const KEY &k1, const KEY &k2)
{
  if (less_than_functor_ == 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                         ASYS_TEXT ("\nNull comparison functor pointer.\n")),
                        0);
    }
  else
    {
      return less_than_functor_->execute (k1, k2);
    }
}


// Returns a pointer to the item corresponding to the
// given key, or 0 if it cannot find the key in the tree.

template <class KEY, class T> T*
ACE_RB_Tree<KEY, T>::find (const KEY &k)
{
  // Find the closest matching node, if there is one.
  ACE_RB_Tree_Node<KEY, T> *current = find_node (k);

  if (current)
  {
    // If a nearest matching node was returned.
    if (this->lessthan (current->key (), k)
        || this->lessthan (k, current->key ()))
    {
      // If the keys differ, there is no match: return 0.
      return 0;
    }
    else
    {
      // The keys match: return a pointer to the node's item.
      return &(current->item ());
    }
  }
  else
  {
    // The tree is empty: return 0.
    return 0;
  }
}



// Inserts a *copy* of the key and the item into the tree:
// both the key type KEY and the item type T must have well
// defined semantics for copy construction and < comparison.
// This method returns a pointer to the inserted item copy,
// or 0 if an error occurred.  NOTE: if an identical key
// already exists in the tree, no new item is created, and
// the returned pointer addresses the existing item
// associated with the existing key.

template <class KEY, class T>  T*
ACE_RB_Tree<KEY, T>::insert (const KEY &k, const T &t)
{
  // Find the closest matching node, if there is one.
  ACE_RB_Tree_Node<KEY, T> *current = find_node (k);
  if (current)
  {
    if (this->lessthan (current->key (), k))
    {
      // If a nearest matching node has a key less than the insertion key.
      if (current->right ())
      {
        // If there is already a right subtree, complain.
        ACE_ERROR_RETURN ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                           ASYS_TEXT ("\nright subtree already present in "
                                      "ACE_RB_Tree<KEY, T>::insert\n")), 0);
      }
      else
      {
        // The right subtree is empty: insert new node there.
        current->right (new ACE_RB_Tree_Node<KEY, T> (k, t));
        if (current->right ())
        {
          // If the node was successfully inserted, set its parent, rebalance
          // the tree, color the root black, and return a pointer to the
          // inserted item.
          T *item = &(current->right ()->item ());
          current->right ()->parent (current);
          RB_rebalance (current->right ());
          root_->color (ACE_RB_Tree_Node_Base::BLACK);
          return item;
        }
        else
        {
          // Memory allocation failed.
        ACE_ERROR_RETURN ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                           ASYS_TEXT ("\nmemory allocation to current->right_ failed "
                                      "in ACE_RB_Tree<KEY, T>::insert\n")), 0);
        }
      }
    }
    else if (this->lessthan (k, current->key ()))
    {
      // If a nearest matching node has a key greater than the insertion key.
      if (current->left ())
      {
        // If there is already a left subtree, complain.
        ACE_ERROR_RETURN ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                           ASYS_TEXT ("\nleft subtree already present in "
                                      "ACE_RB_Tree<KEY, T>::insert\n")), 0);
      }
      else
      {
        // The right subtree is empty: insert new node there.
        current->left (new ACE_RB_Tree_Node<KEY, T> (k, t));
        if (current->left ())
        {
          // If the node was successfully inserted, set its parent, rebalance
          // the tree, color the root black, and return a pointer to the
          // inserted item.
          T *item = &(current->left ()->item ());
          current->left ()->parent (current);
          RB_rebalance (current->left ());
          root_->color (ACE_RB_Tree_Node_Base::BLACK);
          return item;
        }
        else
        {
          // Memory allocation failed.
          ACE_ERROR_RETURN ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                           ASYS_TEXT ("\nmemory allocation to current->left_ failed in "
                                      "ACE_RB_Tree<KEY, T>::insert\n")), 0);
        }
      }
    }
    else
    {
      // The keys match: return a pointer to the node's item.
      return &(current->item ());
    }
  }
  else
  {
    // The tree is empty: insert at the root and color the root black.
    root_ = new ACE_RB_Tree_Node<KEY, T> (k, t);
    if (root_)
    {
      root_->color (ACE_RB_Tree_Node_Base::BLACK);
      return &(root_->item ());
    }
    else
    {
      ACE_ERROR_RETURN ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                         ASYS_TEXT ("\nmemory allocation to root_ failed in "
                                    "ACE_RB_Tree<KEY, T>::insert\n")), 0);
    }
  }
}


// Removes the item associated with the given key from the
// tree and destroys it.  Returns 1 if it found the item
// and successfully destroyed it, 0 if it did not find the
// item, or -1 if an error occurred.

template <class KEY, class T>  int
ACE_RB_Tree<KEY, T>::remove (const KEY &k)
{
  // Find a matching node, if there is one.
  ACE_RB_Tree_Node<KEY, T> *x, *z;

  z = find_node (k);

  if ((z) && (! this->lessthan (z->key (), k))
      && (! this->lessthan (k, z->key ())))
  {
    // There is a matching node: remove and destroy it.
    ACE_RB_Tree_Node<KEY, T> *y;
    if ((z->left ()) && (z->right ()))
    {
      y = RB_tree_successor (z);
    }
    else
    {
      y = z;
    }
    if (y->left ())
    {
      x = y->left ();
    }
    else
    {
      x = y->right ();
    }
    if (x)
        {
          x->parent (y->parent ());
        }
    if (y->parent ())
    {
      if (y == y->parent ()->left ())
      {
        y->parent ()->left (x);
      }
      else
      {
        y->parent ()->right (x);
      }
    }
    else
    {
      root_ = x;
    }
    if (y != z)
    {
      // Copy the elements of y into z.
      z->key () = y->key ();
      z->item () = y->item ();
    }
    if (y->color () == ACE_RB_Tree_Node_Base::BLACK)
    {
      RB_delete_fixup (x);
    }
    y->parent (0);
    y->right (0);
    y->left (0);
    delete y;
    return 1;
  }
  else
  {
    // No matching node was found: return 0.
    return 0;
  }
}


// Method for right rotation of the tree about a given node.

template <class KEY, class T>  void
ACE_RB_Tree<KEY, T>::RB_rotate_right (ACE_RB_Tree_Node<KEY, T> * x)
{
  if (! x)
  {
    ACE_ERROR ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                       ASYS_TEXT ("\nerror: x is a null pointer in "
                                  "ACE_RB_Tree<KEY, T>::RB_rotate_right\n")));
  }
  else if (! (x->left()))
  {
    ACE_ERROR ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                ASYS_TEXT ("\nerror: x->left () is a null pointer in "
                           "ACE_RB_Tree<KEY, T>::RB_rotate_right\n")));
  }
  else
  {
    ACE_RB_Tree_Node<KEY, T> * y;
    y = x->left ();
    x->left (y->right ());
    if (y->right ())
    {
      y->right ()->parent (x);
    }
    y->parent (x->parent ());
    if (x->parent ())
    {
      if (x == x->parent ()->right ())
      {
        x->parent ()->right (y);
      }
      else
      {
        x->parent ()->left (y);
      }
    }
    else
    {
      root_ = y;
    }
    y->right (x);
    x->parent (y);
  }
}


// Method for left rotation of the tree about a given node.

template <class KEY, class T> void
ACE_RB_Tree<KEY, T>::RB_rotate_left (ACE_RB_Tree_Node<KEY, T> * x)
{
  if (! x)
  {
    ACE_ERROR ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                ASYS_TEXT ("\nerror: x is a null pointer in "
                           "ACE_RB_Tree<KEY, T>::RB_rotate_left\n")));
  }
  else if (! (x->right()))
  {
    ACE_ERROR ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                ASYS_TEXT ("\nerror: x->right () is a null pointer "
                           "in ACE_RB_Tree<KEY, T>::RB_rotate_left\n")));
  }
  else
  {
    ACE_RB_Tree_Node<KEY, T> * y;
    y = x->right ();
    x->right (y->left ());
    if (y->left ())
    {
      y->left ()->parent (x);
    }
    y->parent (x->parent ());
    if (x->parent ())
    {
      if (x == x->parent ()->left ())
      {
        x->parent ()->left (y);
      }
      else
      {
        x->parent ()->right (y);
      }
    }
    else
    {
      root_ = y;
    }
    y->left (x);
    x->parent (y);
  }
}


// Method for restoring Red-Black properties after deletion.

template <class KEY, class T>  void
ACE_RB_Tree<KEY, T>::RB_delete_fixup (ACE_RB_Tree_Node<KEY, T> * x)
{
  while ((x) && (x->parent ()) && (x->color () == ACE_RB_Tree_Node_Base::BLACK))
  {
    if (x == x->parent ()->left ())
    {
      ACE_RB_Tree_Node<KEY, T> *w = x->parent ()->right ();
      if (w->color () == ACE_RB_Tree_Node_Base::RED)
      {
        w->color (ACE_RB_Tree_Node_Base::BLACK);
        x->parent ()->color (ACE_RB_Tree_Node_Base::RED);
        RB_rotate_left (x->parent ());
        w = x->parent ()->right ();
      }
      if ((w->left ()->color () == ACE_RB_Tree_Node_Base::BLACK) &&
          (w->right ()->color () == ACE_RB_Tree_Node_Base::BLACK))
      {
        w->color (ACE_RB_Tree_Node_Base::RED);
        x = x->parent ();
      }
      else
      {
        if (w->right ()->color () == ACE_RB_Tree_Node_Base::BLACK)
        {
          w->left ()->color (ACE_RB_Tree_Node_Base::BLACK);
          w->color (ACE_RB_Tree_Node_Base::RED);
          RB_rotate_right (w);
          w = x->parent ()->right ();
        }
        w->color (x->parent ()->color ());
        x->parent ()->color (ACE_RB_Tree_Node_Base::BLACK);
        w->right ()->color (ACE_RB_Tree_Node_Base::BLACK);
        RB_rotate_left (x->parent ());
        x = root_;
      }
    }
    else
    {
      ACE_RB_Tree_Node<KEY, T> *w = x->parent ()->left ();
      if (w->color () == ACE_RB_Tree_Node_Base::RED)
      {
        w->color (ACE_RB_Tree_Node_Base::BLACK);
        x->parent ()->color (ACE_RB_Tree_Node_Base::RED);
        RB_rotate_right (x->parent ());
        w = x->parent ()->left ();
      }
      if ((w->left ()->color () == ACE_RB_Tree_Node_Base::BLACK) &&
          (w->right ()->color () == ACE_RB_Tree_Node_Base::BLACK))
      {
        w->color (ACE_RB_Tree_Node_Base::RED);
        x = x->parent ();
      }
      else
      {
        if (w->left ()->color () == ACE_RB_Tree_Node_Base::BLACK)
        {
          w->right ()->color (ACE_RB_Tree_Node_Base::BLACK);
          w->color (ACE_RB_Tree_Node_Base::RED);
          RB_rotate_left (w);
          w = x->parent ()->left ();
        }
        w->color (x->parent ()->color ());
        x->parent ()->color (ACE_RB_Tree_Node_Base::BLACK);
        w->left ()->color (ACE_RB_Tree_Node_Base::BLACK);
        RB_rotate_right (x->parent ());
        x = root_;
      }
    }
  }

  if (x)
  {
    x->color (ACE_RB_Tree_Node_Base::BLACK);
  }
}



// Return a pointer to a matching node if there is one,
// a pointer to the node under which to insert the item
// if the tree is not empty and there is no such match,
// or 0 if the tree is empty.

template <class KEY, class T> ACE_RB_Tree_Node<KEY, T> *
ACE_RB_Tree<KEY, T>::find_node (const KEY &k)
{
  ACE_RB_Tree_Node<KEY, T> *current = root_;

  while (current)
  {
    // While there are more nodes to examine.
    if (this->lessthan (current->key (), k))
    {
      // If the search key is greater than the current node's key.
      if (current->right ())
      {
        // If the right subtree is not empty, search to the right.
        current = current->right ();
      }
      else
      {
        // If the right subtree is empty, we're done.
        break;
      }
    }
    else if (this->lessthan (k, current->key ()))
    {
      // Else if the search key is less than the current node's key.
      if (current->left ())
      {
        // If the left subtree is not empty, search to the left.
        current = current->left ();
      }
      else
      {
        // If the left subtree is empty, we're done.
        break;
      }
    }
    else
    {
      // If the keys match, we're done.
      break;
    }
  }

  return current;
}


// Rebalance the tree after insertion of a node.

template <class KEY, class T> void
ACE_RB_Tree<KEY, T>::RB_rebalance (ACE_RB_Tree_Node<KEY, T> * x)
{
  ACE_RB_Tree_Node<KEY, T> *y = 0;

  while ((x) && (x->parent ())
         && (x->parent ()->color () == ACE_RB_Tree_Node_Base::RED))
  {
    if (! x->parent ()->parent ())
    {
      // If we got here, something is drastically wrong!
      ACE_ERROR ((LM_ERROR,  ASYS_TEXT ("%p\n"),
                  ASYS_TEXT ("\nerror: parent's parent is null in "
                             "ACE_RB_Tree<KEY, T>::RB_rebalance\n")));
      return;
    }

    if (x->parent () == x->parent ()->parent ()->left ())
    {
      y = x->parent ()->parent ()->right ();
      if (y && (y->color () == ACE_RB_Tree_Node_Base::RED))
      {
        // Handle case 1 (see CLR book, pp. 269).
        x->parent ()->color (ACE_RB_Tree_Node_Base::BLACK);
        y->color (ACE_RB_Tree_Node_Base::BLACK);
        x->parent ()->parent ()->color (ACE_RB_Tree_Node_Base::RED);
        x = x->parent ()->parent ();
      }
      else
      {
        if (x == x->parent ()->right ())
        {
          // Transform case 2 into case 3 (see CLR book, pp. 269).
          x = x->parent ();
          RB_rotate_left (x);
        }

        // Handle case 3 (see CLR book, pp. 269).
        x->parent ()->color (ACE_RB_Tree_Node_Base::BLACK);
        x->parent ()->parent ()->color (ACE_RB_Tree_Node_Base::RED);
        RB_rotate_right (x->parent ()->parent ());
      }
    }
    else
    {
      y = x->parent ()->parent ()->left ();
      if (y && (y->color () == ACE_RB_Tree_Node_Base::RED))
      {
        // Handle case 1 (see CLR book, pp. 269).
        x->parent ()->color (ACE_RB_Tree_Node_Base::BLACK);
        y->color (ACE_RB_Tree_Node_Base::BLACK);
        x->parent ()->parent ()->color (ACE_RB_Tree_Node_Base::RED);
        x = x->parent ()->parent ();
      }
      else
      {
        if (x == x->parent ()->left ())
        {
          // Transform case 2 into case 3 (see CLR book, pp. 269).
          x = x->parent ();
          RB_rotate_right (x);
        }

        // Handle case 3 (see CLR book, pp. 269).
        x->parent ()->color (ACE_RB_Tree_Node_Base::BLACK);
        x->parent ()->parent ()->color (ACE_RB_Tree_Node_Base::RED);
        RB_rotate_left (x->parent ()->parent ());
      }
    }
  }
}


// Method to find the successor node of the given node in the tree.

template <class KEY, class T> ACE_RB_Tree_Node<KEY, T> *
ACE_RB_Tree<KEY, T>::RB_tree_successor (ACE_RB_Tree_Node<KEY, T> *x) const
{
  if (x->right ())
  {
    return RB_tree_minimum (x->right ());
  }

  ACE_RB_Tree_Node<KEY, T> *y = x->parent ();
  while ((y) && (x == y->right ()))
  {
    x = y;
    y = y->parent ();
  }

  return y;
}


// Method to find the predecessor node of the given node in the tree.

template <class KEY, class T> ACE_RB_Tree_Node<KEY, T> *
ACE_RB_Tree<KEY, T>::RB_tree_predecessor (ACE_RB_Tree_Node<KEY, T> *x) const
{
  if (x->left ())
  {
    return RB_tree_maximum (x->left ());
  }

  ACE_RB_Tree_Node<KEY, T> *y = x->parent ();
  while ((y) && (x == y->left ()))
  {
    x = y;
    y = y->parent ();
  }

  return y;
}


// Method to find the minimum node of the subtree rooted at the given node.

template <class KEY, class T> ACE_RB_Tree_Node<KEY, T> *
ACE_RB_Tree<KEY, T>::RB_tree_minimum (ACE_RB_Tree_Node<KEY, T> *x) const
{
  while ((x) && (x->left ()))
  {
    x = x->left ();
  }

  return x;
}


// Method to find the maximum node of the subtree rooted at the given node.

template <class KEY, class T> ACE_RB_Tree_Node<KEY, T> *
ACE_RB_Tree<KEY, T>::RB_tree_maximum (ACE_RB_Tree_Node<KEY, T> *x) const
{
  while ((x) && (x->right ()))
  {
    x = x->right ();
  }

  return x;
}




/////////////////////////////////////////////////
// template class ACE_RB_Tree_Iterator<KEY, T> //
/////////////////////////////////////////////////


// Constructor.

template <class KEY, class T>
ACE_RB_Tree_Iterator<KEY, T>::ACE_RB_Tree_Iterator (const ACE_RB_Tree<KEY, T> &tree)
  : tree_ (tree), node_ (0)
{
  // Position the iterator at the first node in the tree.
  first ();
}


// Destructor.

template <class KEY, class T>
ACE_RB_Tree_Iterator<KEY, T>::~ACE_RB_Tree_Iterator ()
{
}


#endif /* !defined (ACE_RB_TREE_C) */