glpavl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* glpavl.c (binary search tree) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #include "glpavl.h"
  24. AVL *avl_create_tree(int (*fcmp)(void *info, const void *key1,
  25. const void *key2), void *info)
  26. { /* create AVL tree */
  27. AVL *tree;
  28. tree = xmalloc(sizeof(AVL));
  29. tree->pool = dmp_create_pool();
  30. tree->root = NULL;
  31. tree->fcmp = fcmp;
  32. tree->info = info;
  33. tree->size = 0;
  34. tree->height = 0;
  35. return tree;
  36. }
  37. int avl_strcmp(void *info, const void *key1, const void *key2)
  38. { /* compare character string keys */
  39. xassert(info == info);
  40. return strcmp(key1, key2);
  41. }
  42. static AVLNODE *rotate_subtree(AVL *tree, AVLNODE *node);
  43. AVLNODE *avl_insert_node(AVL *tree, const void *key)
  44. { /* insert new node into AVL tree */
  45. AVLNODE *p, *q, *r;
  46. short int flag;
  47. /* find an appropriate point for insertion */
  48. p = NULL; q = tree->root;
  49. while (q != NULL)
  50. { p = q;
  51. if (tree->fcmp(tree->info, key, p->key) <= 0)
  52. { flag = 0;
  53. q = p->left;
  54. p->rank++;
  55. }
  56. else
  57. { flag = 1;
  58. q = p->right;
  59. }
  60. }
  61. /* create new node and insert it into the tree */
  62. r = dmp_get_atom(tree->pool, sizeof(AVLNODE));
  63. r->key = key; r->type = 0; r->link = NULL;
  64. r->rank = 1; r->up = p;
  65. r->flag = (short int)(p == NULL ? 0 : flag);
  66. r->bal = 0; r->left = NULL; r->right = NULL;
  67. tree->size++;
  68. if (p == NULL)
  69. tree->root = r;
  70. else
  71. if (flag == 0) p->left = r; else p->right = r;
  72. /* go upstairs to the root and correct all subtrees affected by
  73. insertion */
  74. while (p != NULL)
  75. { if (flag == 0)
  76. { /* the height of the left subtree of [p] is increased */
  77. if (p->bal > 0)
  78. { p->bal = 0;
  79. break;
  80. }
  81. if (p->bal < 0)
  82. { rotate_subtree(tree, p);
  83. break;
  84. }
  85. p->bal = -1; flag = p->flag; p = p->up;
  86. }
  87. else
  88. { /* the height of the right subtree of [p] is increased */
  89. if (p->bal < 0)
  90. { p->bal = 0;
  91. break;
  92. }
  93. if (p->bal > 0)
  94. { rotate_subtree(tree, p);
  95. break;
  96. }
  97. p->bal = +1; flag = p->flag; p = p->up;
  98. }
  99. }
  100. /* if the root has been reached, the height of the entire tree is
  101. increased */
  102. if (p == NULL) tree->height++;
  103. return r;
  104. }
  105. void avl_set_node_type(AVLNODE *node, int type)
  106. { /* assign the type field of specified node */
  107. node->type = type;
  108. return;
  109. }
  110. void avl_set_node_link(AVLNODE *node, void *link)
  111. { /* assign the link field of specified node */
  112. node->link = link;
  113. return;
  114. }
  115. AVLNODE *avl_find_node(AVL *tree, const void *key)
  116. { /* find node in AVL tree */
  117. AVLNODE *p;
  118. int c;
  119. p = tree->root;
  120. while (p != NULL)
  121. { c = tree->fcmp(tree->info, key, p->key);
  122. if (c == 0) break;
  123. p = (c < 0 ? p->left : p->right);
  124. }
  125. return p;
  126. }
  127. int avl_get_node_type(AVLNODE *node)
  128. { /* retrieve the type field of specified node */
  129. return node->type;
  130. }
  131. void *avl_get_node_link(AVLNODE *node)
  132. { /* retrieve the link field of specified node */
  133. return node->link;
  134. }
  135. static AVLNODE *find_next_node(AVL *tree, AVLNODE *node)
  136. { /* find next node in AVL tree */
  137. AVLNODE *p, *q;
  138. if (tree->root == NULL) return NULL;
  139. p = node;
  140. q = (p == NULL ? tree->root : p->right);
  141. if (q == NULL)
  142. { /* go upstairs from the left subtree */
  143. for (;;)
  144. { q = p->up;
  145. if (q == NULL) break;
  146. if (p->flag == 0) break;
  147. p = q;
  148. }
  149. }
  150. else
  151. { /* go downstairs into the right subtree */
  152. for (;;)
  153. { p = q->left;
  154. if (p == NULL) break;
  155. q = p;
  156. }
  157. }
  158. return q;
  159. }
  160. void avl_delete_node(AVL *tree, AVLNODE *node)
  161. { /* delete specified node from AVL tree */
  162. AVLNODE *f, *p, *q, *r, *s, *x, *y;
  163. short int flag;
  164. p = node;
  165. /* if both subtrees of the specified node are non-empty, the node
  166. should be interchanged with the next one, at least one subtree
  167. of which is always empty */
  168. if (p->left == NULL || p->right == NULL) goto skip;
  169. f = p->up; q = p->left;
  170. r = find_next_node(tree, p); s = r->right;
  171. if (p->right == r)
  172. { if (f == NULL)
  173. tree->root = r;
  174. else
  175. if (p->flag == 0) f->left = r; else f->right = r;
  176. r->rank = p->rank; r->up = f;
  177. r->flag = p->flag; r->bal = p->bal;
  178. r->left = q; r->right = p;
  179. q->up = r;
  180. p->rank = 1; p->up = r; p->flag = 1;
  181. p->bal = (short int)(s == NULL ? 0 : +1);
  182. p->left = NULL; p->right = s;
  183. if (s != NULL) s->up = p;
  184. }
  185. else
  186. { x = p->right; y = r->up;
  187. if (f == NULL)
  188. tree->root = r;
  189. else
  190. if (p->flag == 0) f->left = r; else f->right = r;
  191. r->rank = p->rank; r->up = f;
  192. r->flag = p->flag; r->bal = p->bal;
  193. r->left = q; r->right = x;
  194. q->up = r; x->up = r; y->left = p;
  195. p->rank = 1; p->up = y; p->flag = 0;
  196. p->bal = (short int)(s == NULL ? 0 : +1);
  197. p->left = NULL; p->right = s;
  198. if (s != NULL) s->up = p;
  199. }
  200. skip: /* now the specified node [p] has at least one empty subtree;
  201. go upstairs to the root and adjust the rank field of all nodes
  202. affected by deletion */
  203. q = p; f = q->up;
  204. while (f != NULL)
  205. { if (q->flag == 0) f->rank--;
  206. q = f; f = q->up;
  207. }
  208. /* delete the specified node from the tree */
  209. f = p->up; flag = p->flag;
  210. q = p->left != NULL ? p->left : p->right;
  211. if (f == NULL)
  212. tree->root = q;
  213. else
  214. if (flag == 0) f->left = q; else f->right = q;
  215. if (q != NULL) q->up = f, q->flag = flag;
  216. tree->size--;
  217. /* go upstairs to the root and correct all subtrees affected by
  218. deletion */
  219. while (f != NULL)
  220. { if (flag == 0)
  221. { /* the height of the left subtree of [f] is decreased */
  222. if (f->bal == 0)
  223. { f->bal = +1;
  224. break;
  225. }
  226. if (f->bal < 0)
  227. f->bal = 0;
  228. else
  229. { f = rotate_subtree(tree, f);
  230. if (f->bal < 0) break;
  231. }
  232. flag = f->flag; f = f->up;
  233. }
  234. else
  235. { /* the height of the right subtree of [f] is decreased */
  236. if (f->bal == 0)
  237. { f->bal = -1;
  238. break;
  239. }
  240. if (f->bal > 0)
  241. f->bal = 0;
  242. else
  243. { f = rotate_subtree(tree, f);
  244. if (f->bal > 0) break;
  245. }
  246. flag = f->flag; f = f->up;
  247. }
  248. }
  249. /* if the root has been reached, the height of the entire tree is
  250. decreased */
  251. if (f == NULL) tree->height--;
  252. /* returns the deleted node to the memory pool */
  253. dmp_free_atom(tree->pool, p, sizeof(AVLNODE));
  254. return;
  255. }
  256. static AVLNODE *rotate_subtree(AVL *tree, AVLNODE *node)
  257. { /* restore balance of AVL subtree */
  258. AVLNODE *f, *p, *q, *r, *x, *y;
  259. xassert(node != NULL);
  260. p = node;
  261. if (p->bal < 0)
  262. { /* perform negative (left) rotation */
  263. f = p->up; q = p->left; r = q->right;
  264. if (q->bal <= 0)
  265. { /* perform single negative rotation */
  266. if (f == NULL)
  267. tree->root = q;
  268. else
  269. if (p->flag == 0) f->left = q; else f->right = q;
  270. p->rank -= q->rank;
  271. q->up = f; q->flag = p->flag; q->bal++; q->right = p;
  272. p->up = q; p->flag = 1;
  273. p->bal = (short int)(-q->bal); p->left = r;
  274. if (r != NULL) r->up = p, r->flag = 0;
  275. node = q;
  276. }
  277. else
  278. { /* perform double negative rotation */
  279. x = r->left; y = r->right;
  280. if (f == NULL)
  281. tree->root = r;
  282. else
  283. if (p->flag == 0) f->left = r; else f->right = r;
  284. p->rank -= (q->rank + r->rank);
  285. r->rank += q->rank;
  286. p->bal = (short int)(r->bal >= 0 ? 0 : +1);
  287. q->bal = (short int)(r->bal <= 0 ? 0 : -1);
  288. r->up = f; r->flag = p->flag; r->bal = 0;
  289. r->left = q; r->right = p;
  290. p->up = r; p->flag = 1; p->left = y;
  291. q->up = r; q->flag = 0; q->right = x;
  292. if (x != NULL) x->up = q, x->flag = 1;
  293. if (y != NULL) y->up = p, y->flag = 0;
  294. node = r;
  295. }
  296. }
  297. else
  298. { /* perform positive (right) rotation */
  299. f = p->up; q = p->right; r = q->left;
  300. if (q->bal >= 0)
  301. { /* perform single positive rotation */
  302. if (f == NULL)
  303. tree->root = q;
  304. else
  305. if (p->flag == 0) f->left = q; else f->right = q;
  306. q->rank += p->rank;
  307. q->up = f; q->flag = p->flag; q->bal--; q->left = p;
  308. p->up = q; p->flag = 0;
  309. p->bal = (short int)(-q->bal); p->right = r;
  310. if (r != NULL) r->up = p, r->flag = 1;
  311. node = q;
  312. }
  313. else
  314. { /* perform double positive rotation */
  315. x = r->left; y = r->right;
  316. if (f == NULL)
  317. tree->root = r;
  318. else
  319. if (p->flag == 0) f->left = r; else f->right = r;
  320. q->rank -= r->rank;
  321. r->rank += p->rank;
  322. p->bal = (short int)(r->bal <= 0 ? 0 : -1);
  323. q->bal = (short int)(r->bal >= 0 ? 0 : +1);
  324. r->up = f; r->flag = p->flag; r->bal = 0;
  325. r->left = p; r->right = q;
  326. p->up = r; p->flag = 0; p->right = x;
  327. q->up = r; q->flag = 1; q->left = y;
  328. if (x != NULL) x->up = p, x->flag = 1;
  329. if (y != NULL) y->up = q, y->flag = 0;
  330. node = r;
  331. }
  332. }
  333. return node;
  334. }
  335. void avl_delete_tree(AVL *tree)
  336. { /* delete AVL tree */
  337. dmp_delete_pool(tree->pool);
  338. xfree(tree);
  339. return;
  340. }
  341. /* eof */