00001 #include <stdio.h>
00002 #include <stdlib.h>
00003
00004 #include "red_black_tree.h"
00005
00006
00007
00008
00009 void NullFunction(void * junk) { ; }
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 static void Assert(int assertion, char* error) {
00030 if(!assertion) {
00031 printf("Assertion Failed: %s\n",error);
00032 exit(-1);
00033 }
00034 }
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 rb_red_blk_tree* RBTreeCreate( int (*CompFunc) (const void*,const void*),
00058 void (*DestFunc) (void*),
00059 void (*InfoDestFunc) (void*),
00060 void (*PrintFunc) (const void*),
00061 void (*PrintInfo)(void*)) {
00062 rb_red_blk_tree* newTree;
00063 rb_red_blk_node* temp;
00064
00065 if (!(newTree=(rb_red_blk_tree*) malloc(sizeof(rb_red_blk_tree)))) {
00066 return NULL;
00067 }
00068
00069 newTree->Compare= CompFunc;
00070 newTree->DestroyKey= DestFunc;
00071 newTree->PrintKey= PrintFunc;
00072 newTree->PrintInfo= PrintInfo;
00073 newTree->DestroyInfo= InfoDestFunc;
00074
00075
00076
00077 if (!(temp=newTree->nil= (rb_red_blk_node*) malloc(sizeof(rb_red_blk_node)))) {
00078 goto err_alloc_newTree_nil;
00079 }
00080 temp->parent=temp->left=temp->right=temp;
00081 temp->red=0;
00082 temp->key=0;
00083 if(!(temp=newTree->root= (rb_red_blk_node*) malloc(sizeof(rb_red_blk_node)))) {
00084 goto err_alloc_newTree_root;
00085 }
00086 temp->parent=temp->left=temp->right=newTree->nil;
00087 temp->key=0;
00088 temp->red=0;
00089 return(newTree);
00090
00091 err_alloc_newTree_root:
00092 free(newTree->nil);
00093 err_alloc_newTree_nil:
00094 free(newTree);
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 void LeftRotate(rb_red_blk_tree* tree, rb_red_blk_node* x) {
00115 rb_red_blk_node* y;
00116 rb_red_blk_node* nil=tree->nil;
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 y=x->right;
00129 x->right=y->left;
00130
00131 if (y->left != nil) y->left->parent=x;
00132
00133
00134 y->parent=x->parent;
00135
00136
00137
00138 if( x == x->parent->left) {
00139 x->parent->left=y;
00140 } else {
00141 x->parent->right=y;
00142 }
00143 y->left=x;
00144 x->parent=y;
00145
00146 #ifdef DEBUG_ASSERT
00147 Assert(!tree->nil->red,"nil not red in LeftRotate");
00148 #endif
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 void RightRotate(rb_red_blk_tree* tree, rb_red_blk_node* y) {
00170 rb_red_blk_node* x;
00171 rb_red_blk_node* nil=tree->nil;
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 x=y->left;
00184 y->left=x->right;
00185
00186 if (nil != x->right) x->right->parent=y;
00187
00188
00189
00190
00191 x->parent=y->parent;
00192 if( y == y->parent->left) {
00193 y->parent->left=x;
00194 } else {
00195 y->parent->right=x;
00196 }
00197 x->right=y;
00198 y->parent=x;
00199
00200 #ifdef DEBUG_ASSERT
00201 Assert(!tree->nil->red,"nil not red in RightRotate");
00202 #endif
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 void TreeInsertHelp(rb_red_blk_tree* tree, rb_red_blk_node* z) {
00221
00222 rb_red_blk_node* x;
00223 rb_red_blk_node* y;
00224 rb_red_blk_node* nil=tree->nil;
00225
00226 z->left=z->right=nil;
00227 y=tree->root;
00228 x=tree->root->left;
00229 while( x != nil) {
00230 y=x;
00231 if (1 == tree->Compare(x->key,z->key)) {
00232 x=x->left;
00233 } else {
00234 x=x->right;
00235 }
00236 }
00237 z->parent=y;
00238 if ( (y == tree->root) ||
00239 (1 == tree->Compare(y->key,z->key))) {
00240 y->left=z;
00241 } else {
00242 y->right=z;
00243 }
00244
00245 #ifdef DEBUG_ASSERT
00246 Assert(!tree->nil->red,"nil not red in TreeInsertHelp");
00247 #endif
00248 }
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 rb_red_blk_node * RBTreeInsert(rb_red_blk_tree* tree, void* key, void* info) {
00271 rb_red_blk_node * y;
00272 rb_red_blk_node * x;
00273 rb_red_blk_node * newNode;
00274
00275 if (!(x=(rb_red_blk_node*) malloc(sizeof(rb_red_blk_node)))) {
00276 return NULL;
00277 }
00278 x->key=key;
00279 x->info=info;
00280
00281 TreeInsertHelp(tree,x);
00282 newNode=x;
00283 x->red=1;
00284 while(x->parent->red) {
00285 if (x->parent == x->parent->parent->left) {
00286 y=x->parent->parent->right;
00287 if (y->red) {
00288 x->parent->red=0;
00289 y->red=0;
00290 x->parent->parent->red=1;
00291 x=x->parent->parent;
00292 } else {
00293 if (x == x->parent->right) {
00294 x=x->parent;
00295 LeftRotate(tree,x);
00296 }
00297 x->parent->red=0;
00298 x->parent->parent->red=1;
00299 RightRotate(tree,x->parent->parent);
00300 }
00301 } else {
00302 y=x->parent->parent->left;
00303 if (y->red) {
00304 x->parent->red=0;
00305 y->red=0;
00306 x->parent->parent->red=1;
00307 x=x->parent->parent;
00308 } else {
00309 if (x == x->parent->left) {
00310 x=x->parent;
00311 RightRotate(tree,x);
00312 }
00313 x->parent->red=0;
00314 x->parent->parent->red=1;
00315 LeftRotate(tree,x->parent->parent);
00316 }
00317 }
00318 }
00319 tree->root->left->red=0;
00320 return(newNode);
00321
00322 #ifdef DEBUG_ASSERT
00323 Assert(!tree->nil->red,"nil not red in RBTreeInsert");
00324 Assert(!tree->root->red,"root not red in RBTreeInsert");
00325 #endif
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 rb_red_blk_node* TreeSuccessor(rb_red_blk_tree* tree,rb_red_blk_node* x) {
00343 rb_red_blk_node* y;
00344 rb_red_blk_node* nil=tree->nil;
00345 rb_red_blk_node* root=tree->root;
00346
00347 if (nil != (y = x->right)) {
00348 while(y->left != nil) {
00349 y=y->left;
00350 }
00351 return(y);
00352 } else {
00353 y=x->parent;
00354 while(x == y->right) {
00355 x=y;
00356 y=y->parent;
00357 }
00358 if (y == root) return(nil);
00359 return(y);
00360 }
00361 }
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377 rb_red_blk_node* TreePredecessor(rb_red_blk_tree* tree, rb_red_blk_node* x) {
00378 rb_red_blk_node* y;
00379 rb_red_blk_node* nil=tree->nil;
00380 rb_red_blk_node* root=tree->root;
00381
00382 if (nil != (y = x->left)) {
00383 while(y->right != nil) {
00384 y=y->right;
00385 }
00386 return(y);
00387 } else {
00388 y=x->parent;
00389 while(x == y->left) {
00390 if (y == root) return(nil);
00391 x=y;
00392 y=y->parent;
00393 }
00394 return(y);
00395 }
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 void InorderTreePrint(rb_red_blk_tree* tree, rb_red_blk_node* x) {
00414 rb_red_blk_node* nil=tree->nil;
00415 rb_red_blk_node* root=tree->root;
00416 if (x != tree->nil) {
00417 InorderTreePrint(tree,x->left);
00418 printf("info=");
00419 if (tree->PrintInfo) tree->PrintInfo(x->info);
00420 printf(" key=");
00421 if (tree->PrintKey) {
00422 tree->PrintKey(x->key);
00423 }
00424 printf(" l->key=");
00425 if( x->left == nil) printf("NULL"); else {
00426 if (tree->PrintKey) tree->PrintKey(x->left->key);
00427 }
00428 printf(" r->key=");
00429 if( x->right == nil) printf("NULL"); else {
00430 if (tree->PrintKey) tree->PrintKey(x->right->key);
00431 }
00432 printf(" p->key=");
00433 if( x->parent == root) printf("NULL"); else {
00434 if (tree->PrintKey) tree->PrintKey(x->parent->key);
00435 }
00436 printf(" red=%i\n",x->red);
00437 InorderTreePrint(tree,x->right);
00438 }
00439 }
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 void TreeDestHelper(rb_red_blk_tree* tree, rb_red_blk_node* x) {
00458 rb_red_blk_node* nil=tree->nil;
00459 if (x != nil) {
00460 TreeDestHelper(tree,x->left);
00461 TreeDestHelper(tree,x->right);
00462 tree->DestroyKey(x->key);
00463 tree->DestroyInfo(x->info);
00464 free(x);
00465 }
00466 }
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482 void RBTreeDestroy(rb_red_blk_tree* tree) {
00483 TreeDestHelper(tree,tree->root->left);
00484 free(tree->root);
00485 free(tree->nil);
00486 free(tree);
00487 }
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 void RBTreePrint(rb_red_blk_tree* tree) {
00505 InorderTreePrint(tree,tree->root->left);
00506 }
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523 rb_red_blk_node* RBExactQuery(rb_red_blk_tree* tree, void* q) {
00524 rb_red_blk_node* x=tree->root->left;
00525 rb_red_blk_node* nil=tree->nil;
00526 int compVal;
00527 if (x == nil) return(0);
00528 compVal=tree->Compare(x->key,(int*) q);
00529 while(0 != compVal) {
00530 if (1 == compVal) {
00531 x=x->left;
00532 } else {
00533 x=x->right;
00534 }
00535 if ( x == nil) return(0);
00536 compVal=tree->Compare(x->key,(int*) q);
00537 }
00538 return(x);
00539 }
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 rb_red_blk_node* RBQueryLargestSmaller(rb_red_blk_tree* tree, void* q) {
00554 rb_red_blk_node* x=tree->root->left;
00555 rb_red_blk_node* nil=tree->nil;
00556 int compVal;
00557 if (x == nil) return(0);
00558 compVal=tree->Compare(x->key,(int*) q);
00559 while(0 != compVal) {
00560 if (1 == compVal) {
00561 x=x->left;
00562 } else {
00563 x=x->right;
00564 }
00565 if ( x == nil) return(0);
00566 compVal=tree->Compare(x->key,(int*) q);
00567 }
00568 return(x);
00569 }
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589 void RBDeleteFixUp(rb_red_blk_tree* tree, rb_red_blk_node* x) {
00590 rb_red_blk_node* root=tree->root->left;
00591 rb_red_blk_node* w;
00592
00593 while( (!x->red) && (root != x)) {
00594 if (x == x->parent->left) {
00595 w=x->parent->right;
00596 if (w->red) {
00597 w->red=0;
00598 x->parent->red=1;
00599 LeftRotate(tree,x->parent);
00600 w=x->parent->right;
00601 }
00602 if ( (!w->right->red) && (!w->left->red) ) {
00603 w->red=1;
00604 x=x->parent;
00605 } else {
00606 if (!w->right->red) {
00607 w->left->red=0;
00608 w->red=1;
00609 RightRotate(tree,w);
00610 w=x->parent->right;
00611 }
00612 w->red=x->parent->red;
00613 x->parent->red=0;
00614 w->right->red=0;
00615 LeftRotate(tree,x->parent);
00616 x=root;
00617 }
00618 } else {
00619 w=x->parent->left;
00620 if (w->red) {
00621 w->red=0;
00622 x->parent->red=1;
00623 RightRotate(tree,x->parent);
00624 w=x->parent->left;
00625 }
00626 if ( (!w->right->red) && (!w->left->red) ) {
00627 w->red=1;
00628 x=x->parent;
00629 } else {
00630 if (!w->left->red) {
00631 w->right->red=0;
00632 w->red=1;
00633 LeftRotate(tree,w);
00634 w=x->parent->left;
00635 }
00636 w->red=x->parent->red;
00637 x->parent->red=0;
00638 w->left->red=0;
00639 RightRotate(tree,x->parent);
00640 x=root;
00641 }
00642 }
00643 }
00644 x->red=0;
00645
00646 #ifdef DEBUG_ASSERT
00647 Assert(!tree->nil->red,"nil not black in RBDeleteFixUp");
00648 #endif
00649 }
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668 void RBDelete(rb_red_blk_tree* tree, rb_red_blk_node* z){
00669 rb_red_blk_node* y;
00670 rb_red_blk_node* x;
00671 rb_red_blk_node* nil=tree->nil;
00672 rb_red_blk_node* root=tree->root;
00673
00674 y= ((z->left == nil) || (z->right == nil)) ? z : TreeSuccessor(tree,z);
00675 x= (y->left == nil) ? y->right : y->left;
00676 if (root == (x->parent = y->parent)) {
00677 root->left=x;
00678 } else {
00679 if (y == y->parent->left) {
00680 y->parent->left=x;
00681 } else {
00682 y->parent->right=x;
00683 }
00684 }
00685 if (y != z) {
00686
00687 #ifdef DEBUG_ASSERT
00688 Assert( (y!=tree->nil),"y is nil in RBDelete\n");
00689 #endif
00690
00691
00692 if (!(y->red)) RBDeleteFixUp(tree,x);
00693
00694 tree->DestroyKey(z->key);
00695 tree->DestroyInfo(z->info);
00696 y->left=z->left;
00697 y->right=z->right;
00698 y->parent=z->parent;
00699 y->red=z->red;
00700 z->left->parent=z->right->parent=y;
00701 if (z == z->parent->left) {
00702 z->parent->left=y;
00703 } else {
00704 z->parent->right=y;
00705 }
00706 free(z);
00707 } else {
00708 tree->DestroyKey(y->key);
00709 tree->DestroyInfo(y->info);
00710 if (!(y->red)) RBDeleteFixUp(tree,x);
00711 free(y);
00712 }
00713
00714 #ifdef DEBUG_ASSERT
00715 Assert(!tree->nil->red,"nil not black in RBDelete");
00716 #endif
00717 }
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731 stk_stack* RBEnumerate(rb_red_blk_tree* tree, void* low, void* high) {
00732 stk_stack* enumResultStack;
00733 rb_red_blk_node* nil=tree->nil;
00734 rb_red_blk_node* x=tree->root->left;
00735 rb_red_blk_node* lastBest=nil;
00736
00737 enumResultStack=StackCreate();
00738 while(nil != x) {
00739 if ( 1 == (tree->Compare(x->key,high)) ) {
00740 x=x->left;
00741 } else {
00742 lastBest=x;
00743 x=x->right;
00744 }
00745 }
00746 while ( (lastBest != nil) && (1 != tree->Compare(low,lastBest->key))) {
00747 StackPush(enumResultStack,lastBest);
00748 lastBest=TreePredecessor(tree,lastBest);
00749 }
00750 return(enumResultStack);
00751 }