tree.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /* The MIT License
  2. Copyright (C) 2011, 2012 Zilong Tan ([email protected])
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  16. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  17. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. #include "tree.h"
  22. struct tree_root_np *
  23. tree_search(struct tree_root_np *entry,
  24. int (*compare)(const void *, const void *),
  25. struct tree_root_np *root)
  26. {
  27. int sgn;
  28. while (root) {
  29. sgn = compare(entry, root);
  30. if (sgn == 0)
  31. return root;
  32. if (sgn < 0)
  33. root = root->left;
  34. else
  35. root = root->right;
  36. }
  37. return root;
  38. }
  39. struct tree_root_np *
  40. tree_min(struct tree_root_np *root)
  41. {
  42. if (root) {
  43. while (root->left)
  44. root = root->left;
  45. }
  46. return root;
  47. }
  48. struct tree_root_np *
  49. tree_max(struct tree_root_np *root)
  50. {
  51. if (root) {
  52. while (root->right)
  53. root = root->right;
  54. }
  55. return root;
  56. }
  57. struct tree_root *
  58. tree_successor(struct tree_root *entry)
  59. {
  60. struct tree_root *succ = NULL;
  61. if (entry) {
  62. if (entry->right)
  63. return (struct tree_root *)TREE_MIN(entry->right);
  64. succ = entry->parent;
  65. while (succ && entry == succ->right) {
  66. entry = succ;
  67. succ = succ->parent;
  68. }
  69. }
  70. return succ;
  71. }
  72. struct tree_root *
  73. tree_predecessor(struct tree_root *entry)
  74. {
  75. struct tree_root *pred = NULL;
  76. if (entry) {
  77. if (entry->left)
  78. return (struct tree_root *)TREE_MAX(entry->left);
  79. pred = entry->parent;
  80. while (pred && entry == pred->left) {
  81. entry = pred;
  82. pred = pred->parent;
  83. }
  84. }
  85. return pred;
  86. }
  87. static inline void
  88. __rotate_left(struct tree_root *entry, struct tree_root **root)
  89. {
  90. struct tree_root *child;
  91. child = entry->right;
  92. entry->right = child->left;
  93. if (child->left)
  94. child->left->parent = entry;
  95. child->parent = entry->parent;
  96. if (entry->parent == NULL)
  97. *root = child;
  98. else if (entry == entry->parent->left)
  99. entry->parent->left = child;
  100. else
  101. entry->parent->right = child;
  102. child->left = entry;
  103. entry->parent = child;
  104. }
  105. static inline void
  106. __rotate_right(struct tree_root *entry,
  107. struct tree_root **root)
  108. {
  109. struct tree_root *child;
  110. child = entry->left;
  111. entry->left = child->right;
  112. if (child->right)
  113. child->right->parent = entry;
  114. child->parent = entry->parent;
  115. if (entry->parent == NULL)
  116. *root = child;
  117. else if (entry == entry->parent->left)
  118. entry->parent->left = child;
  119. else
  120. entry->parent->right = child;
  121. child->right = entry;
  122. entry->parent = child;
  123. }
  124. void tree_add(struct tree_root *new,
  125. int (*compare)(const void *, const void *),
  126. struct tree_root **root)
  127. {
  128. int sgn = 0;
  129. struct tree_root *next = *root;
  130. struct tree_root *cur = NULL;
  131. INIT_TREE_ROOT(new);
  132. while (next) {
  133. cur = next;
  134. sgn = compare(new, next);
  135. if (sgn < 0)
  136. next = next->left;
  137. else
  138. next = next->right;
  139. }
  140. new->parent = cur;
  141. if (cur == NULL)
  142. *root = new;
  143. else if (sgn < 0)
  144. cur->left = new;
  145. else
  146. cur->right = new;
  147. }
  148. struct tree_root *
  149. tree_map(struct tree_root *new,
  150. int (*compare)(const void *, const void *),
  151. struct tree_root **root)
  152. {
  153. int sgn = 0;
  154. struct tree_root *next = *root;
  155. struct tree_root *cur = NULL;
  156. INIT_TREE_ROOT(new);
  157. while (next) {
  158. cur = next;
  159. sgn = compare(new, next);
  160. if (sgn == 0)
  161. return next;
  162. else if (sgn < 0)
  163. next = next->left;
  164. else
  165. next = next->right;
  166. }
  167. new->parent = cur;
  168. if (cur == NULL)
  169. *root = new;
  170. else if (sgn < 0)
  171. cur->left = new;
  172. else
  173. cur->right = new;
  174. return new;
  175. }
  176. void
  177. tree_del(struct tree_root *entry, struct tree_root **root)
  178. {
  179. struct tree_root *child;
  180. struct tree_root *succ;
  181. if (entry->left == NULL || entry->right == NULL)
  182. succ = entry;
  183. else
  184. succ = TREE_SUCCESSOR(entry);
  185. if (succ->left)
  186. child = succ->left;
  187. else
  188. child = succ->right;
  189. if (child)
  190. child->parent = succ->parent;
  191. if (succ->parent == NULL)
  192. *root = child;
  193. else if (succ == succ->parent->left)
  194. succ->parent->left = child;
  195. else
  196. succ->parent->right = child;
  197. if (succ != entry) {
  198. succ->left = entry->left;
  199. if (entry->left)
  200. entry->left->parent = succ;
  201. succ->right = entry->right;
  202. if (entry->right)
  203. entry->right->parent = succ;
  204. succ->parent = entry->parent;
  205. if (entry->parent == NULL)
  206. *root = succ;
  207. else if (entry == entry->parent->left)
  208. entry->parent->left = succ;
  209. else
  210. entry->parent->right = succ;
  211. }
  212. }
  213. #define SPLAY_ROTATE_RIGHT(entry, tmp) do { \
  214. (entry)->left = (tmp)->right; \
  215. if ((tmp)->right) (tmp)->right->parent = (entry); \
  216. (tmp)->right = (entry); \
  217. (tmp)->parent = (entry)->parent; \
  218. (entry)->parent = (tmp); \
  219. (entry) = (tmp); \
  220. } while (0)
  221. #define SPLAY_ROTATE_LEFT(entry, tmp) do { \
  222. (entry)->right = (tmp)->left; \
  223. if ((tmp)->left) (tmp)->left->parent = (entry); \
  224. (tmp)->left = (entry); \
  225. (tmp)->parent = (entry)->parent; \
  226. (entry)->parent = (tmp); \
  227. (entry) = (tmp); \
  228. } while (0)
  229. #define SPLAY_LINK_RIGHT(entry, large) do { \
  230. (large)->left = (entry); \
  231. (entry)->parent = (large); \
  232. (large) = (entry); \
  233. (entry) = (entry)->left; \
  234. } while (0)
  235. #define SPLAY_LINK_LEFT(entry, small) do { \
  236. (small)->right = (entry); \
  237. (entry)->parent = (small); \
  238. (small) = (entry); \
  239. (entry) = (entry)->right; \
  240. } while (0)
  241. #define SPLAY_ASSEMBLE(head, node, small, large) do { \
  242. (small)->right = (head)->left; \
  243. if ((head)->left) \
  244. (head)->left->parent = (small); \
  245. (large)->left = (head)->right; \
  246. if ((head)->right) \
  247. (head)->right->parent = (large); \
  248. (head)->left = (node)->right; \
  249. if ((node)->right) \
  250. (node)->right->parent = (head); \
  251. (head)->right = (node)->left; \
  252. if ((node)->left) \
  253. (node)->left->parent = (head); \
  254. } while (0)
  255. #define SPLAY_ROTATE_RIGHT_NP(entry, tmp) do { \
  256. (entry)->left = (tmp)->right; \
  257. (tmp)->right = (entry); \
  258. (entry) = (tmp); \
  259. } while (0)
  260. #define SPLAY_ROTATE_LEFT_NP(entry, tmp) do { \
  261. (entry)->right = (tmp)->left; \
  262. (tmp)->left = (entry); \
  263. (entry) = (tmp); \
  264. } while (0)
  265. #define SPLAY_LINK_RIGHT_NP(entry, large) do { \
  266. (large)->left = (entry); \
  267. (large) = (entry); \
  268. (entry) = (entry)->left; \
  269. } while (0)
  270. #define SPLAY_LINK_LEFT_NP(entry, small) do { \
  271. (small)->right = (entry); \
  272. (small) = (entry); \
  273. (entry) = (entry)->right; \
  274. } while (0)
  275. #define SPLAY_ASSEMBLE_NP(head, node, small, large) do { \
  276. (small)->right = (head)->left; \
  277. (large)->left = (head)->right; \
  278. (head)->left = (node)->right; \
  279. (head)->right = (node)->left; \
  280. } while (0)
  281. struct tree_root *
  282. splay_search(struct tree_root *entry,
  283. int (*compare)(const void *, const void *),
  284. struct tree_root **root)
  285. {
  286. int cmp;
  287. TREE_ROOT(node); /* node for assembly use */
  288. struct tree_root *small, *large, *head, *tmp;
  289. head = *root;
  290. small = large = &node;
  291. while ((cmp = compare(entry, head)) != 0) {
  292. if (cmp < 0) {
  293. tmp = head->left;
  294. if (tmp == NULL)
  295. break;
  296. if (compare(entry, tmp) < 0) {
  297. SPLAY_ROTATE_RIGHT(head, tmp);
  298. if (head->left == NULL)
  299. break;
  300. }
  301. SPLAY_LINK_RIGHT(head, large);
  302. } else {
  303. tmp = head->right;
  304. if (tmp == NULL)
  305. break;
  306. if (compare(entry, tmp) > 0) {
  307. SPLAY_ROTATE_LEFT(head, tmp);
  308. if (head->right == NULL)
  309. break;
  310. }
  311. SPLAY_LINK_LEFT(head, small);
  312. }
  313. }
  314. head->parent = NULL;
  315. SPLAY_ASSEMBLE(head, &node, small, large);
  316. *root = head;
  317. if (cmp != 0)
  318. return NULL;
  319. return head;
  320. }
  321. struct tree_root *
  322. splay_map(struct tree_root *new,
  323. int (*compare)(const void *, const void *),
  324. struct tree_root **root)
  325. {
  326. int cmp;
  327. TREE_ROOT(node); /* node for assembly use */
  328. struct tree_root *small, *large, *head, *tmp;
  329. INIT_TREE_ROOT(new);
  330. small = large = &node;
  331. head = *root;
  332. while (head && (cmp = compare(new, head)) != 0) {
  333. if (cmp < 0) {
  334. tmp = head->left;
  335. if (tmp == NULL) {
  336. /* zig */
  337. SPLAY_LINK_RIGHT(head, large);
  338. break;
  339. }
  340. cmp = compare(new, tmp);
  341. if (cmp < 0) {
  342. /* zig-zig */
  343. SPLAY_ROTATE_RIGHT(head, tmp);
  344. SPLAY_LINK_RIGHT(head, large);
  345. } else if (cmp > 0) {
  346. /* zig-zag */
  347. SPLAY_LINK_RIGHT(head, large);
  348. SPLAY_LINK_LEFT(head, small);
  349. } else {
  350. /* zig */
  351. SPLAY_LINK_RIGHT(head, large);
  352. break;
  353. }
  354. } else {
  355. tmp = head->right;
  356. if (tmp == NULL) {
  357. /* zag */
  358. SPLAY_LINK_LEFT(head, small);
  359. break;
  360. }
  361. cmp = compare(new, tmp);
  362. if (cmp > 0) {
  363. /* zag-zag */
  364. SPLAY_ROTATE_LEFT(head, tmp);
  365. SPLAY_LINK_LEFT(head, small);
  366. } else if (cmp < 0) {
  367. /* zag-zig */
  368. SPLAY_LINK_LEFT(head, small);
  369. SPLAY_LINK_RIGHT(head, large);
  370. } else {
  371. /* zag */
  372. SPLAY_LINK_LEFT(head, small);
  373. break;
  374. }
  375. }
  376. }
  377. if (head == NULL)
  378. head = new;
  379. head->parent = NULL;
  380. SPLAY_ASSEMBLE(head, &node, small, large);
  381. *root = head;
  382. return head;
  383. }
  384. struct tree_root_np *
  385. splay_search_np(struct tree_root_np *entry,
  386. int (*compare)(const void *, const void *),
  387. struct tree_root_np **root)
  388. {
  389. int cmp;
  390. TREE_ROOT_NP(node); /* node for assembly use */
  391. struct tree_root_np *small, *large, *head, *tmp;
  392. head = *root;
  393. small = large = &node;
  394. while ((cmp = compare(entry, head)) != 0) {
  395. if (cmp < 0) {
  396. tmp = head->left;
  397. if (tmp == NULL)
  398. break;
  399. if (compare(entry, tmp) < 0) {
  400. SPLAY_ROTATE_RIGHT_NP(head, tmp);
  401. if (head->left == NULL)
  402. break;
  403. }
  404. SPLAY_LINK_RIGHT_NP(head, large);
  405. } else {
  406. tmp = head->right;
  407. if (tmp == NULL)
  408. break;
  409. if (compare(entry, tmp) > 0) {
  410. SPLAY_ROTATE_LEFT_NP(head, tmp);
  411. if (head->right == NULL)
  412. break;
  413. }
  414. SPLAY_LINK_LEFT_NP(head, small);
  415. }
  416. }
  417. SPLAY_ASSEMBLE_NP(head, &node, small, large);
  418. *root = head;
  419. if (cmp != 0)
  420. return NULL;
  421. return head;
  422. }
  423. struct tree_root_np *
  424. splay_map_np(struct tree_root_np *new,
  425. int (*compare)(const void *, const void *),
  426. struct tree_root_np **root)
  427. {
  428. int cmp;
  429. TREE_ROOT_NP(node); /* node for assembly use */
  430. struct tree_root_np *small, *large, *head, *tmp;
  431. INIT_TREE_ROOT_NP(new);
  432. small = large = &node;
  433. head = *root;
  434. while (head && (cmp = compare(new, head)) != 0) {
  435. if (cmp < 0) {
  436. tmp = head->left;
  437. if (tmp == NULL) {
  438. /* zig */
  439. SPLAY_LINK_RIGHT_NP(head, large);
  440. break;
  441. }
  442. cmp = compare(new, tmp);
  443. if (cmp < 0) {
  444. /* zig-zig */
  445. SPLAY_ROTATE_RIGHT_NP(head, tmp);
  446. SPLAY_LINK_RIGHT_NP(head, large);
  447. } else if (cmp > 0) {
  448. /* zig-zag */
  449. SPLAY_LINK_RIGHT_NP(head, large);
  450. SPLAY_LINK_LEFT_NP(head, small);
  451. } else {
  452. /* zig */
  453. SPLAY_LINK_RIGHT_NP(head, large);
  454. break;
  455. }
  456. } else {
  457. tmp = head->right;
  458. if (tmp == NULL) {
  459. /* zag */
  460. SPLAY_LINK_LEFT_NP(head, small);
  461. break;
  462. }
  463. cmp = compare(new, tmp);
  464. if (cmp > 0) {
  465. /* zag-zag */
  466. SPLAY_ROTATE_LEFT_NP(head, tmp);
  467. SPLAY_LINK_LEFT_NP(head, small);
  468. } else if (cmp < 0) {
  469. /* zag-zig */
  470. SPLAY_LINK_LEFT_NP(head, small);
  471. SPLAY_LINK_RIGHT_NP(head, large);
  472. } else {
  473. /* zag */
  474. SPLAY_LINK_LEFT_NP(head, small);
  475. break;
  476. }
  477. }
  478. }
  479. if (head == NULL)
  480. head = new;
  481. SPLAY_ASSEMBLE_NP(head, &node, small, large);
  482. *root = head;
  483. return head;
  484. }
  485. static inline void
  486. __avl_balance(struct avl_root *new, struct avl_root **root)
  487. {
  488. int balance = 0;
  489. struct avl_root *child, *grandson;
  490. while (new->parent && balance == 0) {
  491. balance = new->parent->balance;
  492. if (new == new->parent->left)
  493. new->parent->balance--;
  494. else
  495. new->parent->balance++;
  496. new = new->parent;
  497. }
  498. if (new->balance == -2) {
  499. child = new->left;
  500. if (child->balance == -1) {
  501. __rotate_right((struct tree_root *)new,
  502. (struct tree_root **)root);
  503. child->balance = 0;
  504. new->balance = 0;
  505. } else {
  506. grandson = child->right;
  507. __rotate_left((struct tree_root *)child,
  508. (struct tree_root **)root);
  509. __rotate_right((struct tree_root *)new,
  510. (struct tree_root **)root);
  511. if (grandson->balance == -1) {
  512. child->balance = 0;
  513. new->balance = 1;
  514. } else if (grandson->balance == 0) {
  515. child->balance = 0;
  516. new->balance = 0;
  517. } else {
  518. child->balance = -1;
  519. new->balance = 0;
  520. }
  521. grandson->balance = 0;
  522. }
  523. } else if (new->balance == 2) {
  524. child = new->right;
  525. if (child->balance == 1) {
  526. __rotate_left((struct tree_root *)new,
  527. (struct tree_root **)root);
  528. child->balance = 0;
  529. new->balance = 0;
  530. } else {
  531. grandson = child->left;
  532. __rotate_right((struct tree_root *)child,
  533. (struct tree_root **)root);
  534. __rotate_left((struct tree_root *)new,
  535. (struct tree_root **)root);
  536. if (grandson->balance == -1) {
  537. child->balance = 1;
  538. new->balance = 0;
  539. } else if (grandson->balance == 0) {
  540. child->balance = 0;
  541. new->balance = 0;
  542. } else {
  543. child->balance = 0;
  544. new->balance = -1;
  545. }
  546. grandson->balance = 0;
  547. }
  548. }
  549. }
  550. void avl_add(struct avl_root *new,
  551. int (*compare)(const void *, const void *),
  552. struct avl_root **root)
  553. {
  554. new->balance = 0;
  555. TREE_ADD(new, compare, root);
  556. __avl_balance(new, root);
  557. }
  558. struct avl_root *
  559. avl_map(struct avl_root *new,
  560. int (*compare)(const void *, const void *),
  561. struct avl_root **root)
  562. {
  563. struct avl_root *node;
  564. new->balance = 0;
  565. node = (struct avl_root *)TREE_MAP(new, compare, root);
  566. if (node != new)
  567. return node;
  568. __avl_balance(new, root);
  569. return new;
  570. }
  571. void avl_del(struct avl_root *entry, struct avl_root **root)
  572. {
  573. int dir = 0;
  574. int dir_next = 0;
  575. struct avl_root *unbalanced, *child, *succ, *parent;
  576. if (entry->right == NULL) {
  577. /* Case 1: entry has no right child */
  578. if (entry->left)
  579. entry->left->parent = entry->parent;
  580. if (entry->parent == NULL) {
  581. *root = entry->left;
  582. return;
  583. } else if (entry == entry->parent->left) {
  584. entry->parent->left = entry->left;
  585. dir = 0;
  586. } else {
  587. entry->parent->right = entry->left;
  588. dir = 1;
  589. }
  590. unbalanced = entry->parent;
  591. } else if (entry->right->left == NULL) {
  592. /* Case 2: entry's right child has no left child */
  593. entry->right->left = entry->left;
  594. if (entry->left)
  595. entry->left->parent = entry->right;
  596. entry->right->parent = entry->parent;
  597. if (entry->parent == NULL)
  598. *root = entry->right;
  599. else if (entry == entry->parent->left)
  600. entry->parent->left = entry->right;
  601. else
  602. entry->parent->right = entry->right;
  603. entry->right->balance = entry->balance;
  604. unbalanced = entry->right;
  605. dir = 1;
  606. } else {
  607. /* Case 3: entry's right child has a left child */
  608. succ = (struct avl_root *)TREE_SUCCESSOR(entry);
  609. if (succ->right)
  610. succ->right->parent = succ->parent;
  611. succ->parent->left = succ->right;
  612. unbalanced = succ->parent;
  613. succ->left = entry->left;
  614. entry->left->parent = succ;
  615. succ->right = entry->right;
  616. entry->right->parent = succ;
  617. succ->parent = entry->parent;
  618. if (entry->parent == NULL)
  619. *root = succ;
  620. else if (entry == entry->parent->left)
  621. entry->parent->left = succ;
  622. else
  623. entry->parent->right = succ;
  624. succ->balance = entry->balance;
  625. dir = 0;
  626. }
  627. for (;;) {
  628. parent = unbalanced->parent;
  629. if (parent)
  630. dir_next = (unbalanced == parent->right);
  631. if (dir == 0) {
  632. ++unbalanced->balance;
  633. if (unbalanced->balance == 1)
  634. break;
  635. if (unbalanced->balance == 2) {
  636. child = unbalanced->right;
  637. if (child->balance == -1) {
  638. succ = child->left;
  639. __rotate_right((struct tree_root *)child,
  640. (struct tree_root **)root);
  641. __rotate_left((struct tree_root *)unbalanced,
  642. (struct tree_root **)root);
  643. if (succ->balance == -1) {
  644. child->balance = 1;
  645. unbalanced->balance = 0;
  646. } else if (succ->balance == 0) {
  647. child->balance = 0;
  648. unbalanced->balance = 0;
  649. } else {
  650. child->balance = 0;
  651. unbalanced->balance = -1;
  652. }
  653. succ->balance = 0;
  654. } else {
  655. __rotate_left((struct tree_root *)unbalanced,
  656. (struct tree_root **)root);
  657. if (child->balance == 0) {
  658. child->balance = -1;
  659. unbalanced->balance = 1;
  660. break;
  661. } else {
  662. child->balance = 0;
  663. unbalanced->balance = 0;
  664. }
  665. }
  666. }
  667. } else {
  668. --unbalanced->balance;
  669. if (unbalanced->balance == -1)
  670. break;
  671. if (unbalanced->balance == -2) {
  672. child = unbalanced->left;
  673. if (child->balance == 1) {
  674. succ = child->right;
  675. __rotate_left((struct tree_root *)child,
  676. (struct tree_root **)root);
  677. __rotate_right((struct tree_root *)unbalanced,
  678. (struct tree_root **)root);
  679. if (succ->balance == -1) {
  680. child->balance = 0;
  681. unbalanced->balance = 1;
  682. } else if (succ->balance == 0) {
  683. child->balance = 0;
  684. unbalanced->balance = 0;
  685. } else {
  686. child->balance = -1;
  687. unbalanced->balance = 0;
  688. }
  689. succ->balance = 0;
  690. } else {
  691. __rotate_right((struct tree_root *)unbalanced,
  692. (struct tree_root **)root);
  693. if (child->balance == 0) {
  694. child->balance = 1;
  695. unbalanced->balance = -1;
  696. break;
  697. } else {
  698. child->balance = 0;
  699. unbalanced->balance = 0;
  700. }
  701. }
  702. }
  703. }
  704. if (parent == NULL)
  705. break;
  706. dir = dir_next;
  707. unbalanced = parent;
  708. }
  709. }