regtree.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*!
  2. * \file rbtree.c
  3. *
  4. * \brief binary search tree
  5. *
  6. * Generic balanced binary search tree (Red Black Tree) implementation
  7. *
  8. * (C) 2009 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public License
  11. * (>=v2). Read the file COPYING that comes with GRASS for details.
  12. *
  13. * \author Original author Julienne Walker 2003, 2008
  14. * GRASS implementation Markus Metz, 2009
  15. */
  16. /* balanced binary search tree implementation
  17. *
  18. * this one is a Red Black Tree, no parent pointers, no threads
  19. * The core code comes from Julienne Walker's tutorials on binary search trees
  20. * original license: public domain
  21. * http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_rbtree.aspx
  22. * some ideas come from libavl (GPL >= 2)
  23. *
  24. * Red Black Trees are used to maintain a data structure with
  25. * search, insertion and deletion in O(log N) time
  26. */
  27. #include <assert.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <grass/gis.h>
  31. #include <grass/glocale.h>
  32. #include "regtree.h"
  33. /* internal functions */
  34. static struct RG_NODE *rgtree_single(struct RG_NODE *, int);
  35. static struct RG_NODE *rgtree_double(struct RG_NODE *, int);
  36. static struct reg_stats *rgtree_first(struct RG_TRAV *);
  37. static struct reg_stats *rgtree_next(struct RG_TRAV *);
  38. static struct RG_NODE *rgtree_make_node(size_t, struct reg_stats *);
  39. static int is_red(struct RG_NODE *);
  40. int compare_regstat(struct reg_stats *a, struct reg_stats *b)
  41. {
  42. return (a->id - b->id);
  43. }
  44. /* create new tree and initialize
  45. * returns pointer to new tree, NULL for memory allocation error
  46. */
  47. struct RG_TREE *rgtree_create(int nbands, size_t rb_datasize)
  48. {
  49. struct RG_TREE *tree = (struct RG_TREE *)malloc(sizeof(struct RG_TREE));
  50. if (tree == NULL) {
  51. G_warning("RB tree: Out of memory!");
  52. return NULL;
  53. }
  54. tree->datasize = rb_datasize;
  55. tree->cmp = compare_regstat;
  56. tree->count = 0;
  57. tree->nbands = nbands;
  58. tree->root = NULL;
  59. return tree;
  60. }
  61. /* add an item to a tree
  62. * non-recursive top-down insertion
  63. * the algorithm does not allow duplicates and also does not warn about a duplicate
  64. * returns 1 on success, 0 on failure
  65. */
  66. int rgtree_insert(struct RG_TREE *tree, struct reg_stats *data)
  67. {
  68. assert(tree && data);
  69. if (tree->root == NULL) {
  70. /* create a new root node for tree */
  71. tree->root = rgtree_make_node(tree->datasize, data);
  72. if (tree->root == NULL)
  73. return 0;
  74. }
  75. else {
  76. struct RG_NODE head = { 0, {0, 0}, {0, 0, 0, 0} }; /* False tree root */
  77. struct RG_NODE *g, *t; /* Grandparent & parent */
  78. struct RG_NODE *p, *q; /* Iterator & parent */
  79. int dir = 0, last = 0;
  80. /* Set up helpers */
  81. t = &head;
  82. g = p = NULL;
  83. q = t->link[1] = tree->root;
  84. /* Search down the tree */
  85. for (;;) {
  86. if (q == NULL) {
  87. /* Insert new node at the bottom */
  88. p->link[dir] = q = rgtree_make_node(tree->datasize, data);
  89. if (q == NULL)
  90. return 0;
  91. }
  92. else if (is_red(q->link[0]) && is_red(q->link[1])) {
  93. /* Color flip */
  94. q->red = 1;
  95. q->link[0]->red = 0;
  96. q->link[1]->red = 0;
  97. }
  98. /* Fix red violation */
  99. if (is_red(q) && is_red(p)) {
  100. int dir2 = t->link[1] == g;
  101. if (q == p->link[last])
  102. t->link[dir2] = rgtree_single(g, !last);
  103. else
  104. t->link[dir2] = rgtree_double(g, !last);
  105. }
  106. last = dir;
  107. dir = tree->cmp(&(q->data), data);
  108. /* Stop if found. This check also disallows duplicates in the tree */
  109. if (dir == 0)
  110. break;
  111. dir = dir < 0;
  112. /* Move the helpers down */
  113. if (g != NULL)
  114. t = g;
  115. g = p, p = q;
  116. q = q->link[dir];
  117. }
  118. /* Update root */
  119. tree->root = head.link[1];
  120. }
  121. /* Make root black */
  122. tree->root->red = 0;
  123. tree->count++;
  124. return 1;
  125. }
  126. /* remove an item from a tree that matches given data
  127. * non-recursive top-down removal
  128. * returns 1 on successful removal
  129. * returns 0 if data item was not found
  130. */
  131. int rgtree_remove(struct RG_TREE *tree, struct reg_stats *data)
  132. {
  133. struct RG_NODE head = { 0, {0, 0}, {0, 0, 0, 0} }; /* False tree root */
  134. struct RG_NODE *q, *p, *g; /* Helpers */
  135. struct RG_NODE *f = NULL; /* Found item */
  136. int dir = 1, removed = 0;
  137. assert(tree && data);
  138. if (tree->root == NULL) {
  139. return 0; /* empty tree, nothing to remove */
  140. }
  141. /* Set up helpers */
  142. q = &head;
  143. g = p = NULL;
  144. q->link[1] = tree->root;
  145. /* Search and push a red down */
  146. while (q->link[dir] != NULL) {
  147. int last = dir;
  148. /* Update helpers */
  149. g = p, p = q;
  150. q = q->link[dir];
  151. dir = tree->cmp(&(q->data), data);
  152. /* Save found node */
  153. if (dir == 0)
  154. f = q;
  155. dir = dir < 0;
  156. /* Push the red node down */
  157. if (!is_red(q) && !is_red(q->link[dir])) {
  158. if (is_red(q->link[!dir]))
  159. p = p->link[last] = rgtree_single(q, dir);
  160. else if (!is_red(q->link[!dir])) {
  161. struct RG_NODE *s = p->link[!last];
  162. if (s != NULL) {
  163. if (!is_red(s->link[!last]) && !is_red(s->link[last])) {
  164. /* Color flip */
  165. p->red = 0;
  166. s->red = 1;
  167. q->red = 1;
  168. }
  169. else {
  170. int dir2 = g->link[1] == p;
  171. if (is_red(s->link[last]))
  172. g->link[dir2] = rgtree_double(p, last);
  173. else if (is_red(s->link[!last]))
  174. g->link[dir2] = rgtree_single(p, last);
  175. /* Ensure correct coloring */
  176. q->red = g->link[dir2]->red = 1;
  177. g->link[dir2]->link[0]->red = 0;
  178. g->link[dir2]->link[1]->red = 0;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. /* Replace and remove if found */
  185. if (f != NULL) {
  186. if (f != q) {
  187. f->data.id = q->data.id;
  188. f->data.count = q->data.count;
  189. memcpy(f->data.sum, q->data.sum, tree->datasize);
  190. memcpy(f->data.mean, q->data.mean, tree->datasize);
  191. /* unused:
  192. memcpy(f->data.min, q->data.min, tree->datasize);
  193. memcpy(f->data.max, q->data.max, tree->datasize);
  194. */
  195. }
  196. p->link[p->link[1] == q] = q->link[q->link[0] == NULL];
  197. free(q->data.sum);
  198. free(q->data.mean);
  199. /* unused:
  200. free(q->data.min);
  201. free(q->data.max);
  202. */
  203. free(q);
  204. q = NULL;
  205. tree->count--;
  206. removed = 1;
  207. }
  208. else
  209. G_debug(2, "RB tree: data not found in search tree");
  210. /* Update root and make it black */
  211. tree->root = head.link[1];
  212. if (tree->root != NULL)
  213. tree->root->red = 0;
  214. return removed;
  215. }
  216. /* find data item in tree
  217. * returns pointer to data item if found else NULL
  218. */
  219. struct reg_stats *rgtree_find(struct RG_TREE *tree, struct reg_stats *data)
  220. {
  221. struct RG_NODE *curr_node = tree->root;
  222. int cmp;
  223. assert(tree && data);
  224. while (curr_node != NULL) {
  225. cmp = tree->cmp(&(curr_node->data), data);
  226. if (cmp == 0)
  227. return &curr_node->data; /* found */
  228. curr_node = curr_node->link[cmp < 0];
  229. }
  230. return NULL;
  231. }
  232. /* initialize tree traversal
  233. * (re-)sets trav structure
  234. * returns 0
  235. */
  236. int rgtree_init_trav(struct RG_TRAV *trav, struct RG_TREE *tree)
  237. {
  238. assert(trav && tree);
  239. trav->tree = tree;
  240. trav->curr_node = tree->root;
  241. trav->first = 1;
  242. trav->top = 0;
  243. return 0;
  244. }
  245. /* traverse the tree in ascending order
  246. * useful to get all items in the tree non-recursively
  247. * struct RG_TRAV *trav needs to be initialized first
  248. * returns pointer to data, NULL when finished
  249. */
  250. struct reg_stats *rgtree_traverse(struct RG_TRAV *trav)
  251. {
  252. assert(trav);
  253. if (trav->curr_node == NULL) {
  254. if (trav->first)
  255. G_debug(1, "RB tree: empty tree");
  256. else
  257. G_debug(1, "RB tree: finished traversing");
  258. return NULL;
  259. }
  260. if (!trav->first)
  261. return rgtree_next(trav);
  262. else {
  263. trav->first = 0;
  264. return rgtree_first(trav);
  265. }
  266. }
  267. /* find start point to traverse the tree in ascending order
  268. * useful to get a selection of items in the tree
  269. * magnitudes faster than traversing the whole tree
  270. * may return first item that's smaller or first item that's larger
  271. * struct RG_TRAV *trav needs to be initialized first
  272. * returns pointer to data, NULL when finished
  273. */
  274. struct reg_stats *rgtree_traverse_start(struct RG_TRAV *trav, struct reg_stats *data)
  275. {
  276. int dir = 0;
  277. assert(trav && data);
  278. if (trav->curr_node == NULL) {
  279. if (trav->first)
  280. G_warning("RB tree: empty tree");
  281. else
  282. G_warning("RB tree: finished traversing");
  283. return NULL;
  284. }
  285. if (!trav->first)
  286. return rgtree_next(trav);
  287. /* else first time, get start node */
  288. trav->first = 0;
  289. trav->top = 0;
  290. while (trav->curr_node != NULL) {
  291. dir = trav->tree->cmp(&(trav->curr_node->data), data);
  292. /* exact match, great! */
  293. if (dir == 0)
  294. return &(trav->curr_node->data);
  295. else {
  296. dir = dir < 0;
  297. /* end of branch, also reached if
  298. * smallest item is larger than search template or
  299. * largest item is smaller than search template */
  300. if (trav->curr_node->link[dir] == NULL)
  301. return &(trav->curr_node->data);
  302. trav->up[trav->top++] = trav->curr_node;
  303. trav->curr_node = trav->curr_node->link[dir];
  304. }
  305. }
  306. return NULL; /* should not happen */
  307. }
  308. /* two functions needed to fully traverse the tree: initialize and continue
  309. * useful to get all items in the tree non-recursively
  310. * this one here uses a stack
  311. * parent pointers or threads would also be possible
  312. * but these would need to be added to RG_NODE
  313. * -> more memory needed for standard operations
  314. */
  315. /* start traversing the tree
  316. * returns pointer to smallest data item
  317. */
  318. static struct reg_stats *rgtree_first(struct RG_TRAV *trav)
  319. {
  320. /* get smallest item */
  321. while (trav->curr_node->link[0] != NULL) {
  322. trav->up[trav->top++] = trav->curr_node;
  323. trav->curr_node = trav->curr_node->link[0];
  324. }
  325. return &(trav->curr_node->data); /* return smallest item */
  326. }
  327. /* continue traversing the tree in ascending order
  328. * returns pointer to data item, NULL when finished
  329. */
  330. static struct reg_stats *rgtree_next(struct RG_TRAV *trav)
  331. {
  332. if (trav->curr_node->link[1] != NULL) {
  333. /* something on the right side: larger item */
  334. trav->up[trav->top++] = trav->curr_node;
  335. trav->curr_node = trav->curr_node->link[1];
  336. /* go down, find smallest item in this branch */
  337. while (trav->curr_node->link[0] != NULL) {
  338. trav->up[trav->top++] = trav->curr_node;
  339. trav->curr_node = trav->curr_node->link[0];
  340. }
  341. }
  342. else {
  343. /* at smallest item in this branch, go back up */
  344. struct RG_NODE *last;
  345. do {
  346. if (trav->top == 0) {
  347. trav->curr_node = NULL;
  348. break;
  349. }
  350. last = trav->curr_node;
  351. trav->curr_node = trav->up[--trav->top];
  352. } while (last == trav->curr_node->link[1]);
  353. }
  354. if (trav->curr_node != NULL) {
  355. return &(trav->curr_node->data);
  356. }
  357. else
  358. return NULL; /* finished traversing */
  359. }
  360. /* destroy the tree */
  361. void rgtree_destroy(struct RG_TREE *tree)
  362. {
  363. struct RG_NODE *it;
  364. struct RG_NODE *save = tree->root;
  365. /*
  366. Rotate away the left links so that
  367. we can treat this like the destruction
  368. of a linked list
  369. */
  370. while((it = save) != NULL) {
  371. if (it->link[0] == NULL) {
  372. /* No left links, just kill the node and move on */
  373. save = it->link[1];
  374. free(it->data.sum);
  375. free(it->data.mean);
  376. free(it);
  377. it = NULL;
  378. }
  379. else {
  380. /* Rotate away the left link and check again */
  381. save = it->link[0];
  382. it->link[0] = save->link[1];
  383. save->link[1] = it;
  384. }
  385. }
  386. free(tree);
  387. tree = NULL;
  388. return;
  389. }
  390. /* used for debugging: check for errors in tree structure */
  391. int rgtree_debug(struct RG_TREE *tree, struct RG_NODE *root)
  392. {
  393. int lh, rh;
  394. if (root == NULL)
  395. return 1;
  396. else {
  397. struct RG_NODE *ln = root->link[0];
  398. struct RG_NODE *rn = root->link[1];
  399. int lcmp = 0, rcmp = 0;
  400. /* Consecutive red links */
  401. if (is_red(root)) {
  402. if (is_red(ln) || is_red(rn)) {
  403. G_warning("Red Black Tree debugging: Red violation");
  404. return 0;
  405. }
  406. }
  407. lh = rgtree_debug(tree, ln);
  408. rh = rgtree_debug(tree, rn);
  409. if (ln) {
  410. lcmp = tree->cmp(&(ln->data), &(root->data));
  411. }
  412. if (rn) {
  413. rcmp = tree->cmp(&(rn->data), &(root->data));
  414. }
  415. /* Invalid binary search tree:
  416. * left node >= parent or right node <= parent */
  417. if ((ln != NULL && lcmp > -1)
  418. || (rn != NULL && rcmp < 1)) {
  419. G_warning("Red Black Tree debugging: Binary tree violation");
  420. return 0;
  421. }
  422. /* Black height mismatch */
  423. if (lh != 0 && rh != 0 && lh != rh) {
  424. G_warning("Red Black Tree debugging: Black violation");
  425. return 0;
  426. }
  427. /* Only count black links */
  428. if (lh != 0 && rh != 0)
  429. return is_red(root) ? lh : lh + 1;
  430. else
  431. return 0;
  432. }
  433. }
  434. /*******************************************************
  435. * *
  436. * internal functions for Red Black Tree maintenance *
  437. * *
  438. *******************************************************/
  439. /* add a new node to the tree */
  440. static struct RG_NODE *rgtree_make_node(size_t datasize, struct reg_stats *data)
  441. {
  442. struct RG_NODE *new_node = (struct RG_NODE *)malloc(sizeof(*new_node));
  443. if (new_node == NULL)
  444. G_fatal_error("RB Search Tree: Out of memory!");
  445. if ((new_node->data.sum = malloc(datasize)) == NULL)
  446. G_fatal_error("RB Search Tree: Out of memory!");
  447. if ((new_node->data.mean = malloc(datasize)) == NULL)
  448. G_fatal_error("RB Search Tree: Out of memory!");
  449. /* unused:
  450. if ((new_node->data.min = malloc(datasize)) == NULL)
  451. G_fatal_error("RB Search Tree: Out of memory!");
  452. if ((new_node->data.max = malloc(datasize)) == NULL)
  453. G_fatal_error("RB Search Tree: Out of memory!");
  454. */
  455. new_node->data.id = data->id;
  456. new_node->data.count = data->count;
  457. memcpy(new_node->data.sum, data->sum, datasize);
  458. memcpy(new_node->data.mean, data->mean, datasize);
  459. /* unused
  460. memcpy(new_node->data.min, data->min, datasize);
  461. memcpy(new_node->data.max, data->max, datasize);
  462. */
  463. new_node->red = 1; /* 1 is red, 0 is black */
  464. new_node->link[0] = NULL;
  465. new_node->link[1] = NULL;
  466. return new_node;
  467. }
  468. /* check for red violation */
  469. static int is_red(struct RG_NODE *root)
  470. {
  471. if (root)
  472. return root->red == 1;
  473. return 0;
  474. }
  475. /* single rotation */
  476. static struct RG_NODE *rgtree_single(struct RG_NODE *root, int dir)
  477. {
  478. struct RG_NODE *newroot = root->link[!dir];
  479. root->link[!dir] = newroot->link[dir];
  480. newroot->link[dir] = root;
  481. root->red = 1;
  482. newroot->red = 0;
  483. return newroot;
  484. }
  485. /* double rotation */
  486. static struct RG_NODE *rgtree_double(struct RG_NODE *root, int dir)
  487. {
  488. root->link[!dir] = rgtree_single(root->link[!dir], !dir);
  489. return rgtree_single(root, dir);
  490. }