rbtree.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. Red Black Trees
  3. (C) 1999 Andrea Arcangeli <andrea@suse.de>
  4. (C) 2002 David Woodhouse <dwmw2@infradead.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. linux/lib/rbtree.c
  17. */
  18. #include <linux/rbtree.h>
  19. #include <linux/export.h>
  20. /*
  21. * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
  22. *
  23. * 1) A node is either red or black
  24. * 2) The root is black
  25. * 3) All leaves (NULL) are black
  26. * 4) Both children of every red node are black
  27. * 5) Every simple path from root to leaves contains the same number
  28. * of black nodes.
  29. *
  30. * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
  31. * consecutive red nodes in a path and every red node is therefore followed by
  32. * a black. So if B is the number of black nodes on every simple path (as per
  33. * 5), then the longest possible path due to 4 is 2B.
  34. *
  35. * We shall indicate color with case, where black nodes are uppercase and red
  36. * nodes will be lowercase. Unknown color nodes shall be drawn as red within
  37. * parentheses and have some accompanying text comment.
  38. */
  39. #define RB_RED 0
  40. #define RB_BLACK 1
  41. #define rb_color(r) ((r)->__rb_parent_color & 1)
  42. #define rb_is_red(r) (!rb_color(r))
  43. #define rb_is_black(r) rb_color(r)
  44. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  45. {
  46. rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
  47. }
  48. static inline void rb_set_parent_color(struct rb_node *rb,
  49. struct rb_node *p, int color)
  50. {
  51. rb->__rb_parent_color = (unsigned long)p | color;
  52. }
  53. static inline struct rb_node *rb_red_parent(struct rb_node *red)
  54. {
  55. return (struct rb_node *)red->__rb_parent_color;
  56. }
  57. /*
  58. * Helper function for rotations:
  59. * - old's parent and color get assigned to new
  60. * - old gets assigned new as a parent and 'color' as a color.
  61. */
  62. static inline void
  63. __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
  64. struct rb_root *root, int color)
  65. {
  66. struct rb_node *parent = rb_parent(old);
  67. new->__rb_parent_color = old->__rb_parent_color;
  68. rb_set_parent_color(old, new, color);
  69. if (parent) {
  70. if (parent->rb_left == old)
  71. parent->rb_left = new;
  72. else
  73. parent->rb_right = new;
  74. } else
  75. root->rb_node = new;
  76. }
  77. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  78. {
  79. struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
  80. while (true) {
  81. /*
  82. * Loop invariant: node is red
  83. *
  84. * If there is a black parent, we are done.
  85. * Otherwise, take some corrective action as we don't
  86. * want a red root or two consecutive red nodes.
  87. */
  88. if (!parent) {
  89. rb_set_parent_color(node, NULL, RB_BLACK);
  90. break;
  91. } else if (rb_is_black(parent))
  92. break;
  93. gparent = rb_red_parent(parent);
  94. if (parent == gparent->rb_left) {
  95. tmp = gparent->rb_right;
  96. if (tmp && rb_is_red(tmp)) {
  97. /*
  98. * Case 1 - color flips
  99. *
  100. * G g
  101. * / \ / \
  102. * p u --> P U
  103. * / /
  104. * n N
  105. *
  106. * However, since g's parent might be red, and
  107. * 4) does not allow this, we need to recurse
  108. * at g.
  109. */
  110. rb_set_parent_color(tmp, gparent, RB_BLACK);
  111. rb_set_parent_color(parent, gparent, RB_BLACK);
  112. node = gparent;
  113. parent = rb_parent(node);
  114. rb_set_parent_color(node, parent, RB_RED);
  115. continue;
  116. }
  117. if (parent->rb_right == node) {
  118. /*
  119. * Case 2 - left rotate at parent
  120. *
  121. * G G
  122. * / \ / \
  123. * p U --> n U
  124. * \ /
  125. * n p
  126. *
  127. * This still leaves us in violation of 4), the
  128. * continuation into Case 3 will fix that.
  129. */
  130. parent->rb_right = tmp = node->rb_left;
  131. node->rb_left = parent;
  132. if (tmp)
  133. rb_set_parent_color(tmp, parent,
  134. RB_BLACK);
  135. rb_set_parent_color(parent, node, RB_RED);
  136. parent = node;
  137. }
  138. /*
  139. * Case 3 - right rotate at gparent
  140. *
  141. * G P
  142. * / \ / \
  143. * p U --> n g
  144. * / \
  145. * n U
  146. */
  147. gparent->rb_left = tmp = parent->rb_right;
  148. parent->rb_right = gparent;
  149. if (tmp)
  150. rb_set_parent_color(tmp, gparent, RB_BLACK);
  151. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  152. break;
  153. } else {
  154. tmp = gparent->rb_left;
  155. if (tmp && rb_is_red(tmp)) {
  156. /* Case 1 - color flips */
  157. rb_set_parent_color(tmp, gparent, RB_BLACK);
  158. rb_set_parent_color(parent, gparent, RB_BLACK);
  159. node = gparent;
  160. parent = rb_parent(node);
  161. rb_set_parent_color(node, parent, RB_RED);
  162. continue;
  163. }
  164. if (parent->rb_left == node) {
  165. /* Case 2 - right rotate at parent */
  166. parent->rb_left = tmp = node->rb_right;
  167. node->rb_right = parent;
  168. if (tmp)
  169. rb_set_parent_color(tmp, parent,
  170. RB_BLACK);
  171. rb_set_parent_color(parent, node, RB_RED);
  172. parent = node;
  173. }
  174. /* Case 3 - left rotate at gparent */
  175. gparent->rb_right = tmp = parent->rb_left;
  176. parent->rb_left = gparent;
  177. if (tmp)
  178. rb_set_parent_color(tmp, gparent, RB_BLACK);
  179. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  180. break;
  181. }
  182. }
  183. }
  184. EXPORT_SYMBOL(rb_insert_color);
  185. static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
  186. struct rb_root *root)
  187. {
  188. struct rb_node *sibling, *tmp1, *tmp2;
  189. while (true) {
  190. /*
  191. * Loop invariant: all leaf paths going through node have a
  192. * black node count that is 1 lower than other leaf paths.
  193. *
  194. * If node is red, we can flip it to black to adjust.
  195. * If node is the root, all leaf paths go through it.
  196. * Otherwise, we need to adjust the tree through color flips
  197. * and tree rotations as per one of the 4 cases below.
  198. */
  199. if (node && rb_is_red(node)) {
  200. rb_set_parent_color(node, parent, RB_BLACK);
  201. break;
  202. } else if (!parent) {
  203. break;
  204. } else if (parent->rb_left == node) {
  205. sibling = parent->rb_right;
  206. if (rb_is_red(sibling)) {
  207. /*
  208. * Case 1 - left rotate at parent
  209. *
  210. * P S
  211. * / \ / \
  212. * N s --> p Sr
  213. * / \ / \
  214. * Sl Sr N Sl
  215. */
  216. parent->rb_right = tmp1 = sibling->rb_left;
  217. sibling->rb_left = parent;
  218. rb_set_parent_color(tmp1, parent, RB_BLACK);
  219. __rb_rotate_set_parents(parent, sibling, root,
  220. RB_RED);
  221. sibling = tmp1;
  222. }
  223. tmp1 = sibling->rb_right;
  224. if (!tmp1 || rb_is_black(tmp1)) {
  225. tmp2 = sibling->rb_left;
  226. if (!tmp2 || rb_is_black(tmp2)) {
  227. /*
  228. * Case 2 - sibling color flip
  229. * (p could be either color here)
  230. *
  231. * (p) (p)
  232. * / \ / \
  233. * N S --> N s
  234. * / \ / \
  235. * Sl Sr Sl Sr
  236. *
  237. * This leaves us violating 5), so
  238. * recurse at p. If p is red, the
  239. * recursion will just flip it to black
  240. * and exit. If coming from Case 1,
  241. * p is known to be red.
  242. */
  243. rb_set_parent_color(sibling, parent,
  244. RB_RED);
  245. node = parent;
  246. parent = rb_parent(node);
  247. continue;
  248. }
  249. /*
  250. * Case 3 - right rotate at sibling
  251. * (p could be either color here)
  252. *
  253. * (p) (p)
  254. * / \ / \
  255. * N S --> N Sl
  256. * / \ \
  257. * sl Sr s
  258. * \
  259. * Sr
  260. */
  261. sibling->rb_left = tmp1 = tmp2->rb_right;
  262. tmp2->rb_right = sibling;
  263. parent->rb_right = tmp2;
  264. if (tmp1)
  265. rb_set_parent_color(tmp1, sibling,
  266. RB_BLACK);
  267. tmp1 = sibling;
  268. sibling = tmp2;
  269. }
  270. /*
  271. * Case 4 - left rotate at parent + color flips
  272. * (p and sl could be either color here.
  273. * After rotation, p becomes black, s acquires
  274. * p's color, and sl keeps its color)
  275. *
  276. * (p) (s)
  277. * / \ / \
  278. * N S --> P Sr
  279. * / \ / \
  280. * (sl) sr N (sl)
  281. */
  282. parent->rb_right = tmp2 = sibling->rb_left;
  283. sibling->rb_left = parent;
  284. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  285. if (tmp2)
  286. rb_set_parent(tmp2, parent);
  287. __rb_rotate_set_parents(parent, sibling, root,
  288. RB_BLACK);
  289. break;
  290. } else {
  291. sibling = parent->rb_left;
  292. if (rb_is_red(sibling)) {
  293. /* Case 1 - right rotate at parent */
  294. parent->rb_left = tmp1 = sibling->rb_right;
  295. sibling->rb_right = parent;
  296. rb_set_parent_color(tmp1, parent, RB_BLACK);
  297. __rb_rotate_set_parents(parent, sibling, root,
  298. RB_RED);
  299. sibling = tmp1;
  300. }
  301. tmp1 = sibling->rb_left;
  302. if (!tmp1 || rb_is_black(tmp1)) {
  303. tmp2 = sibling->rb_right;
  304. if (!tmp2 || rb_is_black(tmp2)) {
  305. /* Case 2 - sibling color flip */
  306. rb_set_parent_color(sibling, parent,
  307. RB_RED);
  308. node = parent;
  309. parent = rb_parent(node);
  310. continue;
  311. }
  312. /* Case 3 - right rotate at sibling */
  313. sibling->rb_right = tmp1 = tmp2->rb_left;
  314. tmp2->rb_left = sibling;
  315. parent->rb_left = tmp2;
  316. if (tmp1)
  317. rb_set_parent_color(tmp1, sibling,
  318. RB_BLACK);
  319. tmp1 = sibling;
  320. sibling = tmp2;
  321. }
  322. /* Case 4 - left rotate at parent + color flips */
  323. parent->rb_left = tmp2 = sibling->rb_right;
  324. sibling->rb_right = parent;
  325. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  326. if (tmp2)
  327. rb_set_parent(tmp2, parent);
  328. __rb_rotate_set_parents(parent, sibling, root,
  329. RB_BLACK);
  330. break;
  331. }
  332. }
  333. }
  334. void rb_erase(struct rb_node *node, struct rb_root *root)
  335. {
  336. struct rb_node *child, *parent;
  337. int color;
  338. if (!node->rb_left)
  339. child = node->rb_right;
  340. else if (!node->rb_right)
  341. child = node->rb_left;
  342. else {
  343. struct rb_node *old = node, *left;
  344. node = node->rb_right;
  345. while ((left = node->rb_left) != NULL)
  346. node = left;
  347. if (rb_parent(old)) {
  348. if (rb_parent(old)->rb_left == old)
  349. rb_parent(old)->rb_left = node;
  350. else
  351. rb_parent(old)->rb_right = node;
  352. } else
  353. root->rb_node = node;
  354. child = node->rb_right;
  355. parent = rb_parent(node);
  356. color = rb_color(node);
  357. if (parent == old) {
  358. parent = node;
  359. } else {
  360. if (child)
  361. rb_set_parent(child, parent);
  362. parent->rb_left = child;
  363. node->rb_right = old->rb_right;
  364. rb_set_parent(old->rb_right, node);
  365. }
  366. node->__rb_parent_color = old->__rb_parent_color;
  367. node->rb_left = old->rb_left;
  368. rb_set_parent(old->rb_left, node);
  369. goto color;
  370. }
  371. parent = rb_parent(node);
  372. color = rb_color(node);
  373. if (child)
  374. rb_set_parent(child, parent);
  375. if (parent) {
  376. if (parent->rb_left == node)
  377. parent->rb_left = child;
  378. else
  379. parent->rb_right = child;
  380. } else
  381. root->rb_node = child;
  382. color:
  383. if (color == RB_BLACK)
  384. __rb_erase_color(child, parent, root);
  385. }
  386. EXPORT_SYMBOL(rb_erase);
  387. static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
  388. {
  389. struct rb_node *parent;
  390. up:
  391. func(node, data);
  392. parent = rb_parent(node);
  393. if (!parent)
  394. return;
  395. if (node == parent->rb_left && parent->rb_right)
  396. func(parent->rb_right, data);
  397. else if (parent->rb_left)
  398. func(parent->rb_left, data);
  399. node = parent;
  400. goto up;
  401. }
  402. /*
  403. * after inserting @node into the tree, update the tree to account for
  404. * both the new entry and any damage done by rebalance
  405. */
  406. void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
  407. {
  408. if (node->rb_left)
  409. node = node->rb_left;
  410. else if (node->rb_right)
  411. node = node->rb_right;
  412. rb_augment_path(node, func, data);
  413. }
  414. EXPORT_SYMBOL(rb_augment_insert);
  415. /*
  416. * before removing the node, find the deepest node on the rebalance path
  417. * that will still be there after @node gets removed
  418. */
  419. struct rb_node *rb_augment_erase_begin(struct rb_node *node)
  420. {
  421. struct rb_node *deepest;
  422. if (!node->rb_right && !node->rb_left)
  423. deepest = rb_parent(node);
  424. else if (!node->rb_right)
  425. deepest = node->rb_left;
  426. else if (!node->rb_left)
  427. deepest = node->rb_right;
  428. else {
  429. deepest = rb_next(node);
  430. if (deepest->rb_right)
  431. deepest = deepest->rb_right;
  432. else if (rb_parent(deepest) != node)
  433. deepest = rb_parent(deepest);
  434. }
  435. return deepest;
  436. }
  437. EXPORT_SYMBOL(rb_augment_erase_begin);
  438. /*
  439. * after removal, update the tree to account for the removed entry
  440. * and any rebalance damage.
  441. */
  442. void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
  443. {
  444. if (node)
  445. rb_augment_path(node, func, data);
  446. }
  447. EXPORT_SYMBOL(rb_augment_erase_end);
  448. /*
  449. * This function returns the first node (in sort order) of the tree.
  450. */
  451. struct rb_node *rb_first(const struct rb_root *root)
  452. {
  453. struct rb_node *n;
  454. n = root->rb_node;
  455. if (!n)
  456. return NULL;
  457. while (n->rb_left)
  458. n = n->rb_left;
  459. return n;
  460. }
  461. EXPORT_SYMBOL(rb_first);
  462. struct rb_node *rb_last(const struct rb_root *root)
  463. {
  464. struct rb_node *n;
  465. n = root->rb_node;
  466. if (!n)
  467. return NULL;
  468. while (n->rb_right)
  469. n = n->rb_right;
  470. return n;
  471. }
  472. EXPORT_SYMBOL(rb_last);
  473. struct rb_node *rb_next(const struct rb_node *node)
  474. {
  475. struct rb_node *parent;
  476. if (RB_EMPTY_NODE(node))
  477. return NULL;
  478. /*
  479. * If we have a right-hand child, go down and then left as far
  480. * as we can.
  481. */
  482. if (node->rb_right) {
  483. node = node->rb_right;
  484. while (node->rb_left)
  485. node=node->rb_left;
  486. return (struct rb_node *)node;
  487. }
  488. /*
  489. * No right-hand children. Everything down and left is smaller than us,
  490. * so any 'next' node must be in the general direction of our parent.
  491. * Go up the tree; any time the ancestor is a right-hand child of its
  492. * parent, keep going up. First time it's a left-hand child of its
  493. * parent, said parent is our 'next' node.
  494. */
  495. while ((parent = rb_parent(node)) && node == parent->rb_right)
  496. node = parent;
  497. return parent;
  498. }
  499. EXPORT_SYMBOL(rb_next);
  500. struct rb_node *rb_prev(const struct rb_node *node)
  501. {
  502. struct rb_node *parent;
  503. if (RB_EMPTY_NODE(node))
  504. return NULL;
  505. /*
  506. * If we have a left-hand child, go down and then right as far
  507. * as we can.
  508. */
  509. if (node->rb_left) {
  510. node = node->rb_left;
  511. while (node->rb_right)
  512. node=node->rb_right;
  513. return (struct rb_node *)node;
  514. }
  515. /*
  516. * No left-hand children. Go up till we find an ancestor which
  517. * is a right-hand child of its parent.
  518. */
  519. while ((parent = rb_parent(node)) && node == parent->rb_left)
  520. node = parent;
  521. return parent;
  522. }
  523. EXPORT_SYMBOL(rb_prev);
  524. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  525. struct rb_root *root)
  526. {
  527. struct rb_node *parent = rb_parent(victim);
  528. /* Set the surrounding nodes to point to the replacement */
  529. if (parent) {
  530. if (victim == parent->rb_left)
  531. parent->rb_left = new;
  532. else
  533. parent->rb_right = new;
  534. } else {
  535. root->rb_node = new;
  536. }
  537. if (victim->rb_left)
  538. rb_set_parent(victim->rb_left, new);
  539. if (victim->rb_right)
  540. rb_set_parent(victim->rb_right, new);
  541. /* Copy the pointers/colour from the victim to the replacement */
  542. *new = *victim;
  543. }
  544. EXPORT_SYMBOL(rb_replace_node);