rbtree.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. Red Black Trees
  3. (C) 1999 Andrea Arcangeli <andrea@suse.de>
  4. (C) 2002 David Woodhouse <dwmw2@infradead.org>
  5. (C) 2012 Michel Lespinasse <walken@google.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. linux/lib/rbtree.c
  18. */
  19. #include <linux/rbtree_augmented.h>
  20. #include <linux/export.h>
  21. /*
  22. * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
  23. *
  24. * 1) A node is either red or black
  25. * 2) The root is black
  26. * 3) All leaves (NULL) are black
  27. * 4) Both children of every red node are black
  28. * 5) Every simple path from root to leaves contains the same number
  29. * of black nodes.
  30. *
  31. * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
  32. * consecutive red nodes in a path and every red node is therefore followed by
  33. * a black. So if B is the number of black nodes on every simple path (as per
  34. * 5), then the longest possible path due to 4 is 2B.
  35. *
  36. * We shall indicate color with case, where black nodes are uppercase and red
  37. * nodes will be lowercase. Unknown color nodes shall be drawn as red within
  38. * parentheses and have some accompanying text comment.
  39. */
  40. /*
  41. * Notes on lockless lookups:
  42. *
  43. * All stores to the tree structure (rb_left and rb_right) must be done using
  44. * WRITE_ONCE(). And we must not inadvertently cause (temporary) loops in the
  45. * tree structure as seen in program order.
  46. *
  47. * These two requirements will allow lockless iteration of the tree -- not
  48. * correct iteration mind you, tree rotations are not atomic so a lookup might
  49. * miss entire subtrees.
  50. *
  51. * But they do guarantee that any such traversal will only see valid elements
  52. * and that it will indeed complete -- does not get stuck in a loop.
  53. *
  54. * It also guarantees that if the lookup returns an element it is the 'correct'
  55. * one. But not returning an element does _NOT_ mean it's not present.
  56. *
  57. * NOTE:
  58. *
  59. * Stores to __rb_parent_color are not important for simple lookups so those
  60. * are left undone as of now. Nor did I check for loops involving parent
  61. * pointers.
  62. */
  63. static inline void rb_set_black(struct rb_node *rb)
  64. {
  65. rb->__rb_parent_color |= RB_BLACK;
  66. }
  67. static inline struct rb_node *rb_red_parent(struct rb_node *red)
  68. {
  69. return (struct rb_node *)red->__rb_parent_color;
  70. }
  71. /*
  72. * Helper function for rotations:
  73. * - old's parent and color get assigned to new
  74. * - old gets assigned new as a parent and 'color' as a color.
  75. */
  76. static inline void
  77. __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
  78. struct rb_root *root, int color)
  79. {
  80. struct rb_node *parent = rb_parent(old);
  81. new->__rb_parent_color = old->__rb_parent_color;
  82. rb_set_parent_color(old, new, color);
  83. __rb_change_child(old, new, parent, root);
  84. }
  85. static __always_inline void
  86. __rb_insert(struct rb_node *node, struct rb_root *root,
  87. bool newleft, struct rb_node **leftmost,
  88. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  89. {
  90. struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
  91. if (newleft)
  92. *leftmost = node;
  93. while (true) {
  94. /*
  95. * Loop invariant: node is red.
  96. */
  97. if (unlikely(!parent)) {
  98. /*
  99. * The inserted node is root. Either this is the
  100. * first node, or we recursed at Case 1 below and
  101. * are no longer violating 4).
  102. */
  103. rb_set_parent_color(node, NULL, RB_BLACK);
  104. break;
  105. }
  106. /*
  107. * If there is a black parent, we are done.
  108. * Otherwise, take some corrective action as,
  109. * per 4), we don't want a red root or two
  110. * consecutive red nodes.
  111. */
  112. if(rb_is_black(parent))
  113. break;
  114. gparent = rb_red_parent(parent);
  115. tmp = gparent->rb_right;
  116. if (parent != tmp) { /* parent == gparent->rb_left */
  117. if (tmp && rb_is_red(tmp)) {
  118. /*
  119. * Case 1 - node's uncle is red (color flips).
  120. *
  121. * G g
  122. * / \ / \
  123. * p u --> P U
  124. * / /
  125. * n n
  126. *
  127. * However, since g's parent might be red, and
  128. * 4) does not allow this, we need to recurse
  129. * at g.
  130. */
  131. rb_set_parent_color(tmp, gparent, RB_BLACK);
  132. rb_set_parent_color(parent, gparent, RB_BLACK);
  133. node = gparent;
  134. parent = rb_parent(node);
  135. rb_set_parent_color(node, parent, RB_RED);
  136. continue;
  137. }
  138. tmp = parent->rb_right;
  139. if (node == tmp) {
  140. /*
  141. * Case 2 - node's uncle is black and node is
  142. * the parent's right child (left rotate at parent).
  143. *
  144. * G G
  145. * / \ / \
  146. * p U --> n U
  147. * \ /
  148. * n p
  149. *
  150. * This still leaves us in violation of 4), the
  151. * continuation into Case 3 will fix that.
  152. */
  153. tmp = node->rb_left;
  154. WRITE_ONCE(parent->rb_right, tmp);
  155. WRITE_ONCE(node->rb_left, parent);
  156. if (tmp)
  157. rb_set_parent_color(tmp, parent,
  158. RB_BLACK);
  159. rb_set_parent_color(parent, node, RB_RED);
  160. augment_rotate(parent, node);
  161. parent = node;
  162. tmp = node->rb_right;
  163. }
  164. /*
  165. * Case 3 - node's uncle is black and node is
  166. * the parent's left child (right rotate at gparent).
  167. *
  168. * G P
  169. * / \ / \
  170. * p U --> n g
  171. * / \
  172. * n U
  173. */
  174. WRITE_ONCE(gparent->rb_left, tmp); /* == parent->rb_right */
  175. WRITE_ONCE(parent->rb_right, gparent);
  176. if (tmp)
  177. rb_set_parent_color(tmp, gparent, RB_BLACK);
  178. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  179. augment_rotate(gparent, parent);
  180. break;
  181. } else {
  182. tmp = gparent->rb_left;
  183. if (tmp && rb_is_red(tmp)) {
  184. /* Case 1 - color flips */
  185. rb_set_parent_color(tmp, gparent, RB_BLACK);
  186. rb_set_parent_color(parent, gparent, RB_BLACK);
  187. node = gparent;
  188. parent = rb_parent(node);
  189. rb_set_parent_color(node, parent, RB_RED);
  190. continue;
  191. }
  192. tmp = parent->rb_left;
  193. if (node == tmp) {
  194. /* Case 2 - right rotate at parent */
  195. tmp = node->rb_right;
  196. WRITE_ONCE(parent->rb_left, tmp);
  197. WRITE_ONCE(node->rb_right, parent);
  198. if (tmp)
  199. rb_set_parent_color(tmp, parent,
  200. RB_BLACK);
  201. rb_set_parent_color(parent, node, RB_RED);
  202. augment_rotate(parent, node);
  203. parent = node;
  204. tmp = node->rb_left;
  205. }
  206. /* Case 3 - left rotate at gparent */
  207. WRITE_ONCE(gparent->rb_right, tmp); /* == parent->rb_left */
  208. WRITE_ONCE(parent->rb_left, gparent);
  209. if (tmp)
  210. rb_set_parent_color(tmp, gparent, RB_BLACK);
  211. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  212. augment_rotate(gparent, parent);
  213. break;
  214. }
  215. }
  216. }
  217. /*
  218. * Inline version for rb_erase() use - we want to be able to inline
  219. * and eliminate the dummy_rotate callback there
  220. */
  221. static __always_inline void
  222. ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
  223. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  224. {
  225. struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
  226. while (true) {
  227. /*
  228. * Loop invariants:
  229. * - node is black (or NULL on first iteration)
  230. * - node is not the root (parent is not NULL)
  231. * - All leaf paths going through parent and node have a
  232. * black node count that is 1 lower than other leaf paths.
  233. */
  234. sibling = parent->rb_right;
  235. if (node != sibling) { /* node == parent->rb_left */
  236. if (rb_is_red(sibling)) {
  237. /*
  238. * Case 1 - left rotate at parent
  239. *
  240. * P S
  241. * / \ / \
  242. * N s --> p Sr
  243. * / \ / \
  244. * Sl Sr N Sl
  245. */
  246. tmp1 = sibling->rb_left;
  247. WRITE_ONCE(parent->rb_right, tmp1);
  248. WRITE_ONCE(sibling->rb_left, parent);
  249. rb_set_parent_color(tmp1, parent, RB_BLACK);
  250. __rb_rotate_set_parents(parent, sibling, root,
  251. RB_RED);
  252. augment_rotate(parent, sibling);
  253. sibling = tmp1;
  254. }
  255. tmp1 = sibling->rb_right;
  256. if (!tmp1 || rb_is_black(tmp1)) {
  257. tmp2 = sibling->rb_left;
  258. if (!tmp2 || rb_is_black(tmp2)) {
  259. /*
  260. * Case 2 - sibling color flip
  261. * (p could be either color here)
  262. *
  263. * (p) (p)
  264. * / \ / \
  265. * N S --> N s
  266. * / \ / \
  267. * Sl Sr Sl Sr
  268. *
  269. * This leaves us violating 5) which
  270. * can be fixed by flipping p to black
  271. * if it was red, or by recursing at p.
  272. * p is red when coming from Case 1.
  273. */
  274. rb_set_parent_color(sibling, parent,
  275. RB_RED);
  276. if (rb_is_red(parent))
  277. rb_set_black(parent);
  278. else {
  279. node = parent;
  280. parent = rb_parent(node);
  281. if (parent)
  282. continue;
  283. }
  284. break;
  285. }
  286. /*
  287. * Case 3 - right rotate at sibling
  288. * (p could be either color here)
  289. *
  290. * (p) (p)
  291. * / \ / \
  292. * N S --> N sl
  293. * / \ \
  294. * sl Sr S
  295. * \
  296. * Sr
  297. *
  298. * Note: p might be red, and then both
  299. * p and sl are red after rotation(which
  300. * breaks property 4). This is fixed in
  301. * Case 4 (in __rb_rotate_set_parents()
  302. * which set sl the color of p
  303. * and set p RB_BLACK)
  304. *
  305. * (p) (sl)
  306. * / \ / \
  307. * N sl --> P S
  308. * \ / \
  309. * S N Sr
  310. * \
  311. * Sr
  312. */
  313. tmp1 = tmp2->rb_right;
  314. WRITE_ONCE(sibling->rb_left, tmp1);
  315. WRITE_ONCE(tmp2->rb_right, sibling);
  316. WRITE_ONCE(parent->rb_right, tmp2);
  317. if (tmp1)
  318. rb_set_parent_color(tmp1, sibling,
  319. RB_BLACK);
  320. augment_rotate(sibling, tmp2);
  321. tmp1 = sibling;
  322. sibling = tmp2;
  323. }
  324. /*
  325. * Case 4 - left rotate at parent + color flips
  326. * (p and sl could be either color here.
  327. * After rotation, p becomes black, s acquires
  328. * p's color, and sl keeps its color)
  329. *
  330. * (p) (s)
  331. * / \ / \
  332. * N S --> P Sr
  333. * / \ / \
  334. * (sl) sr N (sl)
  335. */
  336. tmp2 = sibling->rb_left;
  337. WRITE_ONCE(parent->rb_right, tmp2);
  338. WRITE_ONCE(sibling->rb_left, parent);
  339. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  340. if (tmp2)
  341. rb_set_parent(tmp2, parent);
  342. __rb_rotate_set_parents(parent, sibling, root,
  343. RB_BLACK);
  344. augment_rotate(parent, sibling);
  345. break;
  346. } else {
  347. sibling = parent->rb_left;
  348. if (rb_is_red(sibling)) {
  349. /* Case 1 - right rotate at parent */
  350. tmp1 = sibling->rb_right;
  351. WRITE_ONCE(parent->rb_left, tmp1);
  352. WRITE_ONCE(sibling->rb_right, parent);
  353. rb_set_parent_color(tmp1, parent, RB_BLACK);
  354. __rb_rotate_set_parents(parent, sibling, root,
  355. RB_RED);
  356. augment_rotate(parent, sibling);
  357. sibling = tmp1;
  358. }
  359. tmp1 = sibling->rb_left;
  360. if (!tmp1 || rb_is_black(tmp1)) {
  361. tmp2 = sibling->rb_right;
  362. if (!tmp2 || rb_is_black(tmp2)) {
  363. /* Case 2 - sibling color flip */
  364. rb_set_parent_color(sibling, parent,
  365. RB_RED);
  366. if (rb_is_red(parent))
  367. rb_set_black(parent);
  368. else {
  369. node = parent;
  370. parent = rb_parent(node);
  371. if (parent)
  372. continue;
  373. }
  374. break;
  375. }
  376. /* Case 3 - left rotate at sibling */
  377. tmp1 = tmp2->rb_left;
  378. WRITE_ONCE(sibling->rb_right, tmp1);
  379. WRITE_ONCE(tmp2->rb_left, sibling);
  380. WRITE_ONCE(parent->rb_left, tmp2);
  381. if (tmp1)
  382. rb_set_parent_color(tmp1, sibling,
  383. RB_BLACK);
  384. augment_rotate(sibling, tmp2);
  385. tmp1 = sibling;
  386. sibling = tmp2;
  387. }
  388. /* Case 4 - right rotate at parent + color flips */
  389. tmp2 = sibling->rb_right;
  390. WRITE_ONCE(parent->rb_left, tmp2);
  391. WRITE_ONCE(sibling->rb_right, parent);
  392. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  393. if (tmp2)
  394. rb_set_parent(tmp2, parent);
  395. __rb_rotate_set_parents(parent, sibling, root,
  396. RB_BLACK);
  397. augment_rotate(parent, sibling);
  398. break;
  399. }
  400. }
  401. }
  402. /* Non-inline version for rb_erase_augmented() use */
  403. void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
  404. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  405. {
  406. ____rb_erase_color(parent, root, augment_rotate);
  407. }
  408. EXPORT_SYMBOL(__rb_erase_color);
  409. /*
  410. * Non-augmented rbtree manipulation functions.
  411. *
  412. * We use dummy augmented callbacks here, and have the compiler optimize them
  413. * out of the rb_insert_color() and rb_erase() function definitions.
  414. */
  415. static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
  416. static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
  417. static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
  418. static const struct rb_augment_callbacks dummy_callbacks = {
  419. .propagate = dummy_propagate,
  420. .copy = dummy_copy,
  421. .rotate = dummy_rotate
  422. };
  423. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  424. {
  425. __rb_insert(node, root, false, NULL, dummy_rotate);
  426. }
  427. EXPORT_SYMBOL(rb_insert_color);
  428. void rb_erase(struct rb_node *node, struct rb_root *root)
  429. {
  430. struct rb_node *rebalance;
  431. rebalance = __rb_erase_augmented(node, root,
  432. NULL, &dummy_callbacks);
  433. if (rebalance)
  434. ____rb_erase_color(rebalance, root, dummy_rotate);
  435. }
  436. EXPORT_SYMBOL(rb_erase);
  437. void rb_insert_color_cached(struct rb_node *node,
  438. struct rb_root_cached *root, bool leftmost)
  439. {
  440. __rb_insert(node, &root->rb_root, leftmost,
  441. &root->rb_leftmost, dummy_rotate);
  442. }
  443. EXPORT_SYMBOL(rb_insert_color_cached);
  444. void rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
  445. {
  446. struct rb_node *rebalance;
  447. rebalance = __rb_erase_augmented(node, &root->rb_root,
  448. &root->rb_leftmost, &dummy_callbacks);
  449. if (rebalance)
  450. ____rb_erase_color(rebalance, &root->rb_root, dummy_rotate);
  451. }
  452. EXPORT_SYMBOL(rb_erase_cached);
  453. /*
  454. * Augmented rbtree manipulation functions.
  455. *
  456. * This instantiates the same __always_inline functions as in the non-augmented
  457. * case, but this time with user-defined callbacks.
  458. */
  459. void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  460. bool newleft, struct rb_node **leftmost,
  461. void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
  462. {
  463. __rb_insert(node, root, newleft, leftmost, augment_rotate);
  464. }
  465. EXPORT_SYMBOL(__rb_insert_augmented);
  466. /*
  467. * This function returns the first node (in sort order) of the tree.
  468. */
  469. struct rb_node *rb_first(const struct rb_root *root)
  470. {
  471. struct rb_node *n;
  472. n = root->rb_node;
  473. if (!n)
  474. return NULL;
  475. while (n->rb_left)
  476. n = n->rb_left;
  477. return n;
  478. }
  479. EXPORT_SYMBOL(rb_first);
  480. struct rb_node *rb_last(const struct rb_root *root)
  481. {
  482. struct rb_node *n;
  483. n = root->rb_node;
  484. if (!n)
  485. return NULL;
  486. while (n->rb_right)
  487. n = n->rb_right;
  488. return n;
  489. }
  490. EXPORT_SYMBOL(rb_last);
  491. struct rb_node *rb_next(const struct rb_node *node)
  492. {
  493. struct rb_node *parent;
  494. if (RB_EMPTY_NODE(node))
  495. return NULL;
  496. /*
  497. * If we have a right-hand child, go down and then left as far
  498. * as we can.
  499. */
  500. if (node->rb_right) {
  501. node = node->rb_right;
  502. while (node->rb_left)
  503. node=node->rb_left;
  504. return (struct rb_node *)node;
  505. }
  506. /*
  507. * No right-hand children. Everything down and left is smaller than us,
  508. * so any 'next' node must be in the general direction of our parent.
  509. * Go up the tree; any time the ancestor is a right-hand child of its
  510. * parent, keep going up. First time it's a left-hand child of its
  511. * parent, said parent is our 'next' node.
  512. */
  513. while ((parent = rb_parent(node)) && node == parent->rb_right)
  514. node = parent;
  515. return parent;
  516. }
  517. EXPORT_SYMBOL(rb_next);
  518. struct rb_node *rb_prev(const struct rb_node *node)
  519. {
  520. struct rb_node *parent;
  521. if (RB_EMPTY_NODE(node))
  522. return NULL;
  523. /*
  524. * If we have a left-hand child, go down and then right as far
  525. * as we can.
  526. */
  527. if (node->rb_left) {
  528. node = node->rb_left;
  529. while (node->rb_right)
  530. node=node->rb_right;
  531. return (struct rb_node *)node;
  532. }
  533. /*
  534. * No left-hand children. Go up till we find an ancestor which
  535. * is a right-hand child of its parent.
  536. */
  537. while ((parent = rb_parent(node)) && node == parent->rb_left)
  538. node = parent;
  539. return parent;
  540. }
  541. EXPORT_SYMBOL(rb_prev);
  542. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  543. struct rb_root *root)
  544. {
  545. struct rb_node *parent = rb_parent(victim);
  546. /* Copy the pointers/colour from the victim to the replacement */
  547. *new = *victim;
  548. /* Set the surrounding nodes to point to the replacement */
  549. if (victim->rb_left)
  550. rb_set_parent(victim->rb_left, new);
  551. if (victim->rb_right)
  552. rb_set_parent(victim->rb_right, new);
  553. __rb_change_child(victim, new, parent, root);
  554. }
  555. EXPORT_SYMBOL(rb_replace_node);
  556. void rb_replace_node_cached(struct rb_node *victim, struct rb_node *new,
  557. struct rb_root_cached *root)
  558. {
  559. rb_replace_node(victim, new, &root->rb_root);
  560. if (root->rb_leftmost == victim)
  561. root->rb_leftmost = new;
  562. }
  563. EXPORT_SYMBOL(rb_replace_node_cached);
  564. void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
  565. struct rb_root *root)
  566. {
  567. struct rb_node *parent = rb_parent(victim);
  568. /* Copy the pointers/colour from the victim to the replacement */
  569. *new = *victim;
  570. /* Set the surrounding nodes to point to the replacement */
  571. if (victim->rb_left)
  572. rb_set_parent(victim->rb_left, new);
  573. if (victim->rb_right)
  574. rb_set_parent(victim->rb_right, new);
  575. /* Set the parent's pointer to the new node last after an RCU barrier
  576. * so that the pointers onwards are seen to be set correctly when doing
  577. * an RCU walk over the tree.
  578. */
  579. __rb_change_child_rcu(victim, new, parent, root);
  580. }
  581. EXPORT_SYMBOL(rb_replace_node_rcu);
  582. static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
  583. {
  584. for (;;) {
  585. if (node->rb_left)
  586. node = node->rb_left;
  587. else if (node->rb_right)
  588. node = node->rb_right;
  589. else
  590. return (struct rb_node *)node;
  591. }
  592. }
  593. struct rb_node *rb_next_postorder(const struct rb_node *node)
  594. {
  595. const struct rb_node *parent;
  596. if (!node)
  597. return NULL;
  598. parent = rb_parent(node);
  599. /* If we're sitting on node, we've already seen our children */
  600. if (parent && node == parent->rb_left && parent->rb_right) {
  601. /* If we are the parent's left node, go to the parent's right
  602. * node then all the way down to the left */
  603. return rb_left_deepest_node(parent->rb_right);
  604. } else
  605. /* Otherwise we are the parent's right node, and the parent
  606. * should be next */
  607. return (struct rb_node *)parent;
  608. }
  609. EXPORT_SYMBOL(rb_next_postorder);
  610. struct rb_node *rb_first_postorder(const struct rb_root *root)
  611. {
  612. if (!root->rb_node)
  613. return NULL;
  614. return rb_left_deepest_node(root->rb_node);
  615. }
  616. EXPORT_SYMBOL(rb_first_postorder);