binary_tree_common.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*!
  2. Temelia - Binary tree <common part> implementation source file.
  3. Copyright (C) 2008, 2009 Ceata (http://ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #include "include/binary_tree_common.h"
  18. #include "include/queue.h"
  19. #include <stdlib.h>
  20. static binary_search_tree_t __binary_search_tree_remove(
  21. binary_search_tree_t *bst, binary_search_tree_t deleted);
  22. /*
  23. * Forward declaration
  24. *
  25. * common.c: line 166
  26. */
  27. extern int temelia_inited;
  28. void temelia_init();
  29. _binary_tree_t _binary_tree_new(void *key)
  30. {
  31. _binary_tree_t tree;
  32. if (!temelia_inited)
  33. temelia_init();
  34. tree = (struct _bt_node_t *) _new(sizeof(struct _bt_node_t));
  35. /*
  36. * Encapsulates the key into a node with NULL children.
  37. */
  38. tree->key = key;
  39. tree->parent = tree->left = tree->right = NULL;
  40. return tree;
  41. }
  42. void _binary_tree_delete_node(_binary_tree_node_t node)
  43. {
  44. _ASSERT(node, ==, NULL, NULL_POINTER,);
  45. /*
  46. * Sets the internal pointers to NULL.
  47. */
  48. node->key = node->parent = node->left = node->right = NULL;
  49. _delete(node);
  50. }
  51. void _binary_tree_delete(_binary_tree_t tree)
  52. {
  53. _ASSERT(tree, ==, NULL, NULL_POINTER,);
  54. /*
  55. * delete(node)
  56. * delete the left child
  57. * delete the right child
  58. * delete the current node
  59. */
  60. _binary_tree_delete(tree->left);
  61. _binary_tree_delete(tree->right);
  62. _binary_tree_delete_node(tree);
  63. }
  64. int _binary_tree_compare_trees(_binary_tree_t bt1, _binary_tree_t bt2,
  65. int compare(void *x, void *y, void *context), void *context)
  66. {
  67. int left, right, aux;
  68. /*
  69. * Two NULL trees are equal.
  70. */
  71. if (bt1 == NULL && bt2 == NULL)
  72. return 0;
  73. /*
  74. * If the first tree is NULL then
  75. * return compare(NULL, root key of the second tree)
  76. * else if the second tree is NULL then
  77. * return compare(root key of the first tree, NULL)
  78. * else
  79. * left = compare(left1, left2);
  80. * right = compare(left1, right2);
  81. * if left trees differ
  82. * return left;
  83. * if root's keys differ
  84. * return comparison result of the keys
  85. * return the right
  86. */
  87. _ASSERT(bt1, ==, NULL, NULL_POINTER,
  88. /* compare(NULL, _binary_tree_get_key(bt2), context) */ -1);
  89. _ASSERT (bt2, ==, NULL, NULL_POINTER,
  90. /* compare(_binary_tree_get_key(bt1), NULL, context) */ 1);
  91. _ASSERT(compare, ==, NULL, NULL_POINTER, 0);
  92. left = right = aux = 0;
  93. left = _binary_tree_compare_trees(bt1->left, bt1->left, compare, context);
  94. right
  95. = _binary_tree_compare_trees(bt1->right, bt2->right, compare,
  96. context);
  97. if (left)
  98. return left;
  99. aux = compare(bt1->key, bt2->key, context);
  100. if (aux)
  101. return aux;
  102. return right;
  103. }
  104. int _binary_tree_is_empty(_binary_tree_t tree)
  105. {
  106. _ASSERT(tree, ==, NULL, NULL_POINTER, -1);
  107. /*
  108. * An empty tree has all it's internal pointers equal to NULL
  109. */
  110. return ((tree->left == NULL) && (tree->parent == NULL) && (tree->right
  111. == NULL) && (tree->key == NULL));
  112. }
  113. int _binary_tree_leaf(_binary_tree_t node)
  114. {
  115. _ASSERT(node, ==, NULL, NULL_POINTER, -1);
  116. /*
  117. * A node is leaf only if it's children are NULL.
  118. */
  119. return ((node->left == NULL) && (node->right == NULL));
  120. }
  121. int _binary_tree_get_size(_binary_tree_t tree)
  122. {
  123. _ASSERT(tree, ==, NULL, NOT_EXCEPTION, 0);
  124. /*
  125. * size(NULL) = 0
  126. * size(node) = 1 + size(left child) + size(right child)
  127. */
  128. return _binary_tree_get_size(tree->left) + _binary_tree_get_size(
  129. tree->right) + 1;
  130. }
  131. _binary_tree_t _binary_tree_get_root(_binary_tree_t node)
  132. {
  133. _ASSERT(node, ==, NULL, NULL_POINTER, NULL);
  134. while (node->parent != NULL)
  135. node = node->parent;
  136. return node;
  137. }
  138. void *_binary_tree_get_key(_binary_tree_t node)
  139. {
  140. _ASSERT(node, ==, NULL, NULL_POINTER, NULL);
  141. return node->key;
  142. }
  143. void _binary_tree_set_key(_binary_tree_t node, void *key)
  144. {
  145. _ASSERT(node, ==, NULL, NULL_POINTER,);
  146. node->key = key;
  147. }
  148. _binary_tree_t _binary_tree_get_parent(_binary_tree_t node)
  149. {
  150. _ASSERT(node, ==, NULL, NULL_POINTER, NULL);
  151. return node->parent;
  152. }
  153. void _binary_tree_set_parent(_binary_tree_t node, _binary_tree_t parent,
  154. int dir)
  155. {
  156. _ASSERT(node, ==, NULL, NULL_POINTER,);
  157. _ASSERT(parent, ==, NULL, NULL_POINTER,);
  158. if (dir == -1)
  159. _binary_tree_set_left_child(parent, node);
  160. else if (dir == 1)
  161. _binary_tree_set_right_child(parent, node);
  162. else
  163. temelia_errno = INVALID_INPUT;
  164. }
  165. _binary_tree_t _binary_tree_get_left_child(_binary_tree_t node)
  166. {
  167. _ASSERT(node, ==, NULL, NULL_POINTER, NULL);
  168. return node->left;
  169. }
  170. void _binary_tree_set_left_child(_binary_tree_t node, _binary_tree_t child)
  171. {
  172. _ASSERT(node, ==, NULL, NULL_POINTER,);
  173. node->left = child;
  174. // If NULL left child is given, don't set the parent link.
  175. if (child)
  176. child->parent = node;
  177. }
  178. _binary_tree_t _binary_tree_get_right_child(_binary_tree_t node)
  179. {
  180. _ASSERT(node, ==, NULL, NULL_POINTER, NULL);
  181. return node->right;
  182. }
  183. void _binary_tree_set_right_child(_binary_tree_t node, _binary_tree_t child)
  184. {
  185. _ASSERT(node, ==, NULL, NULL_POINTER,);
  186. node->right = child;
  187. // If NULL right child is given, don't set the parent link.
  188. if (child)
  189. child->parent = node;
  190. }
  191. void _binary_tree_join(_binary_tree_t parent, _binary_tree_t left,
  192. _binary_tree_t right)
  193. {
  194. _ASSERT(right, ==, NULL, NULL_POINTER,);
  195. _ASSERT(left, ==, NULL, NULL_POINTER,);
  196. _ASSERT(parent, ==, NULL, NULL_POINTER,);
  197. /*
  198. * Creates links between parent<->left and parent<->right
  199. * parent
  200. * parent, left, right => / \
  201. * left right
  202. */
  203. _binary_tree_set_left_child(parent, left);
  204. _binary_tree_set_right_child(parent, right);
  205. _binary_tree_set_parent(left, parent, -1);
  206. _binary_tree_set_parent(right, parent, 1);
  207. }
  208. void _binary_tree_split(_binary_tree_t parent, _binary_tree_t *left,
  209. _binary_tree_t *right)
  210. {
  211. _ASSERT(parent, ==, NULL, NULL_POINTER,);
  212. _ASSERT(left, ==, NULL, NULL_POINTER,);
  213. _ASSERT(right, ==, NULL, NULL_POINTER,);
  214. /*
  215. * Splits the tree into 3 trees
  216. * parent
  217. * / \ => parent, left, right
  218. * left right
  219. */
  220. *left = parent->left;
  221. parent->left = NULL;
  222. *right = parent->right;
  223. parent->right = NULL;
  224. }
  225. _binary_tree_t _binary_tree_create_node(_binary_tree_t parent,
  226. _binary_tree_t left, _binary_tree_t right, void *key, int dir)
  227. {
  228. _binary_tree_t new_node = _binary_tree_new(key);
  229. _ASSERT(parent, ==, NULL, NULL_POINTER, NULL);
  230. _ASSERT(new_node, ==, NULL, NULL_POINTER, NULL);
  231. #ifndef RELEASE
  232. if (left && _binary_tree_get_parent(left) != parent)
  233. LOGGER(
  234. "[_binary_tree_create_node] invalid parent pointer %p for left child %p\n",
  235. parent, left);
  236. if (right && _binary_tree_get_parent(right) != parent)
  237. LOGGER(
  238. "[_binary_tree_create_node] invalid parent pointer %p for left child %p\n",
  239. parent, right);
  240. #endif
  241. _binary_tree_set_left_child(new_node, left);
  242. _binary_tree_set_right_child(new_node, right);
  243. /*
  244. * dir == -1
  245. * parent, left, right, key => parent
  246. * /
  247. * new_node(key)
  248. * / \
  249. * left right
  250. *
  251. * dir == 1
  252. * parent, left, right, key => parent
  253. * \
  254. * new_node(key)
  255. * / \
  256. * left right
  257. */
  258. if (dir == -1)
  259. _binary_tree_set_left_child(parent, new_node);
  260. else if (dir == 1)
  261. _binary_tree_set_right_child(parent, new_node);
  262. else
  263. {
  264. temelia_errno = INVALID_INPUT;
  265. return NULL;
  266. }
  267. return new_node;
  268. }
  269. int _binary_tree_get_height(_binary_tree_t tree)
  270. {
  271. _ASSERT(tree, ==, NULL, NOT_EXCEPTION, 0);
  272. return 1 + MAX(_binary_tree_get_height(tree->left),
  273. _binary_tree_get_height(tree->right));
  274. }
  275. int _binary_tree_get_depth(_binary_tree_t node)
  276. {
  277. _ASSERT(node, ==, NULL, NOT_EXCEPTION, -1);
  278. return 1 + _binary_tree_get_depth(_binary_tree_get_parent(node));
  279. }
  280. void _binary_tree_preorder(_binary_tree_t tree, void key_handler(void *key,
  281. void *context), void *context)
  282. {
  283. _ASSERT(tree, ==, NULL, NOT_EXCEPTION,);
  284. _ASSERT(key_handler, ==, NULL, NULL_POINTER,);
  285. key_handler(tree->key, context);
  286. _binary_tree_preorder(tree->left, key_handler, context);
  287. _binary_tree_preorder(tree->right, key_handler, context);
  288. }
  289. void _binary_tree_inorder(_binary_tree_t tree, void key_handler(void *key,
  290. void *context), void *context)
  291. {
  292. _ASSERT(tree, ==, NULL, NOT_EXCEPTION,);
  293. _ASSERT(key_handler, ==, NULL, NULL_POINTER,);
  294. _binary_tree_inorder(tree->left, key_handler, context);
  295. key_handler(tree->key, context);
  296. _binary_tree_inorder(tree->right, key_handler, context);
  297. }
  298. void _binary_tree_reverse_inorder(_binary_tree_t tree, void key_handler(
  299. void *key, void *context), void *context)
  300. {
  301. _ASSERT(tree, ==, NULL, NOT_EXCEPTION,);
  302. _ASSERT(key_handler, ==, NULL, NULL_POINTER,);
  303. _binary_tree_reverse_inorder(tree->right, key_handler, context);
  304. key_handler(tree->key, context);
  305. _binary_tree_reverse_inorder(tree->left, key_handler, context);
  306. }
  307. void _binary_tree_postorder(_binary_tree_t tree, void key_handler(void *key,
  308. void *context), void *context)
  309. {
  310. _ASSERT(tree, ==, NULL, NOT_EXCEPTION,);
  311. _ASSERT(key_handler, ==, NULL, NULL_POINTER,);
  312. _binary_tree_postorder(tree->left, key_handler, context);
  313. _binary_tree_postorder(tree->right, key_handler, context);
  314. key_handler(tree->key, context);
  315. }
  316. void _binary_tree_level_order(_binary_tree_t tree, void key_handler(void *key,
  317. void *context), void *context)
  318. {
  319. queue_t Q = queue_new(DEFAULT_SIZE);
  320. _binary_tree_node_t current_node;
  321. queue_push_back(Q, tree);
  322. while (!queue_is_empty(Q))
  323. {
  324. current_node = queue_get_front(Q);
  325. queue_pop_front(Q);
  326. key_handler(current_node->key, context);
  327. if (current_node->left)
  328. queue_push_back(Q, current_node->left);
  329. if (current_node->right)
  330. queue_push_back(Q, current_node->right);
  331. }
  332. queue_delete(Q);
  333. }
  334. void _binary_tree_show_indented(_binary_tree_t tree, void key_handler(
  335. void *key, int level, void *context), void *context, int level)
  336. {
  337. _ASSERT(tree, ==, NULL, NOT_EXCEPTION,);
  338. _ASSERT(key_handler, ==, NULL, NULL_POINTER,);
  339. _binary_tree_show_indented(tree->right, key_handler, context, level + 1);
  340. key_handler(tree->key, level, context);
  341. _binary_tree_show_indented(tree->left, key_handler, context, level + 1);
  342. }
  343. _binary_search_tree_node_t _binary_search_tree_search(binary_search_tree_t bst,
  344. void *key, int compare(void *x, void *y, void *context), void *context)
  345. {
  346. _ASSERT(bst, ==, NULL, NULL_POINTER, NULL);
  347. _ASSERT(compare, ==, NULL, NULL_POINTER, NULL);
  348. // If the key is in the current tree node, return node's reference.
  349. if (!compare(bst->key, key, context))
  350. return bst;
  351. // If the current key is greater then root's key, search the key in the left sub-tree
  352. if (compare(key, bst->key, context) < 0)
  353. return _binary_search_tree_search(bst->left, key, compare, context);
  354. // Else search in the right sub-tree.
  355. return _binary_search_tree_search(bst->right, key, compare, context);
  356. }
  357. _binary_search_tree_node_t _binary_search_tree_insert(binary_search_tree_t bst,
  358. void *key, int compare(void *x, void *y, void *context), void *context)
  359. {
  360. int direction = 0;
  361. _binary_search_tree_node_t aux, pred;
  362. LOGGER(
  363. "[{internal} binary search tree insert] bst %p, key %p, compare %p, context %p\n",
  364. bst, key, compare, context);
  365. _ASSERT(bst, ==, NULL, NULL_POINTER, NULL);
  366. _ASSERT(compare, ==, NULL, NULL_POINTER, NULL);
  367. if (_binary_tree_is_empty(bst))
  368. return NULL;
  369. aux = bst;
  370. while (aux)
  371. {
  372. pred = aux;
  373. // If key already exists, return NULL.
  374. if (!compare(key, aux->key, context))
  375. return NULL;
  376. // If key is lower than node's key then go to left node.
  377. if (compare(key, aux->key, context) < 0)
  378. {
  379. aux = aux->left;
  380. direction = -1;
  381. }
  382. // Else go to right node.
  383. else
  384. {
  385. aux = aux->right;
  386. direction = 1;
  387. }
  388. }
  389. /*
  390. * Creates the new node in the computed direction; set it a parent and NULL children.
  391. */
  392. return _binary_tree_create_node(pred, NULL, NULL, key, direction);
  393. }
  394. _binary_search_tree_node_t _binary_search_tree_remove(
  395. binary_search_tree_t *bst, void *key, int compare(void *x, void *y,
  396. void *context), void *context, binary_search_tree_t *my_deleted)
  397. {
  398. void *aux;
  399. binary_tree_node_t deleted, replaced;
  400. _ASSERT(bst, ==, NULL, NULL_POINTER, NULL);
  401. _ASSERT (*bst, ==, NULL, NULL_POINTER, NULL);
  402. _ASSERT(compare, ==, NULL, NULL_POINTER, NULL);
  403. deleted = _binary_search_tree_search(*bst, key, compare, context);
  404. *my_deleted = NULL;
  405. _ASSERT(deleted, ==, NULL, ELEMENT_NOT_FOUND, NULL);
  406. // There are 4 cases :
  407. // 1) deleted node is leaf : simply remove it.
  408. // 2) deleted node has 1 child : change child's parent to deleted node's parent.
  409. // 3) deleted node has 2 children : swap between the right child and parent; and rebuild the links.
  410. // 4) deleted node is the root.
  411. if (deleted->left != NULL && deleted->right != NULL)
  412. {
  413. replaced = _binary_search_tree_next(*bst, deleted);
  414. aux = deleted->key;
  415. deleted->key = replaced->key;
  416. replaced->key = aux;
  417. *my_deleted = replaced;
  418. return __binary_search_tree_remove(bst, replaced);
  419. }
  420. *my_deleted = deleted;
  421. return __binary_search_tree_remove(bst, deleted);
  422. }
  423. _binary_search_tree_node_t _binary_search_tree_get_min(binary_search_tree_t bst)
  424. {
  425. _ASSERT(bst, ==, NULL, NULL_POINTER, NULL);
  426. // The minimum key is in the most left node of tree.
  427. while (bst->left)
  428. bst = bst->left;
  429. return bst;
  430. }
  431. _binary_search_tree_node_t _binary_search_tree_get_max(binary_search_tree_t bst)
  432. {
  433. _ASSERT(bst, ==, NULL, NULL_POINTER, NULL);
  434. // The minimum key is in the most right node of tree.
  435. while (bst->right)
  436. bst = bst->right;
  437. return bst;
  438. }
  439. _binary_search_tree_node_t _binary_search_tree_next(binary_search_tree_t bst,
  440. _binary_search_tree_node_t node)
  441. {
  442. _binary_search_tree_node_t parent;
  443. _ASSERT(bst, ==, NULL, NULL_POINTER, NULL);
  444. _ASSERT(node, ==, NULL, NULL_POINTER, NULL);
  445. _ASSERT(node->key, ==, _binary_search_tree_get_max(bst), INVALID_INPUT, NULL);
  446. /*
  447. * If the current node has a right child
  448. * then the next key is the minimum key of the right child tree.
  449. * Else move to the tree until node == parent(node)->right
  450. * and then return the parent; that's because it would have node as
  451. * left child and the next key would be in the parent.
  452. */
  453. if (node->right)
  454. return _binary_search_tree_get_min(node->right);
  455. parent = node->parent;
  456. while (parent->right == node)
  457. {
  458. node = parent;
  459. parent = parent->parent;
  460. }
  461. return parent;
  462. }
  463. _binary_search_tree_node_t _binary_search_tree_prev(binary_search_tree_t bst,
  464. _binary_search_tree_node_t node)
  465. {
  466. _binary_search_tree_node_t parent;
  467. _ASSERT(bst, ==, NULL, NULL_POINTER, NULL);
  468. _ASSERT(node, ==, node, NULL_POINTER, NULL);
  469. _ASSERT(node, ==, _binary_search_tree_get_max(bst), INVALID_INPUT, NULL);
  470. /*
  471. * If the current node has a left child
  472. * then the prev (previous) key is the maximum key of the left child tree.
  473. * Else move to the tree until node == parent(node)->left
  474. * and then return the parent; that's because it would have node as
  475. * right child and the previous key would be in the parent.
  476. */
  477. if (node->left)
  478. return _binary_search_tree_get_max(node->left);
  479. parent = node->parent;
  480. while (parent->left == node)
  481. {
  482. node = parent;
  483. parent = parent->parent;
  484. }
  485. return parent;
  486. }
  487. static binary_search_tree_t __binary_search_tree_remove(
  488. binary_search_tree_t *bst, binary_search_tree_t deleted)
  489. {
  490. int dir = 0;
  491. binary_search_tree_t parent = NULL, replaced = NULL;
  492. parent = deleted->parent;
  493. if (deleted->left)
  494. replaced = deleted->left;
  495. else
  496. replaced = deleted->right;
  497. if (parent)
  498. {
  499. if (parent->left == deleted)
  500. dir = -1;
  501. else
  502. dir = 1;
  503. }
  504. else if (replaced)
  505. {
  506. replaced->parent = NULL;
  507. *bst = replaced;
  508. }
  509. if (replaced)
  510. _binary_tree_set_parent(replaced, parent, dir);
  511. else if (parent)
  512. {
  513. if (dir == -1)
  514. parent->left = NULL;
  515. else
  516. parent->right = NULL;
  517. }
  518. return replaced;
  519. }
  520. _binary_search_tree_node_t _self_balanced_binary_search_tree_rotate_left(
  521. _binary_search_tree_node_t x)
  522. {
  523. _binary_search_tree_node_t y, px, b;
  524. _ASSERT(x, ==, NULL, NULL_POINTER, NULL);
  525. /* Y X
  526. * / \ / \
  527. * X a <= c Y
  528. * / \ / \
  529. * c b b a
  530. */
  531. y = x->right;
  532. _ASSERT(y, ==, NULL, NULL_POINTER, x);
  533. b = y->left;
  534. x->right = b;
  535. y->left = x;
  536. px = x->parent;
  537. x->parent = y;
  538. if (b)
  539. b->parent = x;
  540. y->parent = px;
  541. if (px != NULL)
  542. {
  543. if (px->left == x)
  544. px->left = y;
  545. else
  546. px->right = y;
  547. }
  548. return y;
  549. }
  550. _binary_search_tree_node_t _self_balanced_binary_search_tree_rotate_right(
  551. _binary_search_tree_node_t x)
  552. {
  553. _binary_search_tree_node_t y, px, b;
  554. _ASSERT(x, ==, NULL, NULL_POINTER, NULL);
  555. /* X Y
  556. * / \ / \
  557. * Y a => c X
  558. * / \ / \
  559. * c b b a
  560. */
  561. y = x->left;
  562. _ASSERT(y, ==, NULL, NULL_POINTER, x);
  563. b = y->right;
  564. x->left = b;
  565. y->right = x;
  566. px = x->parent;
  567. x->parent = y;
  568. if (b)
  569. b->parent = x;
  570. y->parent = px;
  571. if (px != NULL)
  572. {
  573. if (px->left == x)
  574. px->left = y;
  575. else
  576. px->right = y;
  577. }
  578. return y;
  579. }
  580. _binary_search_tree_node_t _self_balanced_binary_search_tree_rotate_left_double(
  581. _binary_search_tree_node_t x)
  582. {
  583. _ASSERT(x, ==, NULL, INVALID_INPUT, NULL);
  584. x->right = _self_balanced_binary_search_tree_rotate_right(x->right);
  585. return _self_balanced_binary_search_tree_rotate_left(x);
  586. }
  587. _binary_search_tree_node_t _self_balanced_binary_search_tree_rotate_right_double(
  588. _binary_search_tree_node_t x)
  589. {
  590. _ASSERT(x, ==, NULL, INVALID_INPUT, NULL);
  591. x->left = _self_balanced_binary_search_tree_rotate_left(x->left);
  592. return _self_balanced_binary_search_tree_rotate_right(x);
  593. }