Ignore:
Timestamp:
Aug 9, 2002, 12:35:28 AM (23 years ago)
Author:
umoeller
Message:

Misc fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/tree.c

    r116 r196  
    312312    if (ul1 < ul2)
    313313        return -1;
     314
    314315    if (ul1 > ul2)
    315316        return +1;
    316     return (0);
     317
     318    return 0;
    317319}
    318320
     
    336338    {
    337339        int i = strcmp(p1, p2);
    338         if (i < 0) return (-1);
    339         if (i > 0) return (+1);
     340        if (i < 0)
     341            return -1;
     342        if (i > 0)
     343            return +1;
    340344    }
    341345    else if (p1)
    342346        // but p2 is NULL: p1 greater than p2 then
    343         return (+1);
     347        return +1;
    344348    else if (p2)
    345349        // but p1 is NULL: p1 less than p2 then
    346         return (-1);
     350        return -1;
    347351
    348352    // return 0 if strcmp returned 0 above or both strings are NULL
    349     return (0);
     353    return 0;
    350354}
    351355
     
    358362                       TREE *x)
    359363{
    360    /**************************
    361     *  rotate node x to left *
    362     **************************/
     364    // rotate node x to left
    363365
    364366    TREE *y = x->right;
     
    372374    if (y != LEAF)
    373375        y->parent = x->parent;
     376
    374377    if (x->parent)
    375378    {
     
    396399                        TREE *x)
    397400{
    398 
    399    /****************************
    400     *  rotate node x to right  *
    401     ****************************/
     401    // rotate node x to right
    402402
    403403    TREE *y = x->left;
     
    411411    if (y != LEAF)
    412412        y->parent = x->parent;
     413
    413414    if (x->parent)
    414415    {
     
    435436                        TREE *x)
    436437{
    437    /*************************************
    438     *  maintain Red-Black tree balance  *
    439     *  after inserting node x           *
    440     *************************************/
    441 
    442438    // check Red-Black properties
    443439    while (    x != *root
     
    503499        }
    504500    }
     501
    505502    (*root)->color = BLACK;
    506503}
     
    546543    {
    547544        int iResult;
    548         if (0 == (iResult = pfnCompare(key, current->ulKey))) // if (compEQ(key, current->key))
     545        if (!(iResult = pfnCompare(key, current->ulKey)))
    549546            return STATUS_DUPLICATE_KEY;
    550547
    551548        parent = current;
    552         current = (iResult < 0)    // compLT(key, current->key)
     549        current = (iResult < 0)
    553550                    ? current->left
    554551                    : current->right;
     
    556553
    557554    // set up new node
    558     /* if ((x = malloc (sizeof(*x))) == 0)
    559         return STATUS_MEM_EXHAUSTED; */
    560555    x->parent = parent;
    561556    x->left = LEAF;
    562557    x->right = LEAF;
    563558    x->color = RED;
    564     // x->key = key;
    565     // x->rec = *rec;
    566559
    567560    // insert node in tree
     
    578571    insertFixup(root,
    579572                x);
    580     // lastFind = NULL;
    581573
    582574    if (plCount)
     
    667659        }
    668660    }
     661
    669662    tree->color = BLACK;
    670 
    671    /*************************************
    672     *  maintain Red-Black tree balance  *
    673     *  after deleting node x            *
    674     *************************************/
    675 
    676     /* while (    x != *root
    677             && x->color == BLACK
    678           )
    679     {
    680         if (x == x->parent->left)
    681         {
    682             TREE *w = x->parent->right;
    683             if (w->color == RED)
    684             {
    685                 w->color = BLACK;
    686                 x->parent->color = RED;
    687                 rotateLeft(root,
    688                            x->parent);
    689                 w = x->parent->right;
    690             }
    691             if (    w->left->color == BLACK
    692                  && w->right->color == BLACK
    693                )
    694             {
    695                 w->color = RED;
    696                 x = x->parent;
    697             }
    698             else
    699             {
    700                 if (w->right->color == BLACK)
    701                 {
    702                     w->left->color = BLACK;
    703                     w->color = RED;
    704                     rotateRight(root,
    705                                 w);
    706                     w = x->parent->right;
    707                 }
    708                 w->color = x->parent->color;
    709                 x->parent->color = BLACK;
    710                 w->right->color = BLACK;
    711                 rotateLeft(root,
    712                            x->parent);
    713                 x = *root;
    714             }
    715         }
    716         else
    717         {
    718             TREE *w = x->parent->left;
    719             if (w->color == RED)
    720             {
    721                 w->color = BLACK;
    722                 x->parent->color = RED;
    723                 rotateRight(root,
    724                             x->parent);
    725                 w = x->parent->left;
    726             }
    727             if (    w->right->color == BLACK
    728                  && w->left->color == BLACK
    729                )
    730             {
    731                 w->color = RED;
    732                 x = x->parent;
    733             }
    734             else
    735             {
    736                 if (w->left->color == BLACK)
    737                 {
    738                     w->right->color = BLACK;
    739                     w->color = RED;
    740                     rotateLeft(root,
    741                                w);
    742                     w = x->parent->left;
    743                 }
    744                 w->color = x->parent->color;
    745                 x->parent->color = BLACK;
    746                 w->left->color = BLACK;
    747                 rotateRight(root,
    748                             x->parent);
    749                 x = *root;
    750             }
    751         }
    752     }
    753     x->color = BLACK; */
    754663}
    755664
     
    803712    if (y != LEAF)
    804713        y->parent = d->parent;
     714
    805715    if (d->parent)
    806716    {
     
    863773               FNTREE_COMPARE *pfnCompare)    // in: comparison func
    864774{
    865    /*******************************
    866     *  find node containing data  *
    867     *******************************/
    868 
    869775    TREE *current = root;
    870776    while (current != LEAF)
    871777    {
    872778        int iResult;
    873         if (0 == (iResult = pfnCompare(key, current->ulKey)))
    874             return (current);
    875         else
    876         {
    877             current = (iResult < 0) // compLT (key, current->key)
    878                 ? current->left
    879                 : current->right;
    880         }
     779        if (!(iResult = pfnCompare(key, current->ulKey)))
     780            return current;
     781
     782        current = (iResult < 0)
     783            ? current->left
     784            : current->right;
    881785    }
    882786
     
    957861    p = r;
    958862    if (p->right != LEAF)
    959         return treeFirst (p->right);
    960     else
    961     {
    962         p = r;
    963         child   = LEAF;
    964         while (    (p->parent)
    965                 && (p->right == child)
    966               )
    967         {
    968             child = p;
    969             p = p->parent;
    970         }
    971         if (p->right != child)
    972             return p;
    973         else
    974             return NULL;
    975     }
     863        return treeFirst(p->right);
     864
     865    p = r;
     866    child   = LEAF;
     867    while (    (p->parent)
     868            && (p->right == child)
     869          )
     870    {
     871        child = p;
     872        p = p->parent;
     873    }
     874
     875    if (p->right != child)
     876        return p;
     877
     878    return NULL;
    976879}
    977880
     
    987890
    988891    if (    (!r)
    989          || (r == LEAF))
     892         || (r == LEAF)
     893       )
    990894        return NULL;
    991895
     
    993897    if (p->left != LEAF)
    994898        return treeLast (p->left);
    995     else
    996     {
    997         p = r;
    998         child   = LEAF;
    999         while ((p->parent)
    1000            &&  (p->left == child))
    1001         {
    1002             child = p;
    1003             p = p->parent;
    1004         }
    1005         if (p->left != child)
    1006             return p;
    1007         else
    1008             return NULL;
    1009     }
     899
     900    p = r;
     901    child   = LEAF;
     902    while (    (p->parent)
     903            && (p->left == child)
     904          )
     905    {
     906        child = p;
     907        p = p->parent;
     908    }
     909
     910    if (p->left != child)
     911        return p;
     912
     913    return NULL;
    1010914}
    1011915
Note: See TracChangeset for help on using the changeset viewer.