@@ -31,29 +31,32 @@ struct TRbTreeNodeBase {
31
31
}
32
32
33
33
static TBasePtr MinimumNode (TBasePtr x) {
34
- while (x->Left_ != nullptr )
34
+ while (x->Left_ != nullptr ) {
35
35
x = x->Left_ ;
36
-
36
+ }
37
37
return x;
38
38
}
39
39
40
40
static TBasePtr MaximumNode (TBasePtr x) {
41
- while (x->Right_ != nullptr )
41
+ while (x->Right_ != nullptr ) {
42
42
x = x->Right_ ;
43
-
43
+ }
44
44
return x;
45
45
}
46
46
47
47
static TBasePtr ByIndex (TBasePtr x, size_t index) {
48
48
if (x->Left_ != nullptr ) {
49
- if (index < x->Left_ ->Children_ )
49
+ if (index < x->Left_ ->Children_ ) {
50
50
return ByIndex (x->Left_ , index);
51
+ }
51
52
index -= x->Left_ ->Children_ ;
52
53
}
53
- if (0 == index)
54
+ if (0 == index) {
54
55
return x;
55
- if (!x->Right_ )
56
+ }
57
+ if (!x->Right_ ) {
56
58
ythrow yexception () << " index not found" ;
59
+ }
57
60
return ByIndex (x->Right_ , index - 1 );
58
61
}
59
62
};
@@ -335,12 +338,13 @@ class TRbTree {
335
338
TBasePtr y = nullptr ;
336
339
TBasePtr x = Root (); // Current node.
337
340
338
- while (x != nullptr )
339
- if (!KeyCompare_ (ValueNode (x), k))
341
+ while (x != nullptr ) {
342
+ if (!KeyCompare_ (ValueNode (x), k)) {
340
343
y = x, x = LeftNode (x);
341
- else
344
+ } else {
342
345
x = RightNode (x);
343
-
346
+ }
347
+ }
344
348
if (y) {
345
349
if (KeyCompare_ (k, ValueNode (y))) {
346
350
y = nullptr ;
@@ -375,12 +379,13 @@ class TRbTree {
375
379
TBasePtr y = const_cast <TBasePtr>(&this ->Data_ ); /* Last node which is not less than k. */
376
380
TBasePtr x = Root (); /* Current node. */
377
381
378
- while (x != nullptr )
379
- if (!KeyCompare_ (ValueNode (x), k))
382
+ while (x != nullptr ) {
383
+ if (!KeyCompare_ (ValueNode (x), k)) {
380
384
y = x, x = LeftNode (x);
381
- else
385
+ } else {
382
386
x = RightNode (x);
383
-
387
+ }
388
+ }
384
389
return y;
385
390
}
386
391
@@ -389,12 +394,13 @@ class TRbTree {
389
394
TBasePtr y = const_cast <TBasePtr>(&this ->Data_ ); /* Last node which is greater than k. */
390
395
TBasePtr x = Root (); /* Current node. */
391
396
392
- while (x != nullptr )
393
- if (KeyCompare_ (k, ValueNode (x)))
397
+ while (x != nullptr ) {
398
+ if (KeyCompare_ (k, ValueNode (x))) {
394
399
y = x, x = LeftNode (x);
395
- else
400
+ } else {
396
401
x = RightNode (x);
397
-
402
+ }
403
+ }
398
404
return y;
399
405
}
400
406
@@ -457,14 +463,16 @@ class TRbTree {
457
463
KeyCompare_ (ValueNode (val), ValueNode (parent))))
458
464
{
459
465
LeftNode (parent) = new_node;
460
- if (parent == LeftMost ())
466
+ if (parent == LeftMost ()) {
461
467
// maintain LeftMost() pointing to min node
462
468
LeftMost () = new_node;
469
+ }
463
470
} else {
464
471
RightNode (parent) = new_node;
465
- if (parent == RightMost ())
472
+ if (parent == RightMost ()) {
466
473
// maintain RightMost() pointing to max node
467
474
RightMost () = new_node;
475
+ }
468
476
}
469
477
ParentNode (new_node) = parent;
470
478
TRbGlobalInst::Rebalance (new_node, this ->Data_ .Parent_ );
@@ -532,16 +540,18 @@ template <class TDummy>
532
540
void TRbGlobal<TDummy>::RotateLeft(TRbTreeNodeBase* x, TRbTreeNodeBase*& root) {
533
541
TRbTreeNodeBase* y = x->Right_ ;
534
542
x->Right_ = y->Left_ ;
535
- if (y->Left_ != nullptr )
543
+ if (y->Left_ != nullptr ) {
536
544
y->Left_ ->Parent_ = x;
545
+ }
537
546
y->Parent_ = x->Parent_ ;
538
547
539
- if (x == root)
548
+ if (x == root) {
540
549
root = y;
541
- else if (x == x->Parent_ ->Left_ )
550
+ } else if (x == x->Parent_ ->Left_ ) {
542
551
x->Parent_ ->Left_ = y;
543
- else
552
+ } else {
544
553
x->Parent_ ->Right_ = y;
554
+ }
545
555
y->Left_ = x;
546
556
x->Parent_ = y;
547
557
y->Children_ = x->Children_ ;
@@ -552,16 +562,18 @@ template <class TDummy>
552
562
void TRbGlobal<TDummy>::RotateRight(TRbTreeNodeBase* x, TRbTreeNodeBase*& root) {
553
563
TRbTreeNodeBase* y = x->Left_ ;
554
564
x->Left_ = y->Right_ ;
555
- if (y->Right_ != nullptr )
565
+ if (y->Right_ != nullptr ) {
556
566
y->Right_ ->Parent_ = x;
567
+ }
557
568
y->Parent_ = x->Parent_ ;
558
569
559
- if (x == root)
570
+ if (x == root) {
560
571
root = y;
561
- else if (x == x->Parent_ ->Right_ )
572
+ } else if (x == x->Parent_ ->Right_ ) {
562
573
x->Parent_ ->Right_ = y;
563
- else
574
+ } else {
564
575
x->Parent_ ->Left_ = y;
576
+ }
565
577
y->Right_ = x;
566
578
x->Parent_ = y;
567
579
y->Children_ = x->Children_ ;
@@ -633,15 +645,13 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
633
645
TRbTreeNodeBase* x;
634
646
TRbTreeNodeBase* x_parent;
635
647
636
- if (y->Left_ == nullptr ) // z has at most one non-null child. y == z.
637
- x = y->Right_ ; // x might be null.
638
- else {
639
- if (y->Right_ == nullptr ) // z has exactly one non-null child. y == z.
640
- x = y->Left_ ; // x is not null.
641
- else { // z has two non-null children. Set y to
642
- y = TRbTreeNodeBase::MinimumNode (y->Right_ ); // z's successor. x might be null.
643
- x = y->Right_ ;
644
- }
648
+ if (y->Left_ == nullptr ) { // z has at most one non-null child. y == z.
649
+ x = y->Right_ ; // x might be null.
650
+ } else if (y->Right_ == nullptr ) { // z has exactly one non-null child. y == z.
651
+ x = y->Left_ ; // x is not null.
652
+ } else { // z has two non-null children. Set y to
653
+ y = TRbTreeNodeBase::MinimumNode (y->Right_ ); // z's successor. x might be null.
654
+ x = y->Right_ ;
645
655
}
646
656
647
657
if (y != z) {
@@ -650,19 +660,22 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
650
660
y->Left_ = z->Left_ ;
651
661
if (y != z->Right_ ) {
652
662
x_parent = y->Parent_ ;
653
- if (x)
663
+ if (x) {
654
664
x->Parent_ = y->Parent_ ;
665
+ }
655
666
y->Parent_ ->Left_ = x; // y must be a child of mLeft
656
667
y->Right_ = z->Right_ ;
657
668
z->Right_ ->Parent_ = y;
658
- } else
669
+ } else {
659
670
x_parent = y;
660
- if (root == z)
671
+ }
672
+ if (root == z) {
661
673
root = y;
662
- else if (z->Parent_ ->Left_ == z)
674
+ } else if (z->Parent_ ->Left_ == z) {
663
675
z->Parent_ ->Left_ = y;
664
- else
676
+ } else {
665
677
z->Parent_ ->Right_ = y;
678
+ }
666
679
y->Parent_ = z->Parent_ ;
667
680
DoSwap (y->Color_ , z->Color_ );
668
681
@@ -678,31 +691,35 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
678
691
} else {
679
692
// y == z
680
693
x_parent = y->Parent_ ;
681
- if (x)
694
+ if (x) {
682
695
x->Parent_ = y->Parent_ ;
683
- if (root == z)
696
+ }
697
+ if (root == z) {
684
698
root = x;
685
- else {
686
- if (z->Parent_ ->Left_ == z)
699
+ } else {
700
+ if (z->Parent_ ->Left_ == z) {
687
701
z->Parent_ ->Left_ = x;
688
- else
702
+ } else {
689
703
z->Parent_ ->Right_ = x;
704
+ }
690
705
DecrementChildrenUntilRoot (z->Parent_ , root); // we lost y
691
706
}
692
707
693
708
if (leftmost == z) {
694
- if (z->Right_ == nullptr ) // z->mLeft must be null also
709
+ if (z->Right_ == nullptr ) { // z->mLeft must be null also
695
710
leftmost = z->Parent_ ;
696
711
// makes leftmost == _M_header if z == root
697
- else
712
+ } else {
698
713
leftmost = TRbTreeNodeBase::MinimumNode (x);
714
+ }
699
715
}
700
716
if (rightmost == z) {
701
- if (z->Left_ == nullptr ) // z->mRight must be null also
717
+ if (z->Left_ == nullptr ) { // z->mRight must be null also
702
718
rightmost = z->Parent_ ;
703
719
// makes rightmost == _M_header if z == root
704
- else // x == z->mLeft
720
+ } else { // x == z->mLeft
705
721
rightmost = TRbTreeNodeBase::MaximumNode (x);
722
+ }
706
723
}
707
724
}
708
725
@@ -726,16 +743,18 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
726
743
x_parent = x_parent->Parent_ ;
727
744
} else {
728
745
if (w->Right_ == nullptr || w->Right_ ->Color_ == RBTreeBlack) {
729
- if (w->Left_ )
746
+ if (w->Left_ ) {
730
747
w->Left_ ->Color_ = RBTreeBlack;
748
+ }
731
749
w->Color_ = RBTreeRed;
732
750
RotateRight (w, root);
733
751
w = x_parent->Right_ ;
734
752
}
735
753
w->Color_ = x_parent->Color_ ;
736
754
x_parent->Color_ = RBTreeBlack;
737
- if (w->Right_ )
755
+ if (w->Right_ ) {
738
756
w->Right_ ->Color_ = RBTreeBlack;
757
+ }
739
758
RotateLeft (x_parent, root);
740
759
break ;
741
760
}
@@ -758,31 +777,34 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
758
777
x_parent = x_parent->Parent_ ;
759
778
} else {
760
779
if (w->Left_ == nullptr || w->Left_ ->Color_ == RBTreeBlack) {
761
- if (w->Right_ )
780
+ if (w->Right_ ) {
762
781
w->Right_ ->Color_ = RBTreeBlack;
782
+ }
763
783
w->Color_ = RBTreeRed;
764
784
RotateLeft (w, root);
765
785
w = x_parent->Left_ ;
766
786
}
767
787
w->Color_ = x_parent->Color_ ;
768
788
x_parent->Color_ = RBTreeBlack;
769
- if (w->Left_ )
789
+ if (w->Left_ ) {
770
790
w->Left_ ->Color_ = RBTreeBlack;
791
+ }
771
792
RotateRight (x_parent, root);
772
793
break ;
773
794
}
774
795
}
775
- if (x)
796
+ if (x) {
776
797
x->Color_ = RBTreeBlack;
798
+ }
777
799
}
778
800
return y;
779
801
}
780
802
781
803
template <class TDummy >
782
804
TRbTreeNodeBase* TRbGlobal<TDummy>::DecrementNode(TRbTreeNodeBase* Node_) {
783
- if (Node_->Color_ == RBTreeRed && Node_->Parent_ ->Parent_ == Node_)
805
+ if (Node_->Color_ == RBTreeRed && Node_->Parent_ ->Parent_ == Node_) {
784
806
Node_ = Node_->Right_ ;
785
- else if (Node_->Left_ != nullptr ) {
807
+ } else if (Node_->Left_ != nullptr ) {
786
808
Node_ = TRbTreeNodeBase::MaximumNode (Node_->Left_ );
787
809
} else {
788
810
TBasePtr y = Node_->Parent_ ;
@@ -808,8 +830,9 @@ TRbTreeNodeBase* TRbGlobal<TDummy>::IncrementNode(TRbTreeNodeBase* Node_) {
808
830
// check special case: This is necessary if mNode is the
809
831
// _M_head and the tree contains only a single node y. In
810
832
// that case parent, left and right all point to y!
811
- if (Node_->Right_ != y)
833
+ if (Node_->Right_ != y) {
812
834
Node_ = y;
835
+ }
813
836
}
814
837
return Node_;
815
838
}
0 commit comments