| 1 | /* |
| 2 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 3 | * Copyright (C) 2003-2008 Benny Prijono <[email protected]> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJ_RBTREE_H__ |
| 20 | #define __PJ_RBTREE_H__ |
| 21 | |
| 22 | /** |
| 23 | * @file rbtree.h |
| 24 | * @brief Red/Black Tree |
| 25 | */ |
| 26 | |
| 27 | #include <pj/types.h> |
| 28 | |
| 29 | PJ_BEGIN_DECL |
| 30 | |
| 31 | /** |
| 32 | * @defgroup PJ_RBTREE Red/Black Balanced Tree |
| 33 | * @ingroup PJ_DS |
| 34 | * @brief |
| 35 | * Red/Black tree is the variant of balanced tree, where the search, insert, |
| 36 | * and delete operation is \b guaranteed to take at most \a O( lg(n) ). |
| 37 | * @{ |
| 38 | */ |
| 39 | /** |
| 40 | * Color type for Red-Black tree. |
| 41 | */ |
| 42 | typedef enum pj_rbcolor_t |
| 43 | { |
| 44 | PJ_RBCOLOR_BLACK, |
| 45 | PJ_RBCOLOR_RED |
| 46 | } pj_rbcolor_t; |
| 47 | |
| 48 | /** |
| 49 | * The type of the node of the R/B Tree. |
| 50 | */ |
| 51 | typedef struct pj_rbtree_node |
| 52 | { |
| 53 | /** Pointers to the node's parent. */ |
| 54 | struct pj_rbtree_node *parent; |
| 55 | |
| 56 | /** Pointers to the node's left sibling. */ |
| 57 | struct pj_rbtree_node *left; |
| 58 | |
| 59 | /** Pointers to the node's right sibling. */ |
| 60 | struct pj_rbtree_node *right; |
| 61 | |
| 62 | /** Key associated with the node. */ |
| 63 | const void *key; |
| 64 | |
| 65 | /** User data associated with the node. */ |
| 66 | void *user_data; |
| 67 | |
| 68 | /** The R/B Tree node color. */ |
| 69 | pj_rbcolor_t color; |
| 70 | |
| 71 | } pj_rbtree_node; |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * The type of function use to compare key value of tree node. |
| 76 | * @return |
| 77 | * 0 if the keys are equal |
| 78 | * <0 if key1 is lower than key2 |
| 79 | * >0 if key1 is greater than key2. |
| 80 | */ |
| 81 | typedef int pj_rbtree_comp(const void *key1, const void *key2); |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Declaration of a red-black tree. All elements in the tree must have UNIQUE |
| 86 | * key. |
| 87 | * A red black tree always maintains the balance of the tree, so that the |
| 88 | * tree height will not be greater than lg(N). Insert, search, and delete |
| 89 | * operation will take lg(N) on the worst case. But for insert and delete, |
| 90 | * there is additional time needed to maintain the balance of the tree. |
| 91 | */ |
| 92 | typedef struct pj_rbtree |
| 93 | { |
| 94 | pj_rbtree_node null_node; /**< Constant to indicate NULL node. */ |
| 95 | pj_rbtree_node *null; /**< Constant to indicate NULL node. */ |
| 96 | pj_rbtree_node *root; /**< Root tree node. */ |
| 97 | unsigned size; /**< Number of elements in the tree. */ |
| 98 | pj_rbtree_comp *comp; /**< Key comparison function. */ |
| 99 | } pj_rbtree; |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * Guidance on how much memory required for each of the node. |
| 104 | */ |
| 105 | #define PJ_RBTREE_NODE_SIZE (sizeof(pj_rbtree_node)) |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Guidance on memory required for the tree. |
| 110 | */ |
| 111 | #define PJ_RBTREE_SIZE (sizeof(pj_rbtree)) |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Initialize the tree. |
| 116 | * @param tree the tree to be initialized. |
| 117 | * @param comp key comparison function to be used for this tree. |
| 118 | */ |
| 119 | PJ_DECL(void) pj_rbtree_init( pj_rbtree *tree, pj_rbtree_comp *comp); |
| 120 | |
| 121 | /** |
| 122 | * Get the first element in the tree. |
| 123 | * The first element always has the least value for the key, according to |
| 124 | * the comparison function. |
| 125 | * @param tree the tree. |
| 126 | * @return the tree node, or NULL if the tree has no element. |
| 127 | */ |
| 128 | PJ_DECL(pj_rbtree_node*) pj_rbtree_first( pj_rbtree *tree ); |
| 129 | |
| 130 | /** |
| 131 | * Get the last element in the tree. |
| 132 | * The last element always has the greatest key value, according to the |
| 133 | * comparison function defined for the tree. |
| 134 | * @param tree the tree. |
| 135 | * @return the tree node, or NULL if the tree has no element. |
| 136 | */ |
| 137 | PJ_DECL(pj_rbtree_node*) pj_rbtree_last( pj_rbtree *tree ); |
| 138 | |
| 139 | /** |
| 140 | * Get the successive element for the specified node. |
| 141 | * The successive element is an element with greater key value. |
| 142 | * @param tree the tree. |
| 143 | * @param node the node. |
| 144 | * @return the successive node, or NULL if the node has no successor. |
| 145 | */ |
| 146 | PJ_DECL(pj_rbtree_node*) pj_rbtree_next( pj_rbtree *tree, |
| 147 | pj_rbtree_node *node ); |
| 148 | |
| 149 | /** |
| 150 | * The the previous node for the specified node. |
| 151 | * The previous node is an element with less key value. |
| 152 | * @param tree the tree. |
| 153 | * @param node the node. |
| 154 | * @return the previous node, or NULL if the node has no previous node. |
| 155 | */ |
| 156 | PJ_DECL(pj_rbtree_node*) pj_rbtree_prev( pj_rbtree *tree, |
| 157 | pj_rbtree_node *node ); |
| 158 | |
| 159 | /** |
| 160 | * Insert a new node. |
| 161 | * The node will be inserted at sorted location. The key of the node must |
| 162 | * be UNIQUE, i.e. it hasn't existed in the tree. |
| 163 | * @param tree the tree. |
| 164 | * @param node the node to be inserted. |
| 165 | * @return zero on success, or -1 if the key already exist. |
| 166 | */ |
| 167 | PJ_DECL(int) pj_rbtree_insert( pj_rbtree *tree, |
| 168 | pj_rbtree_node *node ); |
| 169 | |
| 170 | /** |
| 171 | * Find a node which has the specified key. |
| 172 | * @param tree the tree. |
| 173 | * @param key the key to search. |
| 174 | * @return the tree node with the specified key, or NULL if the key can not |
| 175 | * be found. |
| 176 | */ |
| 177 | PJ_DECL(pj_rbtree_node*) pj_rbtree_find( pj_rbtree *tree, |
| 178 | const void *key ); |
| 179 | |
| 180 | /** |
| 181 | * Erase a node from the tree. |
| 182 | * @param tree the tree. |
| 183 | * @param node the node to be erased. |
| 184 | * @return the tree node itself. |
| 185 | */ |
| 186 | PJ_DECL(pj_rbtree_node*) pj_rbtree_erase( pj_rbtree *tree, |
| 187 | pj_rbtree_node *node ); |
| 188 | |
| 189 | /** |
| 190 | * Get the maximum tree height from the specified node. |
| 191 | * @param tree the tree. |
| 192 | * @param node the node, or NULL to get the root of the tree. |
| 193 | * @return the maximum height, which should be at most lg(N) |
| 194 | */ |
| 195 | PJ_DECL(unsigned) pj_rbtree_max_height( pj_rbtree *tree, |
| 196 | pj_rbtree_node *node ); |
| 197 | |
| 198 | /** |
| 199 | * Get the minumum tree height from the specified node. |
| 200 | * @param tree the tree. |
| 201 | * @param node the node, or NULL to get the root of the tree. |
| 202 | * @return the height |
| 203 | */ |
| 204 | PJ_DECL(unsigned) pj_rbtree_min_height( pj_rbtree *tree, |
| 205 | pj_rbtree_node *node ); |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * @} |
| 210 | */ |
| 211 | |
| 212 | PJ_END_DECL |
| 213 | |
| 214 | #endif /* __PJ_RBTREE_H__ */ |
| 215 |