rbtree.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. {
  344. struct rb_node *old = node, *left;
  345. node = node->rb_right;
  346. while ((left = node->rb_left) != NULL)
  347. node = left;
  348. if (rb_parent(old)) {
  349. if (rb_parent(old)->rb_left == old)
  350. rb_parent(old)->rb_left = node;
  351. else
  352. rb_parent(old)->rb_right = node;
  353. } else
  354. root->rb_node = node;
  355. child = node->rb_right;
  356. parent = rb_parent(node);
  357. color = rb_color(node);
  358. if (parent == old) {
  359. parent = node;
  360. } else {
  361. if (child)
  362. rb_set_parent(child, parent);
  363. parent->rb_left = child;
  364. node->rb_right = old->rb_right;
  365. rb_set_parent(old->rb_right, node);
  366. }
  367. node->__rb_parent_color = old->__rb_parent_color;
  368. node->rb_left = old->rb_left;
  369. rb_set_parent(old->rb_left, node);
  370. goto color;
  371. }
  372. parent = rb_parent(node);
  373. color = rb_color(node);
  374. if (child)
  375. rb_set_parent(child, parent);
  376. if (parent)
  377. {
  378. if (parent->rb_left == node)
  379. parent->rb_left = child;
  380. else
  381. parent->rb_right = child;
  382. }
  383. else
  384. root->rb_node = child;
  385. color:
  386. if (color == RB_BLACK)
  387. __rb_erase_color(child, parent, root);
  388. }
  389. EXPORT_SYMBOL(rb_erase);
  390. static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
  391. {
  392. struct rb_node *parent;
  393. up:
  394. func(node, data);
  395. parent = rb_parent(node);
  396. if (!parent)
  397. return;
  398. if (node == parent->rb_left && parent->rb_right)
  399. func(parent->rb_right, data);
  400. else if (parent->rb_left)
  401. func(parent->rb_left, data);
  402. node = parent;
  403. goto up;
  404. }
  405. /*
  406. * after inserting @node into the tree, update the tree to account for
  407. * both the new entry and any damage done by rebalance
  408. */
  409. void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
  410. {
  411. if (node->rb_left)
  412. node = node->rb_left;
  413. else if (node->rb_right)
  414. node = node->rb_right;
  415. rb_augment_path(node, func, data);
  416. }
  417. EXPORT_SYMBOL(rb_augment_insert);
  418. /*
  419. * before removing the node, find the deepest node on the rebalance path
  420. * that will still be there after @node gets removed
  421. */
  422. struct rb_node *rb_augment_erase_begin(struct rb_node *node)
  423. {
  424. struct rb_node *deepest;
  425. if (!node->rb_right && !node->rb_left)
  426. deepest = rb_parent(node);
  427. else if (!node->rb_right)
  428. deepest = node->rb_left;
  429. else if (!node->rb_left)
  430. deepest = node->rb_right;
  431. else {
  432. deepest = rb_next(node);
  433. if (deepest->rb_right)
  434. deepest = deepest->rb_right;
  435. else if (rb_parent(deepest) != node)
  436. deepest = rb_parent(deepest);
  437. }
  438. return deepest;
  439. }
  440. EXPORT_SYMBOL(rb_augment_erase_begin);
  441. /*
  442. * after removal, update the tree to account for the removed entry
  443. * and any rebalance damage.
  444. */
  445. void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
  446. {
  447. if (node)
  448. rb_augment_path(node, func, data);
  449. }
  450. EXPORT_SYMBOL(rb_augment_erase_end);
  451. /*
  452. * This function returns the first node (in sort order) of the tree.
  453. */
  454. struct rb_node *rb_first(const struct rb_root *root)
  455. {
  456. struct rb_node *n;
  457. n = root->rb_node;
  458. if (!n)
  459. return NULL;
  460. while (n->rb_left)
  461. n = n->rb_left;
  462. return n;
  463. }
  464. EXPORT_SYMBOL(rb_first);
  465. struct rb_node *rb_last(const struct rb_root *root)
  466. {
  467. struct rb_node *n;
  468. n = root->rb_node;
  469. if (!n)
  470. return NULL;
  471. while (n->rb_right)
  472. n = n->rb_right;
  473. return n;
  474. }
  475. EXPORT_SYMBOL(rb_last);
  476. struct rb_node *rb_next(const struct rb_node *node)
  477. {
  478. struct rb_node *parent;
  479. if (RB_EMPTY_NODE(node))
  480. return NULL;
  481. /* If we have a right-hand child, go down and then left as far
  482. as we can. */
  483. if (node->rb_right) {
  484. node = node->rb_right;
  485. while (node->rb_left)
  486. node=node->rb_left;
  487. return (struct rb_node *)node;
  488. }
  489. /* No right-hand children. Everything down and left is
  490. smaller than us, so any 'next' node must be in the general
  491. direction of our parent. Go up the tree; any time the
  492. ancestor is a right-hand child of its parent, keep going
  493. up. First time it's a left-hand child of its parent, said
  494. parent is our 'next' node. */
  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. /* If we have a left-hand child, go down and then right as far
  506. as we can. */
  507. if (node->rb_left) {
  508. node = node->rb_left;
  509. while (node->rb_right)
  510. node=node->rb_right;
  511. return (struct rb_node *)node;
  512. }
  513. /* No left-hand children. Go up till we find an ancestor which
  514. is a right-hand child of its parent */
  515. while ((parent = rb_parent(node)) && node == parent->rb_left)
  516. node = parent;
  517. return parent;
  518. }
  519. EXPORT_SYMBOL(rb_prev);
  520. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  521. struct rb_root *root)
  522. {
  523. struct rb_node *parent = rb_parent(victim);
  524. /* Set the surrounding nodes to point to the replacement */
  525. if (parent) {
  526. if (victim == parent->rb_left)
  527. parent->rb_left = new;
  528. else
  529. parent->rb_right = new;
  530. } else {
  531. root->rb_node = new;
  532. }
  533. if (victim->rb_left)
  534. rb_set_parent(victim->rb_left, new);
  535. if (victim->rb_right)
  536. rb_set_parent(victim->rb_right, new);
  537. /* Copy the pointers/colour from the victim to the replacement */
  538. *new = *victim;
  539. }
  540. EXPORT_SYMBOL(rb_replace_node);