avl.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2010 Joseph Adams <joeyadams3.14159@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #ifndef CCAN_AVL_H
  23. #define CCAN_AVL_H
  24. #include <stdbool.h>
  25. #include <stddef.h>
  26. typedef struct AVL AVL;
  27. typedef struct AvlNode AvlNode;
  28. typedef struct AvlIter AvlIter;
  29. typedef int (*AvlCompare)(const void *, const void *);
  30. AVL *avl_new(AvlCompare compare);
  31. /* Create a new AVL tree sorted with the given comparison function. */
  32. void avl_free(AVL *avl);
  33. /* Free an AVL tree. */
  34. void *avl_lookup(const AVL *avl, const void *key);
  35. /* O(log n). Lookup a value at a key. Return NULL if the key is not present. */
  36. #define avl_member(avl, key) (!!avl_lookup_node(avl, key))
  37. /* O(log n). See if a key is present. */
  38. size_t avl_count(const AVL *avl);
  39. /* O(1). Return the number of elements in the tree. */
  40. bool avl_insert(AVL *avl, const void *key, const void *value);
  41. /*
  42. * O(log n). Insert a key/value pair, or replace it if already present.
  43. *
  44. * Return false if the insertion replaced an existing key/value.
  45. */
  46. bool avl_remove(AVL *avl, const void *key);
  47. /*
  48. * O(log n). Remove a key/value pair (if present).
  49. *
  50. * Return true if it was removed.
  51. */
  52. bool avl_check_invariants(AVL *avl);
  53. /* For testing purposes. This function will always return true :-) */
  54. /************************* Traversal *************************/
  55. #define avl_foreach(iter, avl) avl_traverse(iter, avl, FORWARD)
  56. /*
  57. * O(n). Traverse an AVL tree in order.
  58. *
  59. * Example:
  60. *
  61. * AvlIter i;
  62. *
  63. * avl_foreach(i, avl)
  64. * printf("%s -> %s\n", i.key, i.value);
  65. */
  66. #define avl_foreach_reverse(iter, avl) avl_traverse(iter, avl, BACKWARD)
  67. /* O(n). Traverse an AVL tree in reverse order. */
  68. typedef enum AvlDirection {FORWARD = 0, BACKWARD = 1} AvlDirection;
  69. struct AvlIter {
  70. void *key;
  71. void *value;
  72. AvlNode *node;
  73. /* private */
  74. AvlNode *stack[100];
  75. int stack_index;
  76. AvlDirection direction;
  77. };
  78. void avl_iter_begin(AvlIter *iter, AVL *avl, AvlDirection dir);
  79. void avl_iter_next(AvlIter *iter);
  80. #define avl_traverse(iter, avl, direction) \
  81. for (avl_iter_begin(&(iter), avl, direction); \
  82. (iter).node != NULL; \
  83. avl_iter_next(&iter))
  84. /***************** Internal data structures ******************/
  85. struct AVL {
  86. AvlCompare compare;
  87. AvlNode *root;
  88. size_t count;
  89. };
  90. struct AvlNode {
  91. const void *key;
  92. const void *value;
  93. AvlNode *lr[2];
  94. int balance; /* -1, 0, or 1 */
  95. };
  96. AvlNode *avl_lookup_node(const AVL *avl, const void *key);
  97. /* O(log n). Lookup an AVL node by key. Return NULL if not present. */
  98. #endif