tree.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /* $NetBSD: tree.h,v 1.8 2004/03/28 19:38:30 provos Exp $ */
  2. /* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */
  3. /* $FreeBSD$ */
  4. /*-
  5. * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #pragma once
  29. /*
  30. * This file defines data structures for red-black trees.
  31. *
  32. * A red-black tree is a binary search tree with the node color as an
  33. * extra attribute. It fulfills a set of conditions:
  34. * - every search path from the root to a leaf consists of the
  35. * same number of black nodes,
  36. * - each red node (except for the root) has a black parent,
  37. * - each leaf node is black.
  38. *
  39. * Every operation on a red-black tree is bounded as O(lg n).
  40. * The maximum height of a red-black tree is 2lg (n+1).
  41. */
  42. #include "common/assert.h"
  43. namespace Common {
  44. template <typename T>
  45. class RBHead {
  46. public:
  47. [[nodiscard]] T* Root() {
  48. return rbh_root;
  49. }
  50. [[nodiscard]] const T* Root() const {
  51. return rbh_root;
  52. }
  53. void SetRoot(T* root) {
  54. rbh_root = root;
  55. }
  56. [[nodiscard]] bool IsEmpty() const {
  57. return Root() == nullptr;
  58. }
  59. private:
  60. T* rbh_root = nullptr;
  61. };
  62. enum class EntryColor {
  63. Black,
  64. Red,
  65. };
  66. template <typename T>
  67. class RBEntry {
  68. public:
  69. [[nodiscard]] T* Left() {
  70. return rbe_left;
  71. }
  72. [[nodiscard]] const T* Left() const {
  73. return rbe_left;
  74. }
  75. void SetLeft(T* left) {
  76. rbe_left = left;
  77. }
  78. [[nodiscard]] T* Right() {
  79. return rbe_right;
  80. }
  81. [[nodiscard]] const T* Right() const {
  82. return rbe_right;
  83. }
  84. void SetRight(T* right) {
  85. rbe_right = right;
  86. }
  87. [[nodiscard]] T* Parent() {
  88. return rbe_parent;
  89. }
  90. [[nodiscard]] const T* Parent() const {
  91. return rbe_parent;
  92. }
  93. void SetParent(T* parent) {
  94. rbe_parent = parent;
  95. }
  96. [[nodiscard]] bool IsBlack() const {
  97. return rbe_color == EntryColor::Black;
  98. }
  99. [[nodiscard]] bool IsRed() const {
  100. return rbe_color == EntryColor::Red;
  101. }
  102. [[nodiscard]] EntryColor Color() const {
  103. return rbe_color;
  104. }
  105. void SetColor(EntryColor color) {
  106. rbe_color = color;
  107. }
  108. private:
  109. T* rbe_left = nullptr;
  110. T* rbe_right = nullptr;
  111. T* rbe_parent = nullptr;
  112. EntryColor rbe_color{};
  113. };
  114. template <typename Node>
  115. [[nodiscard]] RBEntry<Node>& RB_ENTRY(Node* node) {
  116. return node->GetEntry();
  117. }
  118. template <typename Node>
  119. [[nodiscard]] const RBEntry<Node>& RB_ENTRY(const Node* node) {
  120. return node->GetEntry();
  121. }
  122. template <typename Node>
  123. [[nodiscard]] Node* RB_PARENT(Node* node) {
  124. return RB_ENTRY(node).Parent();
  125. }
  126. template <typename Node>
  127. [[nodiscard]] const Node* RB_PARENT(const Node* node) {
  128. return RB_ENTRY(node).Parent();
  129. }
  130. template <typename Node>
  131. void RB_SET_PARENT(Node* node, Node* parent) {
  132. return RB_ENTRY(node).SetParent(parent);
  133. }
  134. template <typename Node>
  135. [[nodiscard]] Node* RB_LEFT(Node* node) {
  136. return RB_ENTRY(node).Left();
  137. }
  138. template <typename Node>
  139. [[nodiscard]] const Node* RB_LEFT(const Node* node) {
  140. return RB_ENTRY(node).Left();
  141. }
  142. template <typename Node>
  143. void RB_SET_LEFT(Node* node, Node* left) {
  144. return RB_ENTRY(node).SetLeft(left);
  145. }
  146. template <typename Node>
  147. [[nodiscard]] Node* RB_RIGHT(Node* node) {
  148. return RB_ENTRY(node).Right();
  149. }
  150. template <typename Node>
  151. [[nodiscard]] const Node* RB_RIGHT(const Node* node) {
  152. return RB_ENTRY(node).Right();
  153. }
  154. template <typename Node>
  155. void RB_SET_RIGHT(Node* node, Node* right) {
  156. return RB_ENTRY(node).SetRight(right);
  157. }
  158. template <typename Node>
  159. [[nodiscard]] bool RB_IS_BLACK(const Node* node) {
  160. return RB_ENTRY(node).IsBlack();
  161. }
  162. template <typename Node>
  163. [[nodiscard]] bool RB_IS_RED(const Node* node) {
  164. return RB_ENTRY(node).IsRed();
  165. }
  166. template <typename Node>
  167. [[nodiscard]] EntryColor RB_COLOR(const Node* node) {
  168. return RB_ENTRY(node).Color();
  169. }
  170. template <typename Node>
  171. void RB_SET_COLOR(Node* node, EntryColor color) {
  172. return RB_ENTRY(node).SetColor(color);
  173. }
  174. template <typename Node>
  175. void RB_SET(Node* node, Node* parent) {
  176. auto& entry = RB_ENTRY(node);
  177. entry.SetParent(parent);
  178. entry.SetLeft(nullptr);
  179. entry.SetRight(nullptr);
  180. entry.SetColor(EntryColor::Red);
  181. }
  182. template <typename Node>
  183. void RB_SET_BLACKRED(Node* black, Node* red) {
  184. RB_SET_COLOR(black, EntryColor::Black);
  185. RB_SET_COLOR(red, EntryColor::Red);
  186. }
  187. template <typename Node>
  188. void RB_ROTATE_LEFT(RBHead<Node>* head, Node* elm, Node*& tmp) {
  189. tmp = RB_RIGHT(elm);
  190. RB_SET_RIGHT(elm, RB_LEFT(tmp));
  191. if (RB_RIGHT(elm) != nullptr) {
  192. RB_SET_PARENT(RB_LEFT(tmp), elm);
  193. }
  194. RB_SET_PARENT(tmp, RB_PARENT(elm));
  195. if (RB_PARENT(tmp) != nullptr) {
  196. if (elm == RB_LEFT(RB_PARENT(elm))) {
  197. RB_SET_LEFT(RB_PARENT(elm), tmp);
  198. } else {
  199. RB_SET_RIGHT(RB_PARENT(elm), tmp);
  200. }
  201. } else {
  202. head->SetRoot(tmp);
  203. }
  204. RB_SET_LEFT(tmp, elm);
  205. RB_SET_PARENT(elm, tmp);
  206. }
  207. template <typename Node>
  208. void RB_ROTATE_RIGHT(RBHead<Node>* head, Node* elm, Node*& tmp) {
  209. tmp = RB_LEFT(elm);
  210. RB_SET_LEFT(elm, RB_RIGHT(tmp));
  211. if (RB_LEFT(elm) != nullptr) {
  212. RB_SET_PARENT(RB_RIGHT(tmp), elm);
  213. }
  214. RB_SET_PARENT(tmp, RB_PARENT(elm));
  215. if (RB_PARENT(tmp) != nullptr) {
  216. if (elm == RB_LEFT(RB_PARENT(elm))) {
  217. RB_SET_LEFT(RB_PARENT(elm), tmp);
  218. } else {
  219. RB_SET_RIGHT(RB_PARENT(elm), tmp);
  220. }
  221. } else {
  222. head->SetRoot(tmp);
  223. }
  224. RB_SET_RIGHT(tmp, elm);
  225. RB_SET_PARENT(elm, tmp);
  226. }
  227. template <typename Node>
  228. void RB_INSERT_COLOR(RBHead<Node>* head, Node* elm) {
  229. Node* parent = nullptr;
  230. Node* tmp = nullptr;
  231. while ((parent = RB_PARENT(elm)) != nullptr && RB_IS_RED(parent)) {
  232. Node* gparent = RB_PARENT(parent);
  233. if (parent == RB_LEFT(gparent)) {
  234. tmp = RB_RIGHT(gparent);
  235. if (tmp && RB_IS_RED(tmp)) {
  236. RB_SET_COLOR(tmp, EntryColor::Black);
  237. RB_SET_BLACKRED(parent, gparent);
  238. elm = gparent;
  239. continue;
  240. }
  241. if (RB_RIGHT(parent) == elm) {
  242. RB_ROTATE_LEFT(head, parent, tmp);
  243. tmp = parent;
  244. parent = elm;
  245. elm = tmp;
  246. }
  247. RB_SET_BLACKRED(parent, gparent);
  248. RB_ROTATE_RIGHT(head, gparent, tmp);
  249. } else {
  250. tmp = RB_LEFT(gparent);
  251. if (tmp && RB_IS_RED(tmp)) {
  252. RB_SET_COLOR(tmp, EntryColor::Black);
  253. RB_SET_BLACKRED(parent, gparent);
  254. elm = gparent;
  255. continue;
  256. }
  257. if (RB_LEFT(parent) == elm) {
  258. RB_ROTATE_RIGHT(head, parent, tmp);
  259. tmp = parent;
  260. parent = elm;
  261. elm = tmp;
  262. }
  263. RB_SET_BLACKRED(parent, gparent);
  264. RB_ROTATE_LEFT(head, gparent, tmp);
  265. }
  266. }
  267. RB_SET_COLOR(head->Root(), EntryColor::Black);
  268. }
  269. template <typename Node>
  270. void RB_REMOVE_COLOR(RBHead<Node>* head, Node* parent, Node* elm) {
  271. Node* tmp;
  272. while ((elm == nullptr || RB_IS_BLACK(elm)) && elm != head->Root() && parent != nullptr) {
  273. if (RB_LEFT(parent) == elm) {
  274. tmp = RB_RIGHT(parent);
  275. if (!tmp) {
  276. ASSERT_MSG(false, "tmp is invalid!");
  277. break;
  278. }
  279. if (RB_IS_RED(tmp)) {
  280. RB_SET_BLACKRED(tmp, parent);
  281. RB_ROTATE_LEFT(head, parent, tmp);
  282. tmp = RB_RIGHT(parent);
  283. }
  284. if ((RB_LEFT(tmp) == nullptr || RB_IS_BLACK(RB_LEFT(tmp))) &&
  285. (RB_RIGHT(tmp) == nullptr || RB_IS_BLACK(RB_RIGHT(tmp)))) {
  286. RB_SET_COLOR(tmp, EntryColor::Red);
  287. elm = parent;
  288. parent = RB_PARENT(elm);
  289. } else {
  290. if (RB_RIGHT(tmp) == nullptr || RB_IS_BLACK(RB_RIGHT(tmp))) {
  291. Node* oleft;
  292. if ((oleft = RB_LEFT(tmp)) != nullptr) {
  293. RB_SET_COLOR(oleft, EntryColor::Black);
  294. }
  295. RB_SET_COLOR(tmp, EntryColor::Red);
  296. RB_ROTATE_RIGHT(head, tmp, oleft);
  297. tmp = RB_RIGHT(parent);
  298. }
  299. RB_SET_COLOR(tmp, RB_COLOR(parent));
  300. RB_SET_COLOR(parent, EntryColor::Black);
  301. if (RB_RIGHT(tmp)) {
  302. RB_SET_COLOR(RB_RIGHT(tmp), EntryColor::Black);
  303. }
  304. RB_ROTATE_LEFT(head, parent, tmp);
  305. elm = head->Root();
  306. break;
  307. }
  308. } else {
  309. tmp = RB_LEFT(parent);
  310. if (RB_IS_RED(tmp)) {
  311. RB_SET_BLACKRED(tmp, parent);
  312. RB_ROTATE_RIGHT(head, parent, tmp);
  313. tmp = RB_LEFT(parent);
  314. }
  315. if (!tmp) {
  316. ASSERT_MSG(false, "tmp is invalid!");
  317. break;
  318. }
  319. if ((RB_LEFT(tmp) == nullptr || RB_IS_BLACK(RB_LEFT(tmp))) &&
  320. (RB_RIGHT(tmp) == nullptr || RB_IS_BLACK(RB_RIGHT(tmp)))) {
  321. RB_SET_COLOR(tmp, EntryColor::Red);
  322. elm = parent;
  323. parent = RB_PARENT(elm);
  324. } else {
  325. if (RB_LEFT(tmp) == nullptr || RB_IS_BLACK(RB_LEFT(tmp))) {
  326. Node* oright;
  327. if ((oright = RB_RIGHT(tmp)) != nullptr) {
  328. RB_SET_COLOR(oright, EntryColor::Black);
  329. }
  330. RB_SET_COLOR(tmp, EntryColor::Red);
  331. RB_ROTATE_LEFT(head, tmp, oright);
  332. tmp = RB_LEFT(parent);
  333. }
  334. RB_SET_COLOR(tmp, RB_COLOR(parent));
  335. RB_SET_COLOR(parent, EntryColor::Black);
  336. if (RB_LEFT(tmp)) {
  337. RB_SET_COLOR(RB_LEFT(tmp), EntryColor::Black);
  338. }
  339. RB_ROTATE_RIGHT(head, parent, tmp);
  340. elm = head->Root();
  341. break;
  342. }
  343. }
  344. }
  345. if (elm) {
  346. RB_SET_COLOR(elm, EntryColor::Black);
  347. }
  348. }
  349. template <typename Node>
  350. Node* RB_REMOVE(RBHead<Node>* head, Node* elm) {
  351. Node* child = nullptr;
  352. Node* parent = nullptr;
  353. Node* old = elm;
  354. EntryColor color{};
  355. const auto finalize = [&] {
  356. if (color == EntryColor::Black) {
  357. RB_REMOVE_COLOR(head, parent, child);
  358. }
  359. return old;
  360. };
  361. if (RB_LEFT(elm) == nullptr) {
  362. child = RB_RIGHT(elm);
  363. } else if (RB_RIGHT(elm) == nullptr) {
  364. child = RB_LEFT(elm);
  365. } else {
  366. Node* left;
  367. elm = RB_RIGHT(elm);
  368. while ((left = RB_LEFT(elm)) != nullptr) {
  369. elm = left;
  370. }
  371. child = RB_RIGHT(elm);
  372. parent = RB_PARENT(elm);
  373. color = RB_COLOR(elm);
  374. if (child) {
  375. RB_SET_PARENT(child, parent);
  376. }
  377. if (parent) {
  378. if (RB_LEFT(parent) == elm) {
  379. RB_SET_LEFT(parent, child);
  380. } else {
  381. RB_SET_RIGHT(parent, child);
  382. }
  383. } else {
  384. head->SetRoot(child);
  385. }
  386. if (RB_PARENT(elm) == old) {
  387. parent = elm;
  388. }
  389. elm->SetEntry(old->GetEntry());
  390. if (RB_PARENT(old)) {
  391. if (RB_LEFT(RB_PARENT(old)) == old) {
  392. RB_SET_LEFT(RB_PARENT(old), elm);
  393. } else {
  394. RB_SET_RIGHT(RB_PARENT(old), elm);
  395. }
  396. } else {
  397. head->SetRoot(elm);
  398. }
  399. RB_SET_PARENT(RB_LEFT(old), elm);
  400. if (RB_RIGHT(old)) {
  401. RB_SET_PARENT(RB_RIGHT(old), elm);
  402. }
  403. if (parent) {
  404. left = parent;
  405. }
  406. return finalize();
  407. }
  408. parent = RB_PARENT(elm);
  409. color = RB_COLOR(elm);
  410. if (child) {
  411. RB_SET_PARENT(child, parent);
  412. }
  413. if (parent) {
  414. if (RB_LEFT(parent) == elm) {
  415. RB_SET_LEFT(parent, child);
  416. } else {
  417. RB_SET_RIGHT(parent, child);
  418. }
  419. } else {
  420. head->SetRoot(child);
  421. }
  422. return finalize();
  423. }
  424. // Inserts a node into the RB tree
  425. template <typename Node, typename CompareFunction>
  426. Node* RB_INSERT(RBHead<Node>* head, Node* elm, CompareFunction cmp) {
  427. Node* parent = nullptr;
  428. Node* tmp = head->Root();
  429. int comp = 0;
  430. while (tmp) {
  431. parent = tmp;
  432. comp = cmp(elm, parent);
  433. if (comp < 0) {
  434. tmp = RB_LEFT(tmp);
  435. } else if (comp > 0) {
  436. tmp = RB_RIGHT(tmp);
  437. } else {
  438. return tmp;
  439. }
  440. }
  441. RB_SET(elm, parent);
  442. if (parent != nullptr) {
  443. if (comp < 0) {
  444. RB_SET_LEFT(parent, elm);
  445. } else {
  446. RB_SET_RIGHT(parent, elm);
  447. }
  448. } else {
  449. head->SetRoot(elm);
  450. }
  451. RB_INSERT_COLOR(head, elm);
  452. return nullptr;
  453. }
  454. // Finds the node with the same key as elm
  455. template <typename Node, typename CompareFunction>
  456. Node* RB_FIND(RBHead<Node>* head, Node* elm, CompareFunction cmp) {
  457. Node* tmp = head->Root();
  458. while (tmp) {
  459. const int comp = cmp(elm, tmp);
  460. if (comp < 0) {
  461. tmp = RB_LEFT(tmp);
  462. } else if (comp > 0) {
  463. tmp = RB_RIGHT(tmp);
  464. } else {
  465. return tmp;
  466. }
  467. }
  468. return nullptr;
  469. }
  470. // Finds the first node greater than or equal to the search key
  471. template <typename Node, typename CompareFunction>
  472. Node* RB_NFIND(RBHead<Node>* head, Node* elm, CompareFunction cmp) {
  473. Node* tmp = head->Root();
  474. Node* res = nullptr;
  475. while (tmp) {
  476. const int comp = cmp(elm, tmp);
  477. if (comp < 0) {
  478. res = tmp;
  479. tmp = RB_LEFT(tmp);
  480. } else if (comp > 0) {
  481. tmp = RB_RIGHT(tmp);
  482. } else {
  483. return tmp;
  484. }
  485. }
  486. return res;
  487. }
  488. // Finds the node with the same key as lelm
  489. template <typename Node, typename CompareFunction>
  490. Node* RB_FIND_LIGHT(RBHead<Node>* head, const void* lelm, CompareFunction lcmp) {
  491. Node* tmp = head->Root();
  492. while (tmp) {
  493. const int comp = lcmp(lelm, tmp);
  494. if (comp < 0) {
  495. tmp = RB_LEFT(tmp);
  496. } else if (comp > 0) {
  497. tmp = RB_RIGHT(tmp);
  498. } else {
  499. return tmp;
  500. }
  501. }
  502. return nullptr;
  503. }
  504. // Finds the first node greater than or equal to the search key
  505. template <typename Node, typename CompareFunction>
  506. Node* RB_NFIND_LIGHT(RBHead<Node>* head, const void* lelm, CompareFunction lcmp) {
  507. Node* tmp = head->Root();
  508. Node* res = nullptr;
  509. while (tmp) {
  510. const int comp = lcmp(lelm, tmp);
  511. if (comp < 0) {
  512. res = tmp;
  513. tmp = RB_LEFT(tmp);
  514. } else if (comp > 0) {
  515. tmp = RB_RIGHT(tmp);
  516. } else {
  517. return tmp;
  518. }
  519. }
  520. return res;
  521. }
  522. template <typename Node>
  523. Node* RB_NEXT(Node* elm) {
  524. if (RB_RIGHT(elm)) {
  525. elm = RB_RIGHT(elm);
  526. while (RB_LEFT(elm)) {
  527. elm = RB_LEFT(elm);
  528. }
  529. } else {
  530. if (RB_PARENT(elm) && (elm == RB_LEFT(RB_PARENT(elm)))) {
  531. elm = RB_PARENT(elm);
  532. } else {
  533. while (RB_PARENT(elm) && (elm == RB_RIGHT(RB_PARENT(elm)))) {
  534. elm = RB_PARENT(elm);
  535. }
  536. elm = RB_PARENT(elm);
  537. }
  538. }
  539. return elm;
  540. }
  541. template <typename Node>
  542. Node* RB_PREV(Node* elm) {
  543. if (RB_LEFT(elm)) {
  544. elm = RB_LEFT(elm);
  545. while (RB_RIGHT(elm)) {
  546. elm = RB_RIGHT(elm);
  547. }
  548. } else {
  549. if (RB_PARENT(elm) && (elm == RB_RIGHT(RB_PARENT(elm)))) {
  550. elm = RB_PARENT(elm);
  551. } else {
  552. while (RB_PARENT(elm) && (elm == RB_LEFT(RB_PARENT(elm)))) {
  553. elm = RB_PARENT(elm);
  554. }
  555. elm = RB_PARENT(elm);
  556. }
  557. }
  558. return elm;
  559. }
  560. template <typename Node>
  561. Node* RB_MINMAX(RBHead<Node>* head, bool is_min) {
  562. Node* tmp = head->Root();
  563. Node* parent = nullptr;
  564. while (tmp) {
  565. parent = tmp;
  566. if (is_min) {
  567. tmp = RB_LEFT(tmp);
  568. } else {
  569. tmp = RB_RIGHT(tmp);
  570. }
  571. }
  572. return parent;
  573. }
  574. template <typename Node>
  575. Node* RB_MIN(RBHead<Node>* head) {
  576. return RB_MINMAX(head, true);
  577. }
  578. template <typename Node>
  579. Node* RB_MAX(RBHead<Node>* head) {
  580. return RB_MINMAX(head, false);
  581. }
  582. } // namespace Common