rbtree.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. static inline void
  58. __rb_change_child(struct rb_node *old, struct rb_node *new,
  59. struct rb_node *parent, struct rb_root *root)
  60. {
  61. if (parent) {
  62. if (parent->rb_left == old)
  63. parent->rb_left = new;
  64. else
  65. parent->rb_right = new;
  66. } else
  67. root->rb_node = new;
  68. }
  69. /*
  70. * Helper function for rotations:
  71. * - old's parent and color get assigned to new
  72. * - old gets assigned new as a parent and 'color' as a color.
  73. */
  74. static inline void
  75. __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
  76. struct rb_root *root, int color)
  77. {
  78. struct rb_node *parent = rb_parent(old);
  79. new->__rb_parent_color = old->__rb_parent_color;
  80. rb_set_parent_color(old, new, color);
  81. __rb_change_child(old, new, parent, root);
  82. }
  83. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  84. {
  85. struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
  86. while (true) {
  87. /*
  88. * Loop invariant: node is red
  89. *
  90. * If there is a black parent, we are done.
  91. * Otherwise, take some corrective action as we don't
  92. * want a red root or two consecutive red nodes.
  93. */
  94. if (!parent) {
  95. rb_set_parent_color(node, NULL, RB_BLACK);
  96. break;
  97. } else if (rb_is_black(parent))
  98. break;
  99. gparent = rb_red_parent(parent);
  100. tmp = gparent->rb_right;
  101. if (parent != tmp) { /* parent == gparent->rb_left */
  102. if (tmp && rb_is_red(tmp)) {
  103. /*
  104. * Case 1 - color flips
  105. *
  106. * G g
  107. * / \ / \
  108. * p u --> P U
  109. * / /
  110. * n N
  111. *
  112. * However, since g's parent might be red, and
  113. * 4) does not allow this, we need to recurse
  114. * at g.
  115. */
  116. rb_set_parent_color(tmp, gparent, RB_BLACK);
  117. rb_set_parent_color(parent, gparent, RB_BLACK);
  118. node = gparent;
  119. parent = rb_parent(node);
  120. rb_set_parent_color(node, parent, RB_RED);
  121. continue;
  122. }
  123. tmp = parent->rb_right;
  124. if (node == tmp) {
  125. /*
  126. * Case 2 - left rotate at parent
  127. *
  128. * G G
  129. * / \ / \
  130. * p U --> n U
  131. * \ /
  132. * n p
  133. *
  134. * This still leaves us in violation of 4), the
  135. * continuation into Case 3 will fix that.
  136. */
  137. parent->rb_right = tmp = node->rb_left;
  138. node->rb_left = parent;
  139. if (tmp)
  140. rb_set_parent_color(tmp, parent,
  141. RB_BLACK);
  142. rb_set_parent_color(parent, node, RB_RED);
  143. parent = node;
  144. tmp = node->rb_right;
  145. }
  146. /*
  147. * Case 3 - right rotate at gparent
  148. *
  149. * G P
  150. * / \ / \
  151. * p U --> n g
  152. * / \
  153. * n U
  154. */
  155. gparent->rb_left = tmp; /* == parent->rb_right */
  156. parent->rb_right = gparent;
  157. if (tmp)
  158. rb_set_parent_color(tmp, gparent, RB_BLACK);
  159. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  160. break;
  161. } else {
  162. tmp = gparent->rb_left;
  163. if (tmp && rb_is_red(tmp)) {
  164. /* Case 1 - color flips */
  165. rb_set_parent_color(tmp, gparent, RB_BLACK);
  166. rb_set_parent_color(parent, gparent, RB_BLACK);
  167. node = gparent;
  168. parent = rb_parent(node);
  169. rb_set_parent_color(node, parent, RB_RED);
  170. continue;
  171. }
  172. tmp = parent->rb_left;
  173. if (node == tmp) {
  174. /* Case 2 - right rotate at parent */
  175. parent->rb_left = tmp = node->rb_right;
  176. node->rb_right = parent;
  177. if (tmp)
  178. rb_set_parent_color(tmp, parent,
  179. RB_BLACK);
  180. rb_set_parent_color(parent, node, RB_RED);
  181. parent = node;
  182. tmp = node->rb_left;
  183. }
  184. /* Case 3 - left rotate at gparent */
  185. gparent->rb_right = tmp; /* == parent->rb_left */
  186. parent->rb_left = gparent;
  187. if (tmp)
  188. rb_set_parent_color(tmp, gparent, RB_BLACK);
  189. __rb_rotate_set_parents(gparent, parent, root, RB_RED);
  190. break;
  191. }
  192. }
  193. }
  194. EXPORT_SYMBOL(rb_insert_color);
  195. static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
  196. struct rb_root *root)
  197. {
  198. struct rb_node *sibling, *tmp1, *tmp2;
  199. while (true) {
  200. /*
  201. * Loop invariant: all leaf paths going through node have a
  202. * black node count that is 1 lower than other leaf paths.
  203. *
  204. * If node is red, we can flip it to black to adjust.
  205. * If node is the root, all leaf paths go through it.
  206. * Otherwise, we need to adjust the tree through color flips
  207. * and tree rotations as per one of the 4 cases below.
  208. */
  209. if (node && rb_is_red(node)) {
  210. rb_set_parent_color(node, parent, RB_BLACK);
  211. break;
  212. } else if (!parent) {
  213. break;
  214. }
  215. sibling = parent->rb_right;
  216. if (node != sibling) { /* node == parent->rb_left */
  217. if (rb_is_red(sibling)) {
  218. /*
  219. * Case 1 - left rotate at parent
  220. *
  221. * P S
  222. * / \ / \
  223. * N s --> p Sr
  224. * / \ / \
  225. * Sl Sr N Sl
  226. */
  227. parent->rb_right = tmp1 = sibling->rb_left;
  228. sibling->rb_left = parent;
  229. rb_set_parent_color(tmp1, parent, RB_BLACK);
  230. __rb_rotate_set_parents(parent, sibling, root,
  231. RB_RED);
  232. sibling = tmp1;
  233. }
  234. tmp1 = sibling->rb_right;
  235. if (!tmp1 || rb_is_black(tmp1)) {
  236. tmp2 = sibling->rb_left;
  237. if (!tmp2 || rb_is_black(tmp2)) {
  238. /*
  239. * Case 2 - sibling color flip
  240. * (p could be either color here)
  241. *
  242. * (p) (p)
  243. * / \ / \
  244. * N S --> N s
  245. * / \ / \
  246. * Sl Sr Sl Sr
  247. *
  248. * This leaves us violating 5), so
  249. * recurse at p. If p is red, the
  250. * recursion will just flip it to black
  251. * and exit. If coming from Case 1,
  252. * p is known to be red.
  253. */
  254. rb_set_parent_color(sibling, parent,
  255. RB_RED);
  256. node = parent;
  257. parent = rb_parent(node);
  258. continue;
  259. }
  260. /*
  261. * Case 3 - right rotate at sibling
  262. * (p could be either color here)
  263. *
  264. * (p) (p)
  265. * / \ / \
  266. * N S --> N Sl
  267. * / \ \
  268. * sl Sr s
  269. * \
  270. * Sr
  271. */
  272. sibling->rb_left = tmp1 = tmp2->rb_right;
  273. tmp2->rb_right = sibling;
  274. parent->rb_right = tmp2;
  275. if (tmp1)
  276. rb_set_parent_color(tmp1, sibling,
  277. RB_BLACK);
  278. tmp1 = sibling;
  279. sibling = tmp2;
  280. }
  281. /*
  282. * Case 4 - left rotate at parent + color flips
  283. * (p and sl could be either color here.
  284. * After rotation, p becomes black, s acquires
  285. * p's color, and sl keeps its color)
  286. *
  287. * (p) (s)
  288. * / \ / \
  289. * N S --> P Sr
  290. * / \ / \
  291. * (sl) sr N (sl)
  292. */
  293. parent->rb_right = tmp2 = sibling->rb_left;
  294. sibling->rb_left = parent;
  295. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  296. if (tmp2)
  297. rb_set_parent(tmp2, parent);
  298. __rb_rotate_set_parents(parent, sibling, root,
  299. RB_BLACK);
  300. break;
  301. } else {
  302. sibling = parent->rb_left;
  303. if (rb_is_red(sibling)) {
  304. /* Case 1 - right rotate at parent */
  305. parent->rb_left = tmp1 = sibling->rb_right;
  306. sibling->rb_right = parent;
  307. rb_set_parent_color(tmp1, parent, RB_BLACK);
  308. __rb_rotate_set_parents(parent, sibling, root,
  309. RB_RED);
  310. sibling = tmp1;
  311. }
  312. tmp1 = sibling->rb_left;
  313. if (!tmp1 || rb_is_black(tmp1)) {
  314. tmp2 = sibling->rb_right;
  315. if (!tmp2 || rb_is_black(tmp2)) {
  316. /* Case 2 - sibling color flip */
  317. rb_set_parent_color(sibling, parent,
  318. RB_RED);
  319. node = parent;
  320. parent = rb_parent(node);
  321. continue;
  322. }
  323. /* Case 3 - right rotate at sibling */
  324. sibling->rb_right = tmp1 = tmp2->rb_left;
  325. tmp2->rb_left = sibling;
  326. parent->rb_left = tmp2;
  327. if (tmp1)
  328. rb_set_parent_color(tmp1, sibling,
  329. RB_BLACK);
  330. tmp1 = sibling;
  331. sibling = tmp2;
  332. }
  333. /* Case 4 - left rotate at parent + color flips */
  334. parent->rb_left = tmp2 = sibling->rb_right;
  335. sibling->rb_right = parent;
  336. rb_set_parent_color(tmp1, sibling, RB_BLACK);
  337. if (tmp2)
  338. rb_set_parent(tmp2, parent);
  339. __rb_rotate_set_parents(parent, sibling, root,
  340. RB_BLACK);
  341. break;
  342. }
  343. }
  344. }
  345. void rb_erase(struct rb_node *node, struct rb_root *root)
  346. {
  347. struct rb_node *child, *parent;
  348. int color;
  349. if (!node->rb_left)
  350. child = node->rb_right;
  351. else if (!node->rb_right)
  352. child = node->rb_left;
  353. else {
  354. struct rb_node *old = node, *left;
  355. node = node->rb_right;
  356. while ((left = node->rb_left) != NULL)
  357. node = left;
  358. __rb_change_child(old, node, rb_parent(old), root);
  359. child = node->rb_right;
  360. parent = rb_parent(node);
  361. color = rb_color(node);
  362. if (parent == old) {
  363. parent = node;
  364. } else {
  365. if (child)
  366. rb_set_parent(child, parent);
  367. parent->rb_left = child;
  368. node->rb_right = old->rb_right;
  369. rb_set_parent(old->rb_right, node);
  370. }
  371. node->__rb_parent_color = old->__rb_parent_color;
  372. node->rb_left = old->rb_left;
  373. rb_set_parent(old->rb_left, node);
  374. goto color;
  375. }
  376. parent = rb_parent(node);
  377. color = rb_color(node);
  378. if (child)
  379. rb_set_parent(child, parent);
  380. __rb_change_child(node, child, parent, root);
  381. color:
  382. if (color == RB_BLACK)
  383. __rb_erase_color(child, parent, root);
  384. }
  385. EXPORT_SYMBOL(rb_erase);
  386. static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
  387. {
  388. struct rb_node *parent;
  389. up:
  390. func(node, data);
  391. parent = rb_parent(node);
  392. if (!parent)
  393. return;
  394. if (node == parent->rb_left && parent->rb_right)
  395. func(parent->rb_right, data);
  396. else if (parent->rb_left)
  397. func(parent->rb_left, data);
  398. node = parent;
  399. goto up;
  400. }
  401. /*
  402. * after inserting @node into the tree, update the tree to account for
  403. * both the new entry and any damage done by rebalance
  404. */
  405. void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
  406. {
  407. if (node->rb_left)
  408. node = node->rb_left;
  409. else if (node->rb_right)
  410. node = node->rb_right;
  411. rb_augment_path(node, func, data);
  412. }
  413. EXPORT_SYMBOL(rb_augment_insert);
  414. /*
  415. * before removing the node, find the deepest node on the rebalance path
  416. * that will still be there after @node gets removed
  417. */
  418. struct rb_node *rb_augment_erase_begin(struct rb_node *node)
  419. {
  420. struct rb_node *deepest;
  421. if (!node->rb_right && !node->rb_left)
  422. deepest = rb_parent(node);
  423. else if (!node->rb_right)
  424. deepest = node->rb_left;
  425. else if (!node->rb_left)
  426. deepest = node->rb_right;
  427. else {
  428. deepest = rb_next(node);
  429. if (deepest->rb_right)
  430. deepest = deepest->rb_right;
  431. else if (rb_parent(deepest) != node)
  432. deepest = rb_parent(deepest);
  433. }
  434. return deepest;
  435. }
  436. EXPORT_SYMBOL(rb_augment_erase_begin);
  437. /*
  438. * after removal, update the tree to account for the removed entry
  439. * and any rebalance damage.
  440. */
  441. void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
  442. {
  443. if (node)
  444. rb_augment_path(node, func, data);
  445. }
  446. EXPORT_SYMBOL(rb_augment_erase_end);
  447. /*
  448. * This function returns the first node (in sort order) of the tree.
  449. */
  450. struct rb_node *rb_first(const struct rb_root *root)
  451. {
  452. struct rb_node *n;
  453. n = root->rb_node;
  454. if (!n)
  455. return NULL;
  456. while (n->rb_left)
  457. n = n->rb_left;
  458. return n;
  459. }
  460. EXPORT_SYMBOL(rb_first);
  461. struct rb_node *rb_last(const struct rb_root *root)
  462. {
  463. struct rb_node *n;
  464. n = root->rb_node;
  465. if (!n)
  466. return NULL;
  467. while (n->rb_right)
  468. n = n->rb_right;
  469. return n;
  470. }
  471. EXPORT_SYMBOL(rb_last);
  472. struct rb_node *rb_next(const struct rb_node *node)
  473. {
  474. struct rb_node *parent;
  475. if (RB_EMPTY_NODE(node))
  476. return NULL;
  477. /*
  478. * If we have a right-hand child, go down and then left as far
  479. * as we can.
  480. */
  481. if (node->rb_right) {
  482. node = node->rb_right;
  483. while (node->rb_left)
  484. node=node->rb_left;
  485. return (struct rb_node *)node;
  486. }
  487. /*
  488. * No right-hand children. Everything down and left is smaller than us,
  489. * so any 'next' node must be in the general direction of our parent.
  490. * Go up the tree; any time the ancestor is a right-hand child of its
  491. * parent, keep going up. First time it's a left-hand child of its
  492. * parent, said parent is our 'next' node.
  493. */
  494. while ((parent = rb_parent(node)) && node == parent->rb_right)
  495. node = parent;
  496. return parent;
  497. }
  498. EXPORT_SYMBOL(rb_next);
  499. struct rb_node *rb_prev(const struct rb_node *node)
  500. {
  501. struct rb_node *parent;
  502. if (RB_EMPTY_NODE(node))
  503. return NULL;
  504. /*
  505. * If we have a left-hand child, go down and then right as far
  506. * as we can.
  507. */
  508. if (node->rb_left) {
  509. node = node->rb_left;
  510. while (node->rb_right)
  511. node=node->rb_right;
  512. return (struct rb_node *)node;
  513. }
  514. /*
  515. * No left-hand children. Go up till we find an ancestor which
  516. * is a right-hand child of its parent.
  517. */
  518. while ((parent = rb_parent(node)) && node == parent->rb_left)
  519. node = parent;
  520. return parent;
  521. }
  522. EXPORT_SYMBOL(rb_prev);
  523. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  524. struct rb_root *root)
  525. {
  526. struct rb_node *parent = rb_parent(victim);
  527. /* Set the surrounding nodes to point to the replacement */
  528. __rb_change_child(victim, new, parent, root);
  529. if (victim->rb_left)
  530. rb_set_parent(victim->rb_left, new);
  531. if (victim->rb_right)
  532. rb_set_parent(victim->rb_right, new);
  533. /* Copy the pointers/colour from the victim to the replacement */
  534. *new = *victim;
  535. }
  536. EXPORT_SYMBOL(rb_replace_node);