regtree.c 14 KB

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