btree2.dox 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*! \page btree2 btree2 library
  2. \tableofcontents
  3. Red-Black tree
  4. ==============
  5. Include and linking
  6. -------------------
  7. To make use of the binary balanced (Red-Black) search tree include:
  8. #include <grass/rbtree.h>
  9. and link to `BTREE2LIB` in a Makefile.
  10. \note
  11. Duplicates are not supported.
  12. Example
  13. -------
  14. Define custom compare function:
  15. int my_compare_fn(const void *a, const void *b)
  16. {
  17. if ((mydatastruct *) a < (mydatastruct *) b)
  18. return -1;
  19. else if ((mydatastruct *) a > (mydatastruct *) b)
  20. return 1;
  21. else if ((mydatastruct *) a == (mydatastruct *) b)
  22. return 0;
  23. }
  24. Create and initialize tree:
  25. struct RB_TREE *mytree = rbtree_create(my_compare_fn, item_size);
  26. Insert items to tree:
  27. struct mydatastruct data = <some data>;
  28. if (rbtree_insert(mytree, &data) == 0)
  29. G_warning("could not insert data");
  30. Find item in tree:
  31. struct mydatastruct data = <some data>;
  32. if (rbtree_find(mytree, &data) == 0)
  33. G_message("data not found");
  34. Delete item from tree:
  35. struct mydatastruct data = <some data>;
  36. if (rbtree_remove(mytree, &data) == 0)
  37. G_warning("could not find data in tree");
  38. Traverse tree (get all items in tree in ascending order):
  39. struct RB_TRAV trav;
  40. rbtree_init_trav(&trav, tree);
  41. while ((data = rbtree_traverse(&trav)) != NULL) {
  42. if (my_compare_fn(data, threshold_data) == 0) break;
  43. // do something with data (using C++ comments because of Doxygen)
  44. }
  45. Get a selection of items: all data > data1 and < data2.
  46. Start in tree where data is last smaller or first larger compared to data1:
  47. struct RB_TRAV trav;
  48. rbtree_init_trav(&trav, tree);
  49. data = rbtree_traverse_start(&trav, &data1);
  50. // do something with data
  51. while ((data = rbtree_traverse(&trav)) != NULL) {
  52. if (data > data2) break;
  53. // do something with data
  54. }
  55. Destroy tree:
  56. rbtree_destroy(mytree);
  57. Debug the whole tree with:
  58. rbtree_debug(mytree, mytree->root);
  59. See also \ref rbtree.h for more instructions on how to use it.
  60. k-d tree
  61. ========
  62. Description
  63. -----------
  64. k-d tree is a multidimensional (k-dimensional) binary search tree for
  65. nearest neighbor search.
  66. This k-d tree finds the exact nearest neighbor(s), not some
  67. approximation. It supports up to 255 dimensions. It is dynamic, i.e.
  68. points can be inserted and removed at any time. It is balanced to
  69. improve search performance. It provides k nearest neighbor search
  70. (find k neighbors to a given coordinate) as well as radius or distance
  71. search (find all neighbors within radius, i.e. not farther away than
  72. radius to a given coordinate).
  73. Include and linking
  74. -------------------
  75. Include:
  76. #include <grass/kdtree.h>
  77. and link to `BTREE2LIB` in a Makefile.
  78. Example
  79. -------
  80. Create a new k-d tree (here 3D):
  81. struct kdtree *t = kdtree_create(3, NULL);
  82. Insert items:
  83. for (i = 0; i < npoints; i++)
  84. kdtree_insert(t, c, i, 1);
  85. Find nearest neighbor for each point:
  86. for (i = 0; i < npoints; i++)
  87. int found = kdtree_knn(t, c, &uid, &dist, 1, i);
  88. Destroy the tree:
  89. kdtree_destroy(t);
  90. Example usages
  91. --------------
  92. - Nearest neighbor statistics: test if points are randomly
  93. distributed. For example, an older version of GRASS addon `v.nnstat`
  94. used an external k-d tree from PCL (which in turn uses flann)
  95. which finds the approximate, not the exact nearest neighbor.
  96. The GRASS-native k-d tree always finds the real nearest neighbor.
  97. - Spatial cluster analysis: a point cloud can be partitioned into
  98. separate clusters where points within each cluster are closer to each
  99. other than to points of another cluster. For example, as used in
  100. \gmod{v.cluster}.
  101. - %Point cloud thinning: a sample can be generated from a large point
  102. cloud by specifying a minimum distance between sample points.
  103. - This k-d tree is used by \gmod{v.clean} `tool=snap` (Vect_snap_lines()),
  104. reducing both memory consumption and processing time.
  105. See also
  106. ========
  107. - \ref rbtree.h
  108. - \ref kdtree.h
  109. - \ref rtree.h
  110. - \ref btree.h
  111. - [Wikipedia article on Red-black_tree](https://en.wikipedia.org/wiki/Red-black_tree)
  112. - [Wikipedia article on k-d tree](https://en.wikipedia.org/wiki/K-d_tree)
  113. */