avltr.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 avltr.h in libavl. */
  19. #if !avltr_h
  20. #define avltr_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 a right-threaded AVL tree. */
  29. typedef struct avltr_node
  30.   {
  31.     void *data; /* Pointer to data. */
  32.     struct avltr_node *link[2]; /* Subtrees or threads. */
  33.     signed char bal; /* Balance factor. */
  34.     char cache; /* Used during insertion. */
  35.     char pad; /* Reserved for fully threaded trees. */
  36.     signed char rtag; /* Right thread tag. */
  37.   }
  38. avltr_node;
  39. /* Used for traversing a right-threaded AVL tree. */
  40. typedef struct avltr_traverser
  41.   {
  42.     int init; /* Initialized? */
  43.     const avltr_node *p; /* Last node returned. */
  44.   }
  45. avltr_traverser;
  46. /* Initializer for avltr_traverser. */
  47. #define AVLTR_TRAVERSER_INIT {0}
  48. /* Function types. */
  49. #if !AVL_FUNC_TYPES
  50. #define AVL_FUNC_TYPES 1
  51. typedef int (*avl_comparison_func) (const void *a, const void *b, void *param);
  52. typedef void (*avl_node_func) (void *data, void *param);
  53. typedef void *(*avl_copy_func) (void *data, void *param);
  54. #endif
  55. /* Structure which holds information about a threaded AVL tree. */
  56. typedef struct avltr_tree
  57.   {
  58.     avltr_node root; /* Tree root node. */
  59.     avl_comparison_func cmp; /* Used to compare keys. */
  60.     int count; /* Number of nodes in the tree. */
  61.     void *param; /* Arbitary user data. */
  62.   }
  63. avltr_tree;
  64. /* General functions. */
  65. avltr_tree *avltr_create (avl_comparison_func, void *param);
  66. void avltr_destroy (avltr_tree *, avl_node_func);
  67. void avltr_free (avltr_tree *);
  68. int avltr_count (const avltr_tree *);
  69. avltr_tree *avltr_copy (const avltr_tree *, avl_copy_func);
  70. struct avl_tree;
  71. avltr_tree *avltr_thread (struct avl_tree *);
  72. struct avl_tree *avltr_unthread (avltr_tree *);
  73. /* Walk the tree. */
  74. void avltr_walk (const avltr_tree *, avl_node_func, void *param);
  75. void *avltr_traverse (const avltr_tree *, avltr_traverser *);
  76. #define avlt_init_traverser(TRAVERSER) ((TRAVERSER)->init = 0)
  77. void **avltr_next (const avltr_tree *tree, void **item);
  78. /* Search for a given item. */
  79. void **avltr_probe (avltr_tree *, void *);
  80. void *avltr_delete (avltr_tree *, const void *);
  81. void **avltr_find (const avltr_tree *, const void *);
  82. void **avltr_find_close (const avltr_tree *, const void *);
  83. #if __GCC__ >= 2
  84. extern inline void *
  85. avltr_insert (avltr_tree *tree, void *item)
  86. {
  87.   void **p = avltr_probe (tree, item);
  88.   return (*p == item) ? NULL : *p;
  89. }
  90. extern inline void *
  91. avltr_replace (avltr_tree *tree, void *item)
  92. {
  93.   void **p = avltr_probe (tree, item);
  94.   if (*p == item)
  95.     return NULL;
  96.   else
  97.     {
  98.       void *r = *p;
  99.       *p = item;
  100.       return r;
  101.     }
  102. }
  103. #else /* not gcc */
  104. void *avltr_insert (avltr_tree *tree, void *item);
  105. void *avltr_replace (avltr_tree *tree, void *item);
  106. #endif /* not gcc */
  107. /* Easy assertions on insertion & deletion. */
  108. #ifndef NDEBUG
  109. #define avltr_force_insert(A, B)
  110. do
  111.   {
  112.             void *r = avltr_insert (A, B);
  113.     assert (r == NULL);
  114.   }
  115. while (0)
  116. void *avltr_force_delete (avltr_tree *, void *);
  117. #else
  118. #define avltr_force_insert(A, B)
  119. avltr_insert (A, B)
  120. #define avltr_force_delete(A, B)
  121. avltr_delete (A, B)
  122. #endif
  123. #endif /* avltr_h */