tsearch.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Binary tree data structure.
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published
  5. by the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  14. USA. */
  15. #ifndef _TSEARCH_H
  16. #define _TSEARCH_H
  17. #if HAVE_TSEARCH
  18. /* Get tseach(), tfind(), tdelete(), twalk() declarations. */
  19. #include <search.h>
  20. #else
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* See <http://www.opengroup.org/susv3xbd/search.h.html>,
  25. <http://www.opengroup.org/susv3xsh/tsearch.html>
  26. for details. */
  27. typedef enum
  28. {
  29. preorder,
  30. postorder,
  31. endorder,
  32. leaf
  33. }
  34. VISIT;
  35. /* Searches an element in the tree *VROOTP that compares equal to KEY.
  36. If one is found, it is returned. Otherwise, a new element equal to KEY
  37. is inserted in the tree and is returned. */
  38. extern void * tsearch (const void *key, void **vrootp,
  39. int (*compar) (const void *, const void *));
  40. /* Searches an element in the tree *VROOTP that compares equal to KEY.
  41. If one is found, it is returned. Otherwise, NULL is returned. */
  42. extern void * tfind (const void *key, void *const *vrootp,
  43. int (*compar) (const void *, const void *));
  44. /* Searches an element in the tree *VROOTP that compares equal to KEY.
  45. If one is found, it is removed from the tree, and its parent node is
  46. returned. Otherwise, NULL is returned. */
  47. extern void * tdelete (const void *key, void **vrootp,
  48. int (*compar) (const void *, const void *));
  49. /* Perform a depth-first, left-to-right traversal of the tree VROOT.
  50. The ACTION function is called:
  51. - for non-leaf nodes: 3 times, before the left subtree traversal,
  52. after the left subtree traversal but before the right subtree traversal,
  53. and after the right subtree traversal,
  54. - for leaf nodes: once.
  55. The arguments passed to ACTION are:
  56. 1. the node; it can be casted to a 'const void * const *', i.e. into a
  57. pointer to the key,
  58. 2. an indicator which visit of the node this is,
  59. 3. the level of the node in the tree (0 for the root). */
  60. extern void twalk (const void *vroot,
  61. void (*action) (const void *, VISIT, int));
  62. #ifdef __cplusplus
  63. }
  64. #endif
  65. #endif
  66. #endif /* _TSEARCH_H */