avl.h
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:4k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. /* libavl - manipulates AVL trees.
  2.    Copyright (C) 1998, 1999 Free Software Foundation, Inc.
  3.    This program is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU General Public License as
  5.    published by the Free Software Foundation; either version 2 of the
  6.    License, or (at your option) any later version.
  7.    This program is distributed in the hope that it will be useful, but
  8.    WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  10.    General Public License for more details.
  11.    You should have received a copy of the GNU General Public License
  12.    along with this program; if not, write to the Free Software
  13.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  14.    02111-1307, USA.
  15.    The author may be contacted at <pfaffben@pilot.msu.edu> on the
  16.    Internet, or as Ben Pfaff, 12167 Airport Rd, DeWitt MI 48820, USA
  17.    through more mundane means. */
  18. /* This is file avl.h in libavl. */
  19. #if !avl_h
  20. #define avl_h 1
  21. /* The default maximum height of 32 allows for AVL trees having
  22.    between 5,704,880 and 4,294,967,295 nodes, depending on order of
  23.    insertion.  You may change this compile-time constant as you
  24.    wish. */
  25. #ifndef AVL_MAX_HEIGHT
  26. #define AVL_MAX_HEIGHT 32
  27. #endif
  28. /* Structure for a node in an AVL tree. */
  29. typedef struct avl_node
  30.   {
  31.     void *data; /* Pointer to data. */
  32.     struct avl_node *link[2]; /* Subtrees. */
  33.     signed char bal; /* Balance factor. */
  34.     char cache; /* Used during insertion. */
  35.     signed char pad[2]; /* Unused.  Reserved for threaded trees. */
  36.   }
  37. avl_node;
  38. /* Used for traversing an AVL tree. */
  39. typedef struct avl_traverser
  40.   {
  41.     int init; /* Initialized? */
  42.     int nstack; /* Top of stack. */
  43.     const avl_node *p; /* Used for traversal. */
  44.     const avl_node *stack[AVL_MAX_HEIGHT];/* Descended trees. */
  45.   }
  46. avl_traverser;
  47. /* Initializer for avl_traverser. */
  48. #define AVL_TRAVERSER_INIT {0}
  49. /* Function types. */
  50. #if !AVL_FUNC_TYPES
  51. #define AVL_FUNC_TYPES 1
  52. typedef int (*avl_comparison_func) (const void *a, const void *b, void *param);
  53. typedef void (*avl_node_func) (void *data, void *param);
  54. typedef void *(*avl_copy_func) (void *data, void *param);
  55. #endif
  56. /* Structure which holds information about an AVL tree. */
  57. typedef struct avl_tree
  58.   {
  59. #if PSPP
  60.     struct arena **owner; /* Arena to store nodes. */
  61. #endif
  62.     avl_node root; /* Tree root node. */
  63.     avl_comparison_func cmp; /* Used to compare keys. */
  64.     int count; /* Number of nodes in the tree. */
  65.     void *param; /* Arbitary user data. */
  66.   }
  67. avl_tree;
  68. #if PSPP
  69. #define MAYBE_ARENA struct arena **owner,
  70. #else
  71. #define MAYBE_ARENA /* nothing */
  72. #endif
  73. /* General functions. */
  74. avl_tree *avl_create (MAYBE_ARENA avl_comparison_func, void *param);
  75. void avl_destroy (avl_tree *, avl_node_func);
  76. void avl_free (avl_tree *);
  77. int avl_count (const avl_tree *);
  78. avl_tree *avl_copy (MAYBE_ARENA const avl_tree *, avl_copy_func);
  79. /* Walk the tree. */
  80. void avl_walk (const avl_tree *, avl_node_func, void *param);
  81. void *avl_traverse (const avl_tree *, avl_traverser *);
  82. #define avl_init_traverser(TRAVERSER) ((TRAVERSER)->init = 0)
  83. /* Search for a given item. */
  84. void **avl_probe (avl_tree *, void *);
  85. void *avl_delete (avl_tree *, const void *);
  86. void *avl_find (const avl_tree *, const void *);
  87. void *avl_find_close (const avl_tree *, const void *);
  88. #if __GCC__ >= 2
  89. extern inline void *
  90. avl_insert (avl_tree *tree, void *item)
  91. {
  92.   void **p = avl_probe (tree, item);
  93.   return (*p == item) ? NULL : *p;
  94. }
  95. extern inline void *
  96. avl_replace (avl_tree *tree, void *item)
  97. {
  98.   void **p = avl_probe (tree, item);
  99.   if (*p == item)
  100.     return NULL;
  101.   else
  102.     {
  103.       void *r = *p;
  104.       *p = item;
  105.       return r;
  106.     }
  107. }
  108. #else /* not gcc */
  109. void *avl_insert (avl_tree *tree, void *item);
  110. void *avl_replace (avl_tree *tree, void *item);
  111. #endif /* not gcc */
  112. /* Easy assertions on insertion & deletion. */
  113. #ifndef NDEBUG
  114. #define avl_force_insert(A, B)
  115. do
  116.   {
  117.             void *r = avl_insert (A, B);
  118.     assert (r == NULL);
  119.   }
  120. while (0)
  121. void *avl_force_delete (avl_tree *, void *);
  122. #else
  123. #define avl_force_insert(A, B)
  124. avl_insert (A, B)
  125. #define avl_force_delete(A, B)
  126. avl_delete (A, B)
  127. #endif
  128. #endif /* avl_h */