avl.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * \AUTHOR: Serena Pallecchi student of Computer Science University of Pisa (Italy)
  3. * Commission from Faunalia Pontedera (PI) www.faunalia.it
  4. *
  5. * This program is free software under the GPL (>=v2)
  6. * Read the COPYING file that comes with GRASS for details.
  7. *
  8. */
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <grass/gis.h>
  14. #include <grass/glocale.h>
  15. #include "defs.h"
  16. #include "avlDefs.h"
  17. #include "avl.h"
  18. static avl_node *avl_individua(const avl_tree root, const generic_cell k,
  19. avl_node ** father, int *direction);
  20. static int avl_height(const avl_tree root);
  21. static avl_node *critical_node(avl_node * added, int *pos1, int *pos2,
  22. const avl_node * prec);
  23. void avl_rotation_ll(avl_node * critical);
  24. void avl_rotation_lr(avl_node * critical);
  25. void avl_rotation_rl(avl_node * critical);
  26. void avl_rotation_rr(avl_node * critical);
  27. void printAVL(avl_node * r);
  28. /* define function declsred in avl.h */
  29. avl_tree avl_make(const generic_cell k, const long n)
  30. {
  31. avl_node *root = NULL; /* tree root pointer */
  32. /* create root */
  33. root = G_malloc(sizeof(avl_node));
  34. if (root == NULL) {
  35. G_fatal_error("avl.c: avl_make: malloc error");
  36. return NULL;
  37. }
  38. /* inizialize root */
  39. root->right_child = NULL;
  40. root->left_child = NULL;
  41. root->father = NULL;
  42. root->counter = n;
  43. root->key = k;
  44. return root;
  45. }
  46. long howManyCell(const avl_tree root, const generic_cell k)
  47. {
  48. avl_node *nodo = NULL;
  49. nodo = avl_find(root, k);
  50. if (nodo == NULL)
  51. return 0;
  52. else
  53. return nodo->counter;
  54. }
  55. avl_node *avl_find(const avl_tree root, const generic_cell k)
  56. {
  57. avl_node *p = NULL;
  58. int d = 0;
  59. if (root == NULL)
  60. return NULL;
  61. return avl_individua(root, k, &p, &d);
  62. }
  63. int avl_add(avl_tree * root, const generic_cell k, const long n)
  64. {
  65. avl_node *p = NULL;
  66. avl_node *node_temp = NULL;
  67. avl_node *critical = NULL;
  68. int d = 0;
  69. int pos1 = 0, pos2 = 0;
  70. int rotation = 0;
  71. if ((root == NULL) || (*root == NULL)) {
  72. G_fatal_error("\navl.c: avl_add: param NULL");
  73. return AVL_ERR;
  74. }
  75. /* search position where insert the new node */
  76. node_temp = avl_individua(*root, k, &p, &d);
  77. if (node_temp != NULL) {
  78. node_temp->counter = node_temp->counter + n;
  79. return AVL_PRES;
  80. }
  81. node_temp = avl_make(k, n);
  82. if (node_temp == NULL) {
  83. G_fatal_error("\navl.c: avl_add: create node error");
  84. return AVL_ERR;
  85. }
  86. /* link the new node */
  87. node_temp->father = p;
  88. if (d == -1) {
  89. p->left_child = node_temp;
  90. }
  91. else {
  92. if (d == 1) {
  93. p->right_child = node_temp;
  94. }
  95. else {
  96. G_free(node_temp);
  97. G_fatal_error("avl.c: avl_add: new node position unknown");
  98. return AVL_ERR;
  99. }
  100. }
  101. /* if it's necessary balance the tree */
  102. critical = critical_node(node_temp, &pos1, &pos2, NULL);
  103. if (critical == NULL)
  104. return AVL_ADD;
  105. rotation = (pos1 * 10) + pos2;
  106. switch (rotation) {
  107. case AVL_SS:
  108. avl_rotation_ll(critical);
  109. break;
  110. case AVL_SD:
  111. avl_rotation_lr(critical);
  112. break;
  113. case AVL_DS:
  114. avl_rotation_rl(critical);
  115. break;
  116. case AVL_DD:
  117. avl_rotation_rr(critical);
  118. break;
  119. default:
  120. G_fatal_error("avl, avl_add: balancing error\n");
  121. return AVL_ERR;
  122. }
  123. /* if after rotation the root is changed modufy the pointer to the root */
  124. while ((*root)->father != NULL)
  125. *root = (*root)->father;
  126. return AVL_ADD;
  127. }
  128. long avl_to_array(avl_node * root, long i, AVL_table * a)
  129. {
  130. if (root != NULL) {
  131. i = avl_to_array(root->left_child, i, a);
  132. if (a == NULL)
  133. G_fatal_error("avl, avl_to_array: null value");
  134. else {
  135. a[i] = G_malloc(sizeof(AVL_tableRow));
  136. a[i]->k = root->key;
  137. a[i]->tot = root->counter;
  138. i++;
  139. i = avl_to_array(root->right_child, i, a);
  140. }
  141. }
  142. return i;
  143. }
  144. static avl_node *avl_individua(const avl_tree root, const generic_cell k,
  145. avl_node ** father, int *direction)
  146. {
  147. int ris = 0;
  148. if (root == NULL) {
  149. return NULL;
  150. }
  151. ris = equalsGenericCell(root->key, k);
  152. switch (ris) {
  153. case GC_EQUAL:
  154. {
  155. return root;
  156. break;
  157. }
  158. case GC_HIGHER:
  159. {
  160. *father = root;
  161. *direction = -1;
  162. return avl_individua(root->left_child, k, father, direction);
  163. }
  164. case GC_LOWER:
  165. {
  166. *father = root;
  167. *direction = 1;
  168. return avl_individua(root->right_child, k, father, direction);
  169. }
  170. case GC_DIFFERENT_TYPE:
  171. {
  172. G_fatal_error("\avl.c: avl_individua: different type");
  173. return NULL;
  174. }
  175. default:
  176. {
  177. G_fatal_error("\avl.c: avl_individua: error");
  178. return NULL;
  179. }
  180. }
  181. }
  182. static int avl_height(const avl_tree root)
  183. {
  184. if (root == NULL)
  185. return -1;
  186. else {
  187. int tmp1 = avl_height(root->left_child);
  188. int tmp2 = avl_height(root->right_child);
  189. return (1 + ((tmp1 > tmp2) ? tmp1 : tmp2));
  190. }
  191. }
  192. static avl_node *critical_node(avl_node * added, int *pos1, int *pos2,
  193. const avl_node * prec)
  194. {
  195. int fdb = 0;
  196. if (added == NULL)
  197. return NULL;
  198. if (prec == NULL)
  199. *pos1 = *pos2 = 0;
  200. else {
  201. *pos2 = *pos1;
  202. if (prec == added->left_child)
  203. *pos1 = AVL_S;
  204. else
  205. *pos1 = AVL_D; /* prec == added->right_child */
  206. }
  207. fdb = abs(avl_height(added->left_child) - avl_height(added->right_child));
  208. if (fdb > 1)
  209. return added;
  210. else {
  211. prec = added;
  212. return critical_node(added->father, pos1, pos2, prec);
  213. }
  214. }
  215. void avl_rotation_ll(avl_node * critical)
  216. {
  217. avl_node *b = NULL;
  218. avl_node *r = critical;
  219. avl_node *s = critical->left_child;
  220. s->father = r->father;
  221. if (r->father != NULL) {
  222. if ((r->father)->left_child == r)
  223. (r->father)->left_child = s;
  224. else
  225. (r->father)->right_child = s;
  226. }
  227. b = s->right_child;
  228. s->right_child = r;
  229. r->father = s;
  230. r->left_child = b;
  231. if (b != NULL)
  232. b->father = r;
  233. }
  234. void avl_rotation_rr(avl_node * critical)
  235. {
  236. avl_node *b = NULL;
  237. avl_node *r = critical;
  238. avl_node *s = critical->right_child;
  239. s->father = r->father;
  240. if (r->father != NULL) {
  241. if ((r->father)->left_child == r)
  242. (r->father)->left_child = s;
  243. else
  244. (r->father)->right_child = s;
  245. }
  246. b = s->left_child;
  247. s->left_child = r;
  248. r->father = s;
  249. r->right_child = b;
  250. if (b != NULL)
  251. b->father = r;
  252. }
  253. void avl_rotation_lr(avl_node * critical)
  254. {
  255. avl_node *b = NULL;
  256. avl_node *g = NULL;
  257. avl_node *r = critical;
  258. avl_node *s = critical->left_child;
  259. avl_node *t = (critical->left_child)->right_child;
  260. t->father = r->father;
  261. if (r->father != NULL) {
  262. if ((r->father)->left_child == r)
  263. (r->father)->left_child = t;
  264. else
  265. (r->father)->right_child = t;
  266. }
  267. b = t->left_child;
  268. g = t->right_child;
  269. t->left_child = s;
  270. t->right_child = r;
  271. r->father = t;
  272. s->father = t;
  273. s->right_child = b;
  274. r->left_child = g;
  275. if (b != NULL)
  276. b->father = s;
  277. if (g != NULL)
  278. g->father = r;
  279. }
  280. void avl_rotation_rl(avl_node * critical)
  281. {
  282. avl_node *b = NULL;
  283. avl_node *g = NULL;
  284. avl_node *r = critical;
  285. avl_node *s = critical->right_child;
  286. avl_node *t = (critical->right_child)->left_child;
  287. t->father = r->father;
  288. if (r->father != NULL) {
  289. if ((r->father)->left_child == r)
  290. (r->father)->left_child = t;
  291. else
  292. (r->father)->right_child = t;
  293. }
  294. b = t->left_child;
  295. g = t->right_child;
  296. t->left_child = r;
  297. t->right_child = s;
  298. r->father = t;
  299. s->father = t;
  300. r->right_child = b;
  301. s->left_child = g;
  302. if (b != NULL)
  303. b->father = r;
  304. if (g != NULL)
  305. g->father = s;
  306. }