Ignore:
Timestamp:
Oct 23, 2001, 11:25:46 PM (24 years ago)
Author:
umoeller
Message:

Misc updates.

File:
1 edited

Legend:

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

    r106 r113  
    285285 *      initializes the root of a tree.
    286286 *
    287  */
    288 
    289 void treeInit(TREE **root)
     287 *@@changed V0.9.16 (2001-10-19) [umoeller]: added plCount
     288 */
     289
     290void treeInit(TREE **root,
     291              PLONG plCount)            // out: tree item count, set to 0 (ptr can be NULL)
    290292{
    291293    *root = LEAF;
     294
     295    if (plCount)
     296        *plCount = 0;       // V0.9.16 (2001-10-19) [umoeller]
    292297}
    293298
     
    511516 *      STATUS_DUPLICATE_KEY if a node with the
    512517 *      same ulKey already exists.
     518 *
     519 *@@changed V0.9.16 (2001-10-19) [umoeller]: added plCount
    513520 */
    514521
    515522int treeInsert(TREE **root,                     // in: root of the tree
     523               PLONG plCount,                 // in/out: item count (ptr can be NULL)
    516524               TREE *x,                         // in: new node to insert
    517525               FNTREE_COMPARE *pfnCompare)      // in: comparison func
     
    531539        if (0 == (iResult = pfnCompare(key, current->ulKey))) // if (compEQ(key, current->key))
    532540            return STATUS_DUPLICATE_KEY;
     541
    533542        parent = current;
    534543        current = (iResult < 0)    // compLT(key, current->key)
     
    561570                x);
    562571    // lastFind = NULL;
     572
     573    if (plCount)
     574        (*plCount)++;       // V0.9.16 (2001-10-19) [umoeller]
    563575
    564576    return STATUS_OK;
     
    740752 *      Returns 0 if the node was deleted or
    741753 *      STATUS_INVALID_NODE if not.
     754 *
     755 *@@changed V0.9.16 (2001-10-19) [umoeller]: added plCount
    742756 */
    743757
    744758int treeDelete(TREE **root,         // in: root of the tree
     759               PLONG plCount,     // in/out: item count (ptr can be NULL)
    745760               TREE *tree)          // in: tree node to delete
    746761{
     
    820835                    y);
    821836
     837    if (plCount)
     838        (*plCount)--;       // V0.9.16 (2001-10-19) [umoeller]
     839
    822840    return (STATUS_OK);
    823 
    824     /* TREE    *x,
    825             *y; */
    826             // *z;
    827 
    828    /*****************************
    829     *  delete node z from tree  *
    830     *****************************/
    831 
    832     // find node in tree
    833     /* if (lastFind && compEQ(lastFind->key, key))
    834         // if we just found node, use pointer
    835         z = lastFind;
    836     else {
    837         z = *root;
    838         while(z != LEAF)
    839         {
    840             int iResult = pfnCompare(key, z->key);
    841             if (iResult == 0)
    842             // if(compEQ(key, z->key))
    843                 break;
    844             else
    845                 z = (iResult < 0) // compLT(key, z->key)
    846                     ? z->left
    847                     : z->right;
    848         }
    849         if (z == LEAF)
    850             return STATUS_KEY_NOT_FOUND;
    851     }
    852 
    853     if (    z->left == LEAF
    854          || z->right == LEAF
    855        )
    856     {
    857         // y has a LEAF node as a child
    858         y = z;
    859     }
    860     else
    861     {
    862         // find tree successor with a LEAF node as a child
    863         y = z->right;
    864         while (y->left != LEAF)
    865             y = y->left;
    866     }
    867 
    868     // x is y's only child
    869     if (y->left != LEAF)
    870         x = y->left;
    871     else
    872         x = y->right;
    873 
    874     // remove y from the parent chain
    875     x->parent = y->parent;
    876     if (y->parent)
    877         if (y == y->parent->left)
    878             y->parent->left = x;
    879         else
    880             y->parent->right = x;
    881     else
    882         *root = x;
    883 
    884     // y is about to be deleted...
    885 
    886     if (y != z)
    887     {
    888         // now, the original code simply copied the data
    889         // from y to z... we can't safely do that since
    890         // we don't know about the real data in the
    891         // caller's TREE structure
    892         z->ulKey = y->ulKey;
    893         // z->rec = y->rec;         // hope this works...
    894                                     // the original implementation used rec
    895                                     // for the node's data
    896 
    897         if (cbStruct > sizeof(TREE))
    898         {
    899             memcpy(((char*)&z) + sizeof(TREE),
    900                    ((char*)&y) + sizeof(TREE),
    901                    cbStruct - sizeof(TREE));
    902         }
    903     }
    904 
    905     if (y->color == BLACK)
    906         deleteFixup(root,
    907                     x);
    908 
    909     // free(y);
    910     // lastFind = NULL;
    911 
    912     return STATUS_OK; */
    913841}
    914842
     
    11201048
    11211049TREE** treeBuildArray(TREE* pRoot,
    1122                       unsigned long *pulCount)  // in: item count, out: array item count
     1050                      PLONG plCount)  // in: item count, out: array item count
    11231051{
    11241052    TREE            **papNodes = NULL,
    11251053                    **papThis = NULL;
    1126     unsigned long   cb = (sizeof(TREE*) * (*pulCount)),
     1054    long            cb = (sizeof(TREE*) * (*plCount)),
    11271055                    cNodes = 0;
    11281056
     
    11401068            // copy nodes to array
    11411069            while (    pNode
    1142                     && cNodes < (*pulCount)     // just to make sure
     1070                    && cNodes < (*plCount)     // just to make sure
    11431071                  )
    11441072            {
     
    11511079
    11521080            // output count
    1153             *pulCount = cNodes;
     1081            *plCount = cNodes;
    11541082        }
    11551083    }
Note: See TracChangeset for help on using the changeset viewer.