home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OBJASM.ZIP / OUFIND.C < prev    next >
C/C++ Source or Header  |  1990-11-15  |  2KB  |  51 lines

  1. #include <stdio.h>
  2. #include "o.h"
  3.  
  4. char *find( data, root_node, cmp_routine, node_ptr )
  5.     char    *data;                              /* pointer to data to find */
  6.     NODE_T  *root_node;                         /* pointer to root node    */
  7.     int     (*cmp_routine)( void *, void * );   /* routine used to compare */
  8.                                                 /* values at two nodes     */
  9.     NODE_T  **node_ptr;                         /* Ptr to node found       */
  10. {
  11.     NODE_T  *curr_node;
  12.     NODE_T  *child_node;
  13.     int     curr_direct;
  14.  
  15.     /* Prepare initial value for tree traversal */
  16.     child_node = root_node;
  17.  
  18.     /*
  19.     **  Traverse tree looking for the place to insert this new node.  If
  20.     **  we find a node which gives a comparison result of 0 (equal), then
  21.     **  we cannot insert this new node.  To indicate this duplicate value,
  22.     **  a non-zero value is returned (the value happens to be a pointer to
  23.     **  node which contains the duplicate value).
  24.     */
  25.     do  {
  26.         curr_node = child_node;
  27.  
  28.         /*  Compare this new node with the node at this position in the
  29.         **  tree and determine which direction to proceed from here.
  30.         */
  31.         curr_direct = (* cmp_routine)( curr_node->data, data );
  32.  
  33.         /*  is this node is a duplicate node?
  34.         */
  35.         if ( node_ptr && curr_direct == RIGHT ) {
  36.             *node_ptr = curr_node;
  37.         }
  38.         if ( curr_direct == EQUAL ) {
  39.             if ( node_ptr ) {
  40.                 *node_ptr = curr_node;
  41.             }
  42.             return ( curr_node->data );
  43.         }
  44.  
  45.         child_node = curr_node->ptr[curr_direct];
  46.  
  47.     } while ( !curr_node->thread[curr_direct]);
  48.  
  49.     return( NULL );
  50. }
  51.