├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── example.c ├── package.json └── src ├── jsw_rbtree.c └── jsw_rbtree.h /.gitignore: -------------------------------------------------------------------------------- 1 | *.a 2 | *.bak 3 | *.dSYM 4 | *.o 5 | *.sw* 6 | /build 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | * The MIT License (MIT). 2 | * 3 | * https://github.com/clibs/red-black-tree 4 | * 5 | * Copyright (c) 2003-2014 Julienne Walker (happyfrosty@hotmail.com) [Author] 6 | * Copyright (c) 2014 clibs, Jonathan Barronville (jonathan@scrapum.photos) [Maintainer], and contributors. 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 11 | * 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 13 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | AR ?= ar 2 | CC ?= gcc 3 | CFLAGS = -Ideps -pedantic -std=c99 -v -Wall -Wextra 4 | 5 | ifeq ($(APP_DEBUG),true) 6 | CFLAGS += -g -O0 7 | else 8 | CFLAGS += -O2 9 | endif 10 | 11 | PREFIX ?= /usr/local 12 | 13 | SRCS += $(wildcard src/*.c) 14 | 15 | OBJS += $(SRCS:.c=.o) 16 | 17 | all: build 18 | 19 | %.o: %.c 20 | $(CC) $< $(CFLAGS) -c -o $@ 21 | 22 | build: build/lib/libjsw_rbtree.a 23 | mkdir -p build/include/jsw_rbtree 24 | cp -f src/jsw_rbtree.h build/include/jsw_rbtree/jsw_rbtree.h 25 | 26 | build/lib/libjsw_rbtree.a: $(OBJS) 27 | mkdir -p build/lib 28 | $(AR) -crs $@ $^ 29 | 30 | clean: 31 | rm -fr *.o build example example.dSYM src/*.o 32 | 33 | example: build 34 | $(CC) $(CFLAGS) -Ibuild/include -o example example.c -Lbuild/lib -ljsw_rbtree 35 | 36 | install: all 37 | mkdir -p $(PREFIX)/include/jsw_rbtree 38 | mkdir -p $(PREFIX)/lib 39 | cp -f src/jsw_rbtree.h $(PREFIX)/include/jsw_rbtree/jsw_rbtree.h 40 | cp -f build/lib/libjsw_rbtree.a $(PREFIX)/lib/libjsw_rbtree.a 41 | 42 | uninstall: 43 | rm -fr $(PREFIX)/include/jsw_rbtree/jsw_rbtree.h 44 | rm -fr $(PREFIX)/lib/libjsw_rbtree.a 45 | 46 | .PHONY: build clean example install uninstall 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | __TODO__. 2 | -------------------------------------------------------------------------------- /example.c: -------------------------------------------------------------------------------- 1 | // Example. 2 | 3 | #include "src/jsw_rbtree.h" 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | typedef struct centroid { 10 | double mean; 11 | int count; 12 | } centroid_t; 13 | 14 | typedef struct jsw_rbtree centroidset_t; 15 | 16 | static int centroid_cmp(const void *p1, const void *p2) 17 | { 18 | centroid_t *centroid1, *centroid2; 19 | 20 | centroid1 = (centroid_t*)p1; 21 | centroid2 = (centroid_t*)p2; 22 | 23 | if (centroid1->mean > centroid2->mean) 24 | return 1; 25 | 26 | else if (centroid1->mean < centroid2->mean) 27 | return -1; 28 | 29 | return 0; 30 | } 31 | 32 | static void *centroid_dup(void *p) 33 | { 34 | void *dup_p; 35 | 36 | dup_p = calloc(1, sizeof(struct centroid)); 37 | memmove(dup_p, p, sizeof(struct centroid)); 38 | 39 | return dup_p; 40 | } 41 | 42 | static void centroid_rel(void *p) 43 | { 44 | free(p); 45 | } 46 | 47 | centroidset_t *centroidset_new() 48 | { 49 | jsw_rbtree_t *rbtree; 50 | rbtree = jsw_rbnew(centroid_cmp, centroid_dup, centroid_rel); 51 | 52 | return rbtree; 53 | } 54 | 55 | void centroidset_delete(centroidset_t *centroidset) 56 | { 57 | jsw_rbdelete(centroidset); 58 | } 59 | 60 | int centroidset_weighted_insert(centroidset_t *centroidset, double mean, int weight) 61 | { 62 | int ret; 63 | 64 | centroid_t *centroid; 65 | centroid = calloc(1, sizeof(centroid_t)); 66 | 67 | centroid->mean = mean; 68 | centroid->count = weight; 69 | 70 | ret = jsw_rbinsert(centroidset, (void *)centroid); 71 | if (ret == 0) { 72 | printf("failed to insert the centroid with mean %f and weight %d\n", mean, weight); 73 | free(centroid); 74 | return -1; 75 | } 76 | 77 | return 0; 78 | } 79 | 80 | int centroidset_insert(centroidset_t *centroidset, double mean) 81 | { 82 | return centroidset_weighted_insert(centroidset, mean, 1); 83 | } 84 | 85 | int centroidset_erase(centroidset_t *centroidset, double mean) 86 | { 87 | int ret; 88 | centroid_t *centroid; 89 | 90 | centroid = calloc(1, sizeof(centroid_t)); 91 | centroid->mean = mean; 92 | 93 | ret = jsw_rberase(centroidset, (void*)centroid); 94 | if (ret == 0) { 95 | printf("failed to erase the centroid with mean %f\n", mean); 96 | free(centroid); 97 | return -1; 98 | } 99 | 100 | return 0; 101 | } 102 | 103 | double centroidset_find(centroidset_t *centroidset, double mean) 104 | { 105 | centroid_t *centroid, centroid_find; 106 | 107 | centroid_find.mean = mean; 108 | centroid = jsw_rbfind(centroidset, ¢roid_find); 109 | if (!centroid) { 110 | return NAN; 111 | } 112 | return centroid->mean; 113 | } 114 | 115 | void centroidset_printset(centroidset_t *centroidset) 116 | { 117 | centroid_t *centroid; 118 | 119 | jsw_rbtrav_t *rbtrav; 120 | rbtrav = jsw_rbtnew(); 121 | 122 | centroid = jsw_rbtfirst(rbtrav, centroidset); 123 | printf("mean %f\n", centroid->mean); 124 | 125 | while ((centroid = jsw_rbtnext(rbtrav)) != NULL) { 126 | printf("mean %f\n", centroid->mean); 127 | } 128 | } 129 | 130 | int main() 131 | { 132 | centroidset_t *centroidset; 133 | centroidset = centroidset_new(); 134 | centroidset_insert(centroidset, 1.0); 135 | centroidset_insert(centroidset, 2.0); 136 | centroidset_insert(centroidset, 1.5); 137 | centroidset_insert(centroidset, 9.9); 138 | centroidset_insert(centroidset, 1.8); 139 | centroidset_insert(centroidset, 3.3); 140 | double ret; 141 | ret = centroidset_find(centroidset, 1.0f); 142 | printf("find 1.0: %f\n", ret); 143 | ret = centroidset_find(centroidset, 9.900000); 144 | printf("find 9.9: %f\n", ret); 145 | ret = centroidset_find(centroidset, 1.800000); 146 | printf("find 1.8: %f\n", ret); 147 | ret = centroidset_find(centroidset, 0); 148 | printf("find 0 (not there) %f\n", ret); 149 | ret = centroidset_erase(centroidset, 1.800000); 150 | printf("erase 1.800000: %f\n", ret); 151 | ret = centroidset_find(centroidset, 1.800000); 152 | printf("find 1.8: %f\n", ret); 153 | centroidset_printset(centroidset); 154 | centroidset_delete(centroidset); 155 | } 156 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "Generic red-black tree library (by Julienne Walker).", 3 | 4 | "keywords": [ 5 | "binary", 6 | "black", 7 | "red", 8 | "search", 9 | "tree" 10 | ], 11 | 12 | "license": "MIT", 13 | "name": "red-black-tree", 14 | "repo": "clibs/red-black-tree", 15 | 16 | "src": [ 17 | "src/jsw_rbtree.c", 18 | "src/jsw_rbtree.h" 19 | ], 20 | 21 | "version": "1.0.0" 22 | } 23 | -------------------------------------------------------------------------------- /src/jsw_rbtree.c: -------------------------------------------------------------------------------- 1 | /* 2 | Red Black balanced tree library 3 | 4 | > Created (Julienne Walker): August 23, 2003 5 | > Modified (Julienne Walker): March 14, 2008 6 | */ 7 | #include "jsw_rbtree.h" 8 | 9 | #ifdef __cplusplus 10 | #include 11 | 12 | using std::malloc; 13 | using std::free; 14 | using std::size_t; 15 | #else 16 | #include 17 | #endif 18 | 19 | #ifndef HEIGHT_LIMIT 20 | #define HEIGHT_LIMIT 64 /* Tallest allowable tree */ 21 | #endif 22 | 23 | typedef struct jsw_rbnode { 24 | int red; /* Color (1=red, 0=black) */ 25 | void *data; /* User-defined content */ 26 | struct jsw_rbnode *link[2]; /* Left (0) and right (1) links */ 27 | } jsw_rbnode_t; 28 | 29 | struct jsw_rbtree { 30 | jsw_rbnode_t *root; /* Top of the tree */ 31 | cmp_f cmp; /* Compare two items */ 32 | dup_f dup; /* Clone an item (user-defined) */ 33 | rel_f rel; /* Destroy an item (user-defined) */ 34 | size_t size; /* Number of items (user-defined) */ 35 | }; 36 | 37 | struct jsw_rbtrav { 38 | jsw_rbtree_t *tree; /* Paired tree */ 39 | jsw_rbnode_t *it; /* Current node */ 40 | jsw_rbnode_t *path[HEIGHT_LIMIT]; /* Traversal path */ 41 | size_t top; /* Top of stack */ 42 | }; 43 | 44 | /** 45 | 46 | Checks the color of a red black node 47 | 48 | The node to check 49 | 1 for a red node, 0 for a black node 50 | For jsw_rbtree.c internal use only 51 | */ 52 | static int is_red ( jsw_rbnode_t *root ) 53 | { 54 | return root != NULL && root->red == 1; 55 | } 56 | 57 | /** 58 | 59 | Performs a single red black rotation in the specified direction 60 | This function assumes that all nodes are valid for a rotation 61 | 62 | The original root to rotate around 63 | The direction to rotate (0 = left, 1 = right) 64 | The new root ater rotation 65 | For jsw_rbtree.c internal use only 66 | */ 67 | static jsw_rbnode_t *jsw_single ( jsw_rbnode_t *root, int dir ) 68 | { 69 | jsw_rbnode_t *save = root->link[!dir]; 70 | 71 | root->link[!dir] = save->link[dir]; 72 | save->link[dir] = root; 73 | 74 | root->red = 1; 75 | save->red = 0; 76 | 77 | return save; 78 | } 79 | 80 | /** 81 | 82 | Performs a double red black rotation in the specified direction 83 | This function assumes that all nodes are valid for a rotation 84 | 85 | The original root to rotate around 86 | The direction to rotate (0 = left, 1 = right) 87 | The new root after rotation 88 | For jsw_rbtree.c internal use only 89 | */ 90 | static jsw_rbnode_t *jsw_double ( jsw_rbnode_t *root, int dir ) 91 | { 92 | root->link[!dir] = jsw_single ( root->link[!dir], !dir ); 93 | 94 | return jsw_single ( root, dir ); 95 | } 96 | 97 | /** 98 | 99 | Creates an initializes a new red black node with a copy of 100 | the data. This function does not insert the new node into a tree 101 | 102 | The red black tree this node is being created for 103 | The data value that will be stored in this node 104 | A pointer to the new node 105 | 106 | For jsw_rbtree.c internal use only. The data for this node must 107 | be freed using the same tree's rel function. The returned pointer 108 | must be freed using C's free function 109 | 110 | */ 111 | static jsw_rbnode_t *new_node ( jsw_rbtree_t *tree, void *data ) 112 | { 113 | jsw_rbnode_t *rn = (jsw_rbnode_t *)malloc ( sizeof *rn ); 114 | 115 | if ( rn == NULL ) 116 | return NULL; 117 | 118 | rn->red = 1; 119 | rn->data = tree->dup ( data ); 120 | rn->link[0] = rn->link[1] = NULL; 121 | 122 | return rn; 123 | } 124 | 125 | /** 126 | 127 | Creates and initializes an empty red black tree with 128 | user-defined comparison, data copy, and data release operations 129 | 130 | User-defined data comparison function 131 | User-defined data copy function 132 | User-defined data release function 133 | A pointer to the new tree 134 | 135 | The returned pointer must be released with jsw_rbdelete 136 | 137 | */ 138 | jsw_rbtree_t *jsw_rbnew ( cmp_f cmp, dup_f dup, rel_f rel ) 139 | { 140 | jsw_rbtree_t *rt = (jsw_rbtree_t *)malloc ( sizeof *rt ); 141 | 142 | if ( rt == NULL ) 143 | return NULL; 144 | 145 | rt->root = NULL; 146 | rt->cmp = cmp; 147 | rt->dup = dup; 148 | rt->rel = rel; 149 | rt->size = 0; 150 | 151 | return rt; 152 | } 153 | 154 | /** 155 | 156 | Releases a valid red black tree 157 | 158 | The tree to release 159 | 160 | The tree must have been created using jsw_rbnew 161 | 162 | */ 163 | void jsw_rbdelete ( jsw_rbtree_t *tree ) 164 | { 165 | jsw_rbnode_t *it = tree->root; 166 | jsw_rbnode_t *save; 167 | 168 | /* 169 | Rotate away the left links so that 170 | we can treat this like the destruction 171 | of a linked list 172 | */ 173 | while ( it != NULL ) { 174 | if ( it->link[0] == NULL ) { 175 | /* No left links, just kill the node and move on */ 176 | save = it->link[1]; 177 | tree->rel ( it->data ); 178 | free ( it ); 179 | } 180 | else { 181 | /* Rotate away the left link and check again */ 182 | save = it->link[0]; 183 | it->link[0] = save->link[1]; 184 | save->link[1] = it; 185 | } 186 | 187 | it = save; 188 | } 189 | 190 | free ( tree ); 191 | } 192 | 193 | /** 194 | 195 | Search for a copy of the specified 196 | node data in a red black tree 197 | 198 | The tree to search 199 | The data value to search for 200 | 201 | A pointer to the data value stored in the tree, 202 | or a null pointer if no data could be found 203 | 204 | */ 205 | void *jsw_rbfind ( jsw_rbtree_t *tree, void *data ) 206 | { 207 | jsw_rbnode_t *it = tree->root; 208 | 209 | while ( it != NULL ) { 210 | int cmp = tree->cmp ( it->data, data ); 211 | 212 | if ( cmp == 0 ) 213 | break; 214 | 215 | /* 216 | If the tree supports duplicates, they should be 217 | chained to the right subtree for this to work 218 | */ 219 | it = it->link[cmp < 0]; 220 | } 221 | 222 | return it == NULL ? NULL : it->data; 223 | } 224 | 225 | /** 226 | 227 | Insert a copy of the user-specified 228 | data into a red black tree 229 | 230 | The tree to insert into 231 | The data value to insert 232 | 233 | 1 if the value was inserted successfully, 234 | 0 if the insertion failed for any reason 235 | 236 | */ 237 | int jsw_rbinsert ( jsw_rbtree_t *tree, void *data ) 238 | { 239 | if ( tree->root == NULL ) { 240 | /* 241 | We have an empty tree; attach the 242 | new node directly to the root 243 | */ 244 | tree->root = new_node ( tree, data ); 245 | 246 | if ( tree->root == NULL ) 247 | return 0; 248 | } 249 | else { 250 | jsw_rbnode_t head = {0}; /* False tree root */ 251 | jsw_rbnode_t *g, *t; /* Grandparent & parent */ 252 | jsw_rbnode_t *p, *q; /* Iterator & parent */ 253 | int dir = 0, last = 0; 254 | 255 | /* Set up our helpers */ 256 | t = &head; 257 | g = p = NULL; 258 | q = t->link[1] = tree->root; 259 | 260 | /* Search down the tree for a place to insert */ 261 | for ( ; ; ) { 262 | if ( q == NULL ) { 263 | /* Insert a new node at the first null link */ 264 | p->link[dir] = q = new_node ( tree, data ); 265 | 266 | if ( q == NULL ) 267 | return 0; 268 | } 269 | else if ( is_red ( q->link[0] ) && is_red ( q->link[1] ) ) { 270 | /* Simple red violation: color flip */ 271 | q->red = 1; 272 | q->link[0]->red = 0; 273 | q->link[1]->red = 0; 274 | } 275 | 276 | if ( is_red ( q ) && is_red ( p ) ) { 277 | /* Hard red violation: rotations necessary */ 278 | int dir2 = t->link[1] == g; 279 | 280 | if ( q == p->link[last] ) 281 | t->link[dir2] = jsw_single ( g, !last ); 282 | else 283 | t->link[dir2] = jsw_double ( g, !last ); 284 | } 285 | 286 | /* 287 | Stop working if we inserted a node. This 288 | check also disallows duplicates in the tree 289 | */ 290 | if ( tree->cmp ( q->data, data ) == 0 ) 291 | break; 292 | 293 | last = dir; 294 | dir = tree->cmp ( q->data, data ) < 0; 295 | 296 | /* Move the helpers down */ 297 | if ( g != NULL ) 298 | t = g; 299 | 300 | g = p, p = q; 301 | q = q->link[dir]; 302 | } 303 | 304 | /* Update the root (it may be different) */ 305 | tree->root = head.link[1]; 306 | } 307 | 308 | /* Make the root black for simplified logic */ 309 | tree->root->red = 0; 310 | ++tree->size; 311 | 312 | return 1; 313 | } 314 | 315 | /** 316 | 317 | Remove a node from a red black tree 318 | that matches the user-specified data 319 | 320 | The tree to remove from 321 | The data value to search for 322 | 323 | 1 if the value was removed successfully, 324 | 0 if the removal failed for any reason 325 | 326 | 327 | The most common failure reason should be 328 | that the data was not found in the tree 329 | 330 | */ 331 | int jsw_rberase ( jsw_rbtree_t *tree, void *data ) 332 | { 333 | if ( tree->root != NULL ) { 334 | jsw_rbnode_t head = {0}; /* False tree root */ 335 | jsw_rbnode_t *q, *p, *g; /* Helpers */ 336 | jsw_rbnode_t *f = NULL; /* Found item */ 337 | int dir = 1; 338 | 339 | /* Set up our helpers */ 340 | q = &head; 341 | g = p = NULL; 342 | q->link[1] = tree->root; 343 | 344 | /* 345 | Search and push a red node down 346 | to fix red violations as we go 347 | */ 348 | while ( q->link[dir] != NULL ) { 349 | int last = dir; 350 | 351 | /* Move the helpers down */ 352 | g = p, p = q; 353 | q = q->link[dir]; 354 | dir = tree->cmp ( q->data, data ) < 0; 355 | 356 | /* 357 | Save the node with matching data and keep 358 | going; we'll do removal tasks at the end 359 | */ 360 | if ( tree->cmp ( q->data, data ) == 0 ) 361 | f = q; 362 | 363 | /* Push the red node down with rotations and color flips */ 364 | if ( !is_red ( q ) && !is_red ( q->link[dir] ) ) { 365 | if ( is_red ( q->link[!dir] ) ) 366 | p = p->link[last] = jsw_single ( q, dir ); 367 | else if ( !is_red ( q->link[!dir] ) ) { 368 | jsw_rbnode_t *s = p->link[!last]; 369 | 370 | if ( s != NULL ) { 371 | if ( !is_red ( s->link[!last] ) && !is_red ( s->link[last] ) ) { 372 | /* Color flip */ 373 | p->red = 0; 374 | s->red = 1; 375 | q->red = 1; 376 | } 377 | else { 378 | int dir2 = g->link[1] == p; 379 | 380 | if ( is_red ( s->link[last] ) ) 381 | g->link[dir2] = jsw_double ( p, last ); 382 | else if ( is_red ( s->link[!last] ) ) 383 | g->link[dir2] = jsw_single ( p, last ); 384 | 385 | /* Ensure correct coloring */ 386 | q->red = g->link[dir2]->red = 1; 387 | g->link[dir2]->link[0]->red = 0; 388 | g->link[dir2]->link[1]->red = 0; 389 | } 390 | } 391 | } 392 | } 393 | } 394 | 395 | /* Replace and remove the saved node */ 396 | if ( f != NULL ) { 397 | tree->rel ( f->data ); 398 | f->data = q->data; 399 | p->link[p->link[1] == q] = 400 | q->link[q->link[0] == NULL]; 401 | free ( q ); 402 | } 403 | 404 | /* Update the root (it may be different) */ 405 | tree->root = head.link[1]; 406 | 407 | /* Make the root black for simplified logic */ 408 | if ( tree->root != NULL ) 409 | tree->root->red = 0; 410 | 411 | --tree->size; 412 | } 413 | 414 | return 1; 415 | } 416 | 417 | /** 418 | 419 | Gets the number of nodes in a red black tree 420 | 421 | The tree to calculate a size for 422 | The number of nodes in the tree 423 | */ 424 | size_t jsw_rbsize ( jsw_rbtree_t *tree ) 425 | { 426 | return tree->size; 427 | } 428 | 429 | /** 430 | 431 | Create a new traversal object 432 | 433 | A pointer to the new object 434 | 435 | The traversal object is not initialized until 436 | jsw_rbtfirst or jsw_rbtlast are called. 437 | The pointer must be released with jsw_rbtdelete 438 | 439 | */ 440 | jsw_rbtrav_t *jsw_rbtnew ( void ) 441 | { 442 | return (jsw_rbtrav_t*)malloc ( sizeof ( jsw_rbtrav_t ) ); 443 | } 444 | 445 | /** 446 | 447 | Release a traversal object 448 | 449 | The object to release 450 | 451 | The object must have been created with jsw_rbtnew 452 | 453 | */ 454 | void jsw_rbtdelete ( jsw_rbtrav_t *trav ) 455 | { 456 | free ( trav ); 457 | } 458 | 459 | /** 460 | 461 | Initialize a traversal object. The user-specified 462 | direction determines whether to begin traversal at the 463 | smallest or largest valued node 464 | 465 | The traversal object to initialize 466 | The tree that the object will be attached to 467 | 468 | The direction to traverse (0 = ascending, 1 = descending) 469 | 470 | A pointer to the smallest or largest data value 471 | For jsw_rbtree.c internal use only 472 | */ 473 | static void *start ( jsw_rbtrav_t *trav, jsw_rbtree_t *tree, int dir ) 474 | { 475 | trav->tree = tree; 476 | trav->it = tree->root; 477 | trav->top = 0; 478 | 479 | /* Save the path for later traversal */ 480 | if ( trav->it != NULL ) { 481 | while ( trav->it->link[dir] != NULL ) { 482 | trav->path[trav->top++] = trav->it; 483 | trav->it = trav->it->link[dir]; 484 | } 485 | } 486 | 487 | return trav->it == NULL ? NULL : trav->it->data; 488 | } 489 | 490 | /** 491 | 492 | Traverse a red black tree in the user-specified direction 493 | 494 | The initialized traversal object 495 | 496 | The direction to traverse (0 = ascending, 1 = descending) 497 | 498 | 499 | A pointer to the next data value in the specified direction 500 | 501 | For jsw_rbtree.c internal use only 502 | */ 503 | static void *move ( jsw_rbtrav_t *trav, int dir ) 504 | { 505 | if ( trav->it->link[dir] != NULL ) { 506 | /* Continue down this branch */ 507 | trav->path[trav->top++] = trav->it; 508 | trav->it = trav->it->link[dir]; 509 | 510 | while ( trav->it->link[!dir] != NULL ) { 511 | trav->path[trav->top++] = trav->it; 512 | trav->it = trav->it->link[!dir]; 513 | } 514 | } 515 | else { 516 | /* Move to the next branch */ 517 | jsw_rbnode_t *last; 518 | 519 | do { 520 | if ( trav->top == 0 ) { 521 | trav->it = NULL; 522 | break; 523 | } 524 | 525 | last = trav->it; 526 | trav->it = trav->path[--trav->top]; 527 | } while ( last == trav->it->link[dir] ); 528 | } 529 | 530 | return trav->it == NULL ? NULL : trav->it->data; 531 | } 532 | 533 | /** 534 | 535 | Initialize a traversal object to the smallest valued node 536 | 537 | The traversal object to initialize 538 | The tree that the object will be attached to 539 | A pointer to the smallest data value 540 | */ 541 | void *jsw_rbtfirst ( jsw_rbtrav_t *trav, jsw_rbtree_t *tree ) 542 | { 543 | return start ( trav, tree, 0 ); /* Min value */ 544 | } 545 | 546 | /** 547 | 548 | Initialize a traversal object to the largest valued node 549 | 550 | The traversal object to initialize 551 | The tree that the object will be attached to 552 | A pointer to the largest data value 553 | */ 554 | void *jsw_rbtlast ( jsw_rbtrav_t *trav, jsw_rbtree_t *tree ) 555 | { 556 | return start ( trav, tree, 1 ); /* Max value */ 557 | } 558 | 559 | /** 560 | 561 | Traverse to the next value in ascending order 562 | 563 | The initialized traversal object 564 | A pointer to the next value in ascending order 565 | */ 566 | void *jsw_rbtnext ( jsw_rbtrav_t *trav ) 567 | { 568 | return move ( trav, 1 ); /* Toward larger items */ 569 | } 570 | 571 | /** 572 | 573 | Traverse to the next value in descending order 574 | 575 | The initialized traversal object 576 | A pointer to the next value in descending order 577 | */ 578 | void *jsw_rbtprev ( jsw_rbtrav_t *trav ) 579 | { 580 | return move ( trav, 0 ); /* Toward smaller items */ 581 | } -------------------------------------------------------------------------------- /src/jsw_rbtree.h: -------------------------------------------------------------------------------- 1 | #ifndef JSW_RBTREE_H 2 | #define JSW_RBTREE_H 3 | 4 | /* 5 | Red Black balanced tree library 6 | 7 | > Created (Julienne Walker): August 23, 2003 8 | > Modified (Julienne Walker): March 14, 2008 9 | 10 | This code is in the public domain. Anyone may 11 | use it or change it in any way that they see 12 | fit. The author assumes no responsibility for 13 | damages incurred through use of the original 14 | code or any variations thereof. 15 | 16 | It is requested, but not required, that due 17 | credit is given to the original author and 18 | anyone who has modified the code through 19 | a header comment, such as this one. 20 | */ 21 | #ifdef __cplusplus 22 | #include 23 | 24 | using std::size_t; 25 | 26 | extern "C" { 27 | #else 28 | #include 29 | #endif 30 | 31 | /* Opaque types */ 32 | typedef struct jsw_rbtree jsw_rbtree_t; 33 | typedef struct jsw_rbtrav jsw_rbtrav_t; 34 | 35 | /* User-defined item handling */ 36 | typedef int (*cmp_f) ( const void *p1, const void *p2 ); 37 | typedef void *(*dup_f) ( void *p ); 38 | typedef void (*rel_f) ( void *p ); 39 | 40 | /* Red Black tree functions */ 41 | jsw_rbtree_t *jsw_rbnew ( cmp_f cmp, dup_f dup, rel_f rel ); 42 | void jsw_rbdelete ( jsw_rbtree_t *tree ); 43 | void *jsw_rbfind ( jsw_rbtree_t *tree, void *data ); 44 | int jsw_rbinsert ( jsw_rbtree_t *tree, void *data ); 45 | int jsw_rberase ( jsw_rbtree_t *tree, void *data ); 46 | size_t jsw_rbsize ( jsw_rbtree_t *tree ); 47 | 48 | /* Traversal functions */ 49 | jsw_rbtrav_t *jsw_rbtnew ( void ); 50 | void jsw_rbtdelete ( jsw_rbtrav_t *trav ); 51 | void *jsw_rbtfirst ( jsw_rbtrav_t *trav, jsw_rbtree_t *tree ); 52 | void *jsw_rbtlast ( jsw_rbtrav_t *trav, jsw_rbtree_t *tree ); 53 | void *jsw_rbtnext ( jsw_rbtrav_t *trav ); 54 | void *jsw_rbtprev ( jsw_rbtrav_t *trav ); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif 61 | --------------------------------------------------------------------------------