rb_tree.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. #pragma once
  2. #include <util/generic/utility.h>
  3. #include <util/generic/yexception.h>
  4. using TRbTreeColorType = bool;
  5. #define RBTreeRed false
  6. #define RBTreeBlack true
  7. struct TRbTreeNodeBase {
  8. using TColorType = TRbTreeColorType;
  9. using TBasePtr = TRbTreeNodeBase*;
  10. TColorType Color_;
  11. TBasePtr Parent_;
  12. TBasePtr Left_;
  13. TBasePtr Right_;
  14. size_t Children_;
  15. inline TRbTreeNodeBase() noexcept {
  16. ReInitNode();
  17. }
  18. inline void ReInitNode() noexcept {
  19. Color_ = RBTreeBlack;
  20. Parent_ = nullptr;
  21. Left_ = nullptr;
  22. Right_ = nullptr;
  23. Children_ = 1;
  24. }
  25. static TBasePtr MinimumNode(TBasePtr x) {
  26. while (x->Left_ != nullptr)
  27. x = x->Left_;
  28. return x;
  29. }
  30. static TBasePtr MaximumNode(TBasePtr x) {
  31. while (x->Right_ != nullptr)
  32. x = x->Right_;
  33. return x;
  34. }
  35. static TBasePtr ByIndex(TBasePtr x, size_t index) {
  36. if (x->Left_ != nullptr) {
  37. if (index < x->Left_->Children_)
  38. return ByIndex(x->Left_, index);
  39. index -= x->Left_->Children_;
  40. }
  41. if (0 == index)
  42. return x;
  43. if (!x->Right_)
  44. ythrow yexception() << "index not found";
  45. return ByIndex(x->Right_, index - 1);
  46. }
  47. };
  48. struct TRbTreeBaseIterator;
  49. template <class TDummy>
  50. class TRbGlobal {
  51. public:
  52. using TBasePtr = TRbTreeNodeBase*;
  53. static void Rebalance(TBasePtr x, TBasePtr& root);
  54. static TBasePtr RebalanceForErase(TBasePtr z, TBasePtr& root, TBasePtr& leftmost, TBasePtr& rightmost);
  55. static void DecrementChildrenUntilRoot(TBasePtr x, TBasePtr root);
  56. static void RecalcChildren(TBasePtr x);
  57. static TBasePtr IncrementNode(TBasePtr);
  58. static TBasePtr DecrementNode(TBasePtr);
  59. static void RotateLeft(TBasePtr x, TBasePtr& root);
  60. static void RotateRight(TBasePtr x, TBasePtr& root);
  61. };
  62. using TRbGlobalInst = TRbGlobal<bool>;
  63. struct TRbTreeBaseIterator {
  64. using TBasePtr = TRbTreeNodeBase*;
  65. TBasePtr Node_;
  66. inline TRbTreeBaseIterator(TBasePtr x = nullptr) noexcept
  67. : Node_(x)
  68. {
  69. }
  70. };
  71. template <class TValue, class TTraits>
  72. struct TRbTreeIterator: public TRbTreeBaseIterator {
  73. using TReference = typename TTraits::TReference;
  74. using TPointer = typename TTraits::TPointer;
  75. using TSelf = TRbTreeIterator<TValue, TTraits>;
  76. using TBasePtr = TRbTreeNodeBase*;
  77. inline TRbTreeIterator() noexcept = default;
  78. template <class T1>
  79. inline TRbTreeIterator(const T1& x) noexcept
  80. : TRbTreeBaseIterator(x)
  81. {
  82. }
  83. inline TReference operator*() const noexcept {
  84. return *static_cast<TValue*>(Node_);
  85. }
  86. inline TPointer operator->() const noexcept {
  87. return static_cast<TValue*>(Node_);
  88. }
  89. inline TSelf& operator++() noexcept {
  90. Node_ = TRbGlobalInst::IncrementNode(Node_);
  91. return *this;
  92. }
  93. inline TSelf operator++(int) noexcept {
  94. TSelf tmp = *this;
  95. ++(*this);
  96. return tmp;
  97. }
  98. inline TSelf& operator--() noexcept {
  99. Node_ = TRbGlobalInst::DecrementNode(Node_);
  100. return *this;
  101. }
  102. inline TSelf operator--(int) noexcept {
  103. TSelf tmp = *this;
  104. --(*this);
  105. return tmp;
  106. }
  107. template <class T1>
  108. inline bool operator==(const T1& rhs) const noexcept {
  109. return Node_ == rhs.Node_;
  110. }
  111. template <class T1>
  112. inline bool operator!=(const T1& rhs) const noexcept {
  113. return Node_ != rhs.Node_;
  114. }
  115. };
  116. template <class TValue, class TCmp>
  117. class TRbTree {
  118. struct TCmpAdaptor: public TCmp {
  119. inline TCmpAdaptor() noexcept = default;
  120. inline TCmpAdaptor(const TCmp& cmp) noexcept
  121. : TCmp(cmp)
  122. {
  123. }
  124. template <class T1, class T2>
  125. inline bool operator()(const T1& l, const T2& r) const {
  126. return TCmp::Compare(l, r);
  127. }
  128. };
  129. struct TNonConstTraits {
  130. using TReference = TValue&;
  131. using TPointer = TValue*;
  132. };
  133. struct TConstTraits {
  134. using TReference = const TValue&;
  135. using TPointer = const TValue*;
  136. };
  137. using TNodeBase = TRbTreeNodeBase;
  138. using TBasePtr = TRbTreeNodeBase*;
  139. using TColorType = TRbTreeColorType;
  140. public:
  141. class TRealNode: public TNodeBase {
  142. public:
  143. inline TRealNode()
  144. : Tree_(nullptr)
  145. {
  146. }
  147. inline ~TRealNode() {
  148. UnLink();
  149. }
  150. inline void UnLink() noexcept {
  151. if (Tree_) {
  152. Tree_->EraseImpl(this);
  153. ReInitNode();
  154. Tree_ = nullptr;
  155. }
  156. }
  157. inline void SetRbTreeParent(TRbTree* parent) noexcept {
  158. Tree_ = parent;
  159. }
  160. inline TRbTree* ParentTree() const noexcept {
  161. return Tree_;
  162. }
  163. private:
  164. TRbTree* Tree_;
  165. };
  166. using TIterator = TRbTreeIterator<TValue, TNonConstTraits>;
  167. using TConstIterator = TRbTreeIterator<TValue, TConstTraits>;
  168. inline TRbTree() noexcept {
  169. Init();
  170. }
  171. inline TRbTree(const TCmp& cmp) noexcept
  172. : KeyCompare_(cmp)
  173. {
  174. Init();
  175. }
  176. inline void Init() noexcept {
  177. Data_.Color_ = RBTreeRed;
  178. Data_.Parent_ = nullptr;
  179. Data_.Left_ = &Data_;
  180. Data_.Right_ = &Data_;
  181. Data_.Children_ = 0;
  182. }
  183. struct TDestroy {
  184. inline void operator()(TValue& v) const noexcept {
  185. v.SetRbTreeParent(nullptr);
  186. v.ReInitNode();
  187. }
  188. };
  189. inline ~TRbTree() {
  190. ForEachNoOrder(TDestroy());
  191. }
  192. inline void Clear() noexcept {
  193. ForEachNoOrder(TDestroy());
  194. Init();
  195. }
  196. template <class F>
  197. inline void ForEachNoOrder(const F& f) {
  198. ForEachNoOrder(Root(), f);
  199. }
  200. template <class F>
  201. inline void ForEachNoOrder(TNodeBase* n, const F& f) {
  202. if (n && n != &Data_) {
  203. ForEachNoOrder(n->Left_, f);
  204. ForEachNoOrder(n->Right_, f);
  205. f(ValueNode(n));
  206. }
  207. }
  208. inline TIterator Begin() noexcept {
  209. return LeftMost();
  210. }
  211. inline TConstIterator Begin() const noexcept {
  212. return LeftMost();
  213. }
  214. inline TIterator End() noexcept {
  215. return &this->Data_;
  216. }
  217. inline TConstIterator End() const noexcept {
  218. return const_cast<TBasePtr>(&this->Data_);
  219. }
  220. inline bool Empty() const noexcept {
  221. return this->Begin() == this->End();
  222. }
  223. inline explicit operator bool() const noexcept {
  224. return !this->Empty();
  225. }
  226. inline TIterator Insert(TValue* val) {
  227. return Insert(*val);
  228. }
  229. inline TIterator Insert(TValue& val) {
  230. val.UnLink();
  231. TBasePtr y = &this->Data_;
  232. TBasePtr x = Root();
  233. while (x != nullptr) {
  234. ++(x->Children_);
  235. y = x;
  236. if (KeyCompare_(ValueNode(&val), ValueNode(x))) {
  237. x = LeftNode(x);
  238. } else {
  239. x = RightNode(x);
  240. }
  241. }
  242. return InsertImpl(y, &val, x);
  243. }
  244. template <class F>
  245. inline void ForEach(F& f) {
  246. TIterator it = Begin();
  247. while (it != End()) {
  248. f(*it++);
  249. }
  250. }
  251. inline void Erase(TValue& val) noexcept {
  252. val.UnLink();
  253. }
  254. inline void Erase(TValue* val) noexcept {
  255. Erase(*val);
  256. }
  257. inline void Erase(TIterator pos) noexcept {
  258. Erase(*pos);
  259. }
  260. inline void EraseImpl(TNodeBase* val) noexcept {
  261. TRbGlobalInst::RebalanceForErase(val, this->Data_.Parent_, this->Data_.Left_, this->Data_.Right_);
  262. }
  263. template <class T1>
  264. inline TValue* Find(const T1& k) const {
  265. TBasePtr y = nullptr;
  266. TBasePtr x = Root(); // Current node.
  267. while (x != nullptr)
  268. if (!KeyCompare_(ValueNode(x), k))
  269. y = x, x = LeftNode(x);
  270. else
  271. x = RightNode(x);
  272. if (y) {
  273. if (KeyCompare_(k, ValueNode(y))) {
  274. y = nullptr;
  275. }
  276. }
  277. return static_cast<TValue*>(y);
  278. }
  279. size_t GetIndex(TBasePtr x) const {
  280. size_t index = 0;
  281. if (x->Left_ != nullptr) {
  282. index += x->Left_->Children_;
  283. }
  284. while (x != nullptr && x->Parent_ != nullptr && x->Parent_ != const_cast<TBasePtr>(&this->Data_)) {
  285. if (x->Parent_->Right_ == x && x->Parent_->Left_ != nullptr) {
  286. index += x->Parent_->Left_->Children_;
  287. }
  288. if (x->Parent_->Right_ == x) {
  289. index += 1;
  290. }
  291. x = x->Parent_;
  292. }
  293. return index;
  294. }
  295. template <class T1>
  296. inline TBasePtr LowerBound(const T1& k) const {
  297. TBasePtr y = const_cast<TBasePtr>(&this->Data_); /* Last node which is not less than k. */
  298. TBasePtr x = Root(); /* Current node. */
  299. while (x != nullptr)
  300. if (!KeyCompare_(ValueNode(x), k))
  301. y = x, x = LeftNode(x);
  302. else
  303. x = RightNode(x);
  304. return y;
  305. }
  306. template <class T1>
  307. inline TBasePtr UpperBound(const T1& k) const {
  308. TBasePtr y = const_cast<TBasePtr>(&this->Data_); /* Last node which is greater than k. */
  309. TBasePtr x = Root(); /* Current node. */
  310. while (x != nullptr)
  311. if (KeyCompare_(k, ValueNode(x)))
  312. y = x, x = LeftNode(x);
  313. else
  314. x = RightNode(x);
  315. return y;
  316. }
  317. template <class T1>
  318. inline size_t LessCount(const T1& k) const {
  319. auto x = LowerBound(k);
  320. if (x == const_cast<TBasePtr>(&this->Data_)) {
  321. if (const auto root = Root()) {
  322. return root->Children_;
  323. } else {
  324. return 0;
  325. }
  326. } else {
  327. return GetIndex(x);
  328. }
  329. }
  330. template <class T1>
  331. inline size_t NotLessCount(const T1& k) const {
  332. return Root()->Children_ - LessCount<T1>(k);
  333. }
  334. template <class T1>
  335. inline size_t GreaterCount(const T1& k) const {
  336. auto x = UpperBound(k);
  337. if (x == const_cast<TBasePtr>(&this->Data_)) {
  338. return 0;
  339. } else {
  340. return Root()->Children_ - GetIndex(x);
  341. }
  342. }
  343. template <class T1>
  344. inline size_t NotGreaterCount(const T1& k) const {
  345. return Root()->Children_ - GreaterCount<T1>(k);
  346. }
  347. TValue* ByIndex(size_t index) {
  348. return static_cast<TValue*>(TRbTreeNodeBase::ByIndex(Root(), index));
  349. }
  350. private:
  351. // CRP 7/10/00 inserted argument on_right, which is another hint (meant to
  352. // act like on_left and ignore a portion of the if conditions -- specify
  353. // on_right != nullptr to bypass comparison as false or on_left != nullptr to bypass
  354. // comparison as true)
  355. TIterator InsertImpl(TRbTreeNodeBase* parent, TRbTreeNodeBase* val, TRbTreeNodeBase* on_left = nullptr, TRbTreeNodeBase* on_right = nullptr) {
  356. ValueNode(val).SetRbTreeParent(this);
  357. TBasePtr new_node = val;
  358. if (parent == &this->Data_) {
  359. LeftNode(parent) = new_node;
  360. // also makes LeftMost() = new_node
  361. Root() = new_node;
  362. RightMost() = new_node;
  363. } else if (on_right == nullptr &&
  364. // If on_right != nullptr, the remainder fails to false
  365. (on_left != nullptr ||
  366. // If on_left != nullptr, the remainder succeeds to true
  367. KeyCompare_(ValueNode(val), ValueNode(parent))))
  368. {
  369. LeftNode(parent) = new_node;
  370. if (parent == LeftMost())
  371. // maintain LeftMost() pointing to min node
  372. LeftMost() = new_node;
  373. } else {
  374. RightNode(parent) = new_node;
  375. if (parent == RightMost())
  376. // maintain RightMost() pointing to max node
  377. RightMost() = new_node;
  378. }
  379. ParentNode(new_node) = parent;
  380. TRbGlobalInst::Rebalance(new_node, this->Data_.Parent_);
  381. return new_node;
  382. }
  383. TBasePtr Root() const {
  384. return this->Data_.Parent_;
  385. }
  386. TBasePtr LeftMost() const {
  387. return this->Data_.Left_;
  388. }
  389. TBasePtr RightMost() const {
  390. return this->Data_.Right_;
  391. }
  392. TBasePtr& Root() {
  393. return this->Data_.Parent_;
  394. }
  395. TBasePtr& LeftMost() {
  396. return this->Data_.Left_;
  397. }
  398. TBasePtr& RightMost() {
  399. return this->Data_.Right_;
  400. }
  401. static TBasePtr& LeftNode(TBasePtr x) {
  402. return x->Left_;
  403. }
  404. static TBasePtr& RightNode(TBasePtr x) {
  405. return x->Right_;
  406. }
  407. static TBasePtr& ParentNode(TBasePtr x) {
  408. return x->Parent_;
  409. }
  410. static TValue& ValueNode(TBasePtr x) {
  411. return *static_cast<TValue*>(x);
  412. }
  413. static TBasePtr MinimumNode(TBasePtr x) {
  414. return TRbTreeNodeBase::MinimumNode(x);
  415. }
  416. static TBasePtr MaximumNode(TBasePtr x) {
  417. return TRbTreeNodeBase::MaximumNode(x);
  418. }
  419. private:
  420. TCmpAdaptor KeyCompare_;
  421. TNodeBase Data_;
  422. };
  423. template <class TValue, class TCmp>
  424. class TRbTreeItem: public TRbTree<TValue, TCmp>::TRealNode {
  425. };
  426. template <class TDummy>
  427. void TRbGlobal<TDummy>::RotateLeft(TRbTreeNodeBase* x, TRbTreeNodeBase*& root) {
  428. TRbTreeNodeBase* y = x->Right_;
  429. x->Right_ = y->Left_;
  430. if (y->Left_ != nullptr)
  431. y->Left_->Parent_ = x;
  432. y->Parent_ = x->Parent_;
  433. if (x == root)
  434. root = y;
  435. else if (x == x->Parent_->Left_)
  436. x->Parent_->Left_ = y;
  437. else
  438. x->Parent_->Right_ = y;
  439. y->Left_ = x;
  440. x->Parent_ = y;
  441. y->Children_ = x->Children_;
  442. x->Children_ = ((x->Left_) ? x->Left_->Children_ : 0) + ((x->Right_) ? x->Right_->Children_ : 0) + 1;
  443. }
  444. template <class TDummy>
  445. void TRbGlobal<TDummy>::RotateRight(TRbTreeNodeBase* x, TRbTreeNodeBase*& root) {
  446. TRbTreeNodeBase* y = x->Left_;
  447. x->Left_ = y->Right_;
  448. if (y->Right_ != nullptr)
  449. y->Right_->Parent_ = x;
  450. y->Parent_ = x->Parent_;
  451. if (x == root)
  452. root = y;
  453. else if (x == x->Parent_->Right_)
  454. x->Parent_->Right_ = y;
  455. else
  456. x->Parent_->Left_ = y;
  457. y->Right_ = x;
  458. x->Parent_ = y;
  459. y->Children_ = x->Children_;
  460. x->Children_ = ((x->Left_) ? x->Left_->Children_ : 0) + ((x->Right_) ? x->Right_->Children_ : 0) + 1;
  461. }
  462. template <class TDummy>
  463. void TRbGlobal<TDummy>::Rebalance(TRbTreeNodeBase* x, TRbTreeNodeBase*& root) {
  464. x->Color_ = RBTreeRed;
  465. while (x != root && x->Parent_->Color_ == RBTreeRed) {
  466. if (x->Parent_ == x->Parent_->Parent_->Left_) {
  467. TRbTreeNodeBase* y = x->Parent_->Parent_->Right_;
  468. if (y && y->Color_ == RBTreeRed) {
  469. x->Parent_->Color_ = RBTreeBlack;
  470. y->Color_ = RBTreeBlack;
  471. x->Parent_->Parent_->Color_ = RBTreeRed;
  472. x = x->Parent_->Parent_;
  473. } else {
  474. if (x == x->Parent_->Right_) {
  475. x = x->Parent_;
  476. RotateLeft(x, root);
  477. }
  478. x->Parent_->Color_ = RBTreeBlack;
  479. x->Parent_->Parent_->Color_ = RBTreeRed;
  480. RotateRight(x->Parent_->Parent_, root);
  481. }
  482. } else {
  483. TRbTreeNodeBase* y = x->Parent_->Parent_->Left_;
  484. if (y && y->Color_ == RBTreeRed) {
  485. x->Parent_->Color_ = RBTreeBlack;
  486. y->Color_ = RBTreeBlack;
  487. x->Parent_->Parent_->Color_ = RBTreeRed;
  488. x = x->Parent_->Parent_;
  489. } else {
  490. if (x == x->Parent_->Left_) {
  491. x = x->Parent_;
  492. RotateRight(x, root);
  493. }
  494. x->Parent_->Color_ = RBTreeBlack;
  495. x->Parent_->Parent_->Color_ = RBTreeRed;
  496. RotateLeft(x->Parent_->Parent_, root);
  497. }
  498. }
  499. }
  500. root->Color_ = RBTreeBlack;
  501. }
  502. template <class TDummy>
  503. void TRbGlobal<TDummy>::RecalcChildren(TRbTreeNodeBase* x) {
  504. x->Children_ = ((x->Left_) ? x->Left_->Children_ : 0) + ((x->Right_) ? x->Right_->Children_ : 0) + 1;
  505. }
  506. template <class TDummy>
  507. void TRbGlobal<TDummy>::DecrementChildrenUntilRoot(TRbTreeNodeBase* x, TRbTreeNodeBase* root) {
  508. auto* ptr = x;
  509. --ptr->Children_;
  510. while (ptr != root) {
  511. ptr = ptr->Parent_;
  512. --ptr->Children_;
  513. }
  514. }
  515. template <class TDummy>
  516. TRbTreeNodeBase* TRbGlobal<TDummy>::RebalanceForErase(TRbTreeNodeBase* z,
  517. TRbTreeNodeBase*& root,
  518. TRbTreeNodeBase*& leftmost,
  519. TRbTreeNodeBase*& rightmost) {
  520. TRbTreeNodeBase* y = z;
  521. TRbTreeNodeBase* x;
  522. TRbTreeNodeBase* x_parent;
  523. if (y->Left_ == nullptr) // z has at most one non-null child. y == z.
  524. x = y->Right_; // x might be null.
  525. else {
  526. if (y->Right_ == nullptr) // z has exactly one non-null child. y == z.
  527. x = y->Left_; // x is not null.
  528. else { // z has two non-null children. Set y to
  529. y = TRbTreeNodeBase::MinimumNode(y->Right_); // z's successor. x might be null.
  530. x = y->Right_;
  531. }
  532. }
  533. if (y != z) {
  534. // relink y in place of z. y is z's successor
  535. z->Left_->Parent_ = y;
  536. y->Left_ = z->Left_;
  537. if (y != z->Right_) {
  538. x_parent = y->Parent_;
  539. if (x)
  540. x->Parent_ = y->Parent_;
  541. y->Parent_->Left_ = x; // y must be a child of mLeft
  542. y->Right_ = z->Right_;
  543. z->Right_->Parent_ = y;
  544. } else
  545. x_parent = y;
  546. if (root == z)
  547. root = y;
  548. else if (z->Parent_->Left_ == z)
  549. z->Parent_->Left_ = y;
  550. else
  551. z->Parent_->Right_ = y;
  552. y->Parent_ = z->Parent_;
  553. DoSwap(y->Color_, z->Color_);
  554. RecalcChildren(y);
  555. if (x_parent != y) {
  556. --x_parent->Children_;
  557. }
  558. if (x_parent != root) {
  559. DecrementChildrenUntilRoot(x_parent->Parent_, root);
  560. }
  561. y = z;
  562. // y now points to node to be actually deleted
  563. } else {
  564. // y == z
  565. x_parent = y->Parent_;
  566. if (x)
  567. x->Parent_ = y->Parent_;
  568. if (root == z)
  569. root = x;
  570. else {
  571. if (z->Parent_->Left_ == z)
  572. z->Parent_->Left_ = x;
  573. else
  574. z->Parent_->Right_ = x;
  575. DecrementChildrenUntilRoot(z->Parent_, root); // we lost y
  576. }
  577. if (leftmost == z) {
  578. if (z->Right_ == nullptr) // z->mLeft must be null also
  579. leftmost = z->Parent_;
  580. // makes leftmost == _M_header if z == root
  581. else
  582. leftmost = TRbTreeNodeBase::MinimumNode(x);
  583. }
  584. if (rightmost == z) {
  585. if (z->Left_ == nullptr) // z->mRight must be null also
  586. rightmost = z->Parent_;
  587. // makes rightmost == _M_header if z == root
  588. else // x == z->mLeft
  589. rightmost = TRbTreeNodeBase::MaximumNode(x);
  590. }
  591. }
  592. if (y->Color_ != RBTreeRed) {
  593. while (x != root && (x == nullptr || x->Color_ == RBTreeBlack))
  594. if (x == x_parent->Left_) {
  595. TRbTreeNodeBase* w = x_parent->Right_;
  596. if (w->Color_ == RBTreeRed) {
  597. w->Color_ = RBTreeBlack;
  598. x_parent->Color_ = RBTreeRed;
  599. RotateLeft(x_parent, root);
  600. w = x_parent->Right_;
  601. }
  602. if ((w->Left_ == nullptr ||
  603. w->Left_->Color_ == RBTreeBlack) &&
  604. (w->Right_ == nullptr ||
  605. w->Right_->Color_ == RBTreeBlack))
  606. {
  607. w->Color_ = RBTreeRed;
  608. x = x_parent;
  609. x_parent = x_parent->Parent_;
  610. } else {
  611. if (w->Right_ == nullptr || w->Right_->Color_ == RBTreeBlack) {
  612. if (w->Left_)
  613. w->Left_->Color_ = RBTreeBlack;
  614. w->Color_ = RBTreeRed;
  615. RotateRight(w, root);
  616. w = x_parent->Right_;
  617. }
  618. w->Color_ = x_parent->Color_;
  619. x_parent->Color_ = RBTreeBlack;
  620. if (w->Right_)
  621. w->Right_->Color_ = RBTreeBlack;
  622. RotateLeft(x_parent, root);
  623. break;
  624. }
  625. } else {
  626. // same as above, with mRight <-> mLeft.
  627. TRbTreeNodeBase* w = x_parent->Left_;
  628. if (w->Color_ == RBTreeRed) {
  629. w->Color_ = RBTreeBlack;
  630. x_parent->Color_ = RBTreeRed;
  631. RotateRight(x_parent, root);
  632. w = x_parent->Left_;
  633. }
  634. if ((w->Right_ == nullptr ||
  635. w->Right_->Color_ == RBTreeBlack) &&
  636. (w->Left_ == nullptr ||
  637. w->Left_->Color_ == RBTreeBlack))
  638. {
  639. w->Color_ = RBTreeRed;
  640. x = x_parent;
  641. x_parent = x_parent->Parent_;
  642. } else {
  643. if (w->Left_ == nullptr || w->Left_->Color_ == RBTreeBlack) {
  644. if (w->Right_)
  645. w->Right_->Color_ = RBTreeBlack;
  646. w->Color_ = RBTreeRed;
  647. RotateLeft(w, root);
  648. w = x_parent->Left_;
  649. }
  650. w->Color_ = x_parent->Color_;
  651. x_parent->Color_ = RBTreeBlack;
  652. if (w->Left_)
  653. w->Left_->Color_ = RBTreeBlack;
  654. RotateRight(x_parent, root);
  655. break;
  656. }
  657. }
  658. if (x)
  659. x->Color_ = RBTreeBlack;
  660. }
  661. return y;
  662. }
  663. template <class TDummy>
  664. TRbTreeNodeBase* TRbGlobal<TDummy>::DecrementNode(TRbTreeNodeBase* Node_) {
  665. if (Node_->Color_ == RBTreeRed && Node_->Parent_->Parent_ == Node_)
  666. Node_ = Node_->Right_;
  667. else if (Node_->Left_ != nullptr) {
  668. Node_ = TRbTreeNodeBase::MaximumNode(Node_->Left_);
  669. } else {
  670. TBasePtr y = Node_->Parent_;
  671. while (Node_ == y->Left_) {
  672. Node_ = y;
  673. y = y->Parent_;
  674. }
  675. Node_ = y;
  676. }
  677. return Node_;
  678. }
  679. template <class TDummy>
  680. TRbTreeNodeBase* TRbGlobal<TDummy>::IncrementNode(TRbTreeNodeBase* Node_) {
  681. if (Node_->Right_ != nullptr) {
  682. Node_ = TRbTreeNodeBase::MinimumNode(Node_->Right_);
  683. } else {
  684. TBasePtr y = Node_->Parent_;
  685. while (Node_ == y->Right_) {
  686. Node_ = y;
  687. y = y->Parent_;
  688. }
  689. // check special case: This is necessary if mNode is the
  690. // _M_head and the tree contains only a single node y. In
  691. // that case parent, left and right all point to y!
  692. if (Node_->Right_ != y)
  693. Node_ = y;
  694. }
  695. return Node_;
  696. }
  697. #undef RBTreeRed
  698. #undef RBTreeBlack