Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ tsearch(3C) — sys5 — Apollo

Media Vault

Software Library

Restoration Projects

Artifacts Sought



TSEARCH(3C)     DOMAIN/IX Reference Manual (SYS5)     TSEARCH(3C)



NAME
     tsearch, tfind, tdelete, twalk - manage binary search trees

USAGE
     #include <search.h>

     char *tsearch ((char *) key, (char **) rootp, compar)
     int (*compar)( );

     char *tfind ((char *) key, (char **) rootp, compar)
     int (*compar)( );

     char *tdelete ((char *) key, (char **) rootp, compar)
     int (*compar)( );

     void twalk ((char *) root, action)
     void (*action)( );

DESCRIPTION
     Tsearch, tfind, tdelete, and twalk are routines for manipu-
     lating binary search trees.  They are generalized from Knuth
     (6.2.2) Algorithms T and D.  All comparisons are performed
     with a user-supplied routine.  This routine is called with
     two arguments, the pointers to the elements being compared.
     It returns an integer less than, equal to, or greater than
     zero, according to whether the first argument is to be con-
     sidered less than, equal to, or greater than the second
     argument.  The comparison function need not compare every
     byte, so arbitrary data can be contained in the elements
     along with the values being compared.

     Tsearch is used to build and access the tree.  Key is a
     pointer to a datum to be accessed or stored.  If there is a
     datum in the tree equal to *key (the value pointed to by
     key), a pointer to this datum is returned.  Otherwise, *key
     is inserted, and a pointer to it returned.  Only pointers
     are copied, so the calling routine must store the data.
     Rootp points to a variable that points to the root of the
     tree.  A NULL value for the variable pointed to by rootp
     denotes an empty tree; in this case, the variable will be
     set to point to the datum which will be at the root of the
     new tree.

     Like tsearch, tfind will search for a datum in the tree,
     returning a pointer to it if it finds it.  However, if it is
     not found, tfind will return a NULL pointer.  The arguments
     for tfind are the same as for tsearch.

     Tdelete deletes an item from a binary search tree.  The
     arguments are the same as for tsearch.  The variable pointed
     to by rootp will be changed if the deleted item was the root
     of the tree.  Tdelete returns a pointer to the parent of the



Printed 6/4/85                                          TSEARCH-1





TSEARCH(3C)     DOMAIN/IX Reference Manual (SYS5)     TSEARCH(3C)



     deleted item, or a NULL pointer if the item is not found.

     Twalk traverses a binary search tree.  Root is the root of
     the tree to be traversed.  (Any item in a tree may be used
     as the root for a walk below that item.) Action is the name
     of a routine to be invoked at each item.  This routine is,
     in turn, called with three arguments.  The first argument is
     the address of the item being visited.  The second argument
     is a value from an enumeration data type ``typedef enum {
     preorder, postorder, endorder, leaf } VISIT;'' (defined in
     the <search.h> header file), depending on whether this is
     the first, second, or third time that the item has been
     visited (during a depth-first, left-to-right traversal of
     the tree), or whether the item is a leaf.  The third argu-
     ment is the level of the item in the tree, with the root
     being level zero.

     The pointers to the key and the root of the tree should be
     of type pointer-to-element, and cast to type pointer-to-
     character.  Similarly, although declared as type pointer-
     to-character, the value returned should be cast into type
     pointer-to-element.

EXAMPLE
     The following code reads in strings and stores structures
     containing a pointer to each string and a count of its
     length.  It then walks the tree, printing out the stored
     strings and their lengths in alphabetical order.

          #include <search.h>
          #include <stdio.h>

          struct item {       /* pointers to these are stored in the tree */
               char *string;
               int length;
          };
          char string_space[10000];     /* space to store strings */
          struct item items[500];       /* items to store */
          struct item *root = NULL;     /* this points to the root */

          main( )
          {
               char *strptr = string_space;
               struct item *itemptr = items;
               void print_item( ), twalk( );
               int i = 0, item_compare( );

               while (gets(strptr) != NULL && i++ < 500)  {
                    /* set item */
                    itemptr->string = strptr;
                    itemptr->length = strlen(strptr);
                    /* put item into the tree */



TSEARCH-2                                          Printed 6/4/85





TSEARCH(3C)     DOMAIN/IX Reference Manual (SYS5)     TSEARCH(3C)



                    (void) tsearch((char *)itemptr, &root,
                           item_compare);
                    /* adjust pointers, so we don't overwrite tree */
                    strptr += itemptr->length + 1;
                    itemptr++;
               }
               twalk(root, print_item);
          }
          /*
               This routine compares two items, based on an
               alphabetical ordering of the string field.
          */
          int
          item_compare(item1, item2)
          struct item *item1, *item2;
          {
               return strcmp(item1->string, item2->string);
          }
          /*
               This routine prints out an item, the first time
               twalk encounters it.
          */



          void
          print_item(item, order, level)
          struct item **item;
          VISIT order;
          int level;
          {
               if (order == preorder || order == leaf)  {
                    (void)printf("string = %20s,  length = %d\n",
                         (*item)->string, (*item)->length);
               }
          }

NOTES
     If the calling function alters the pointer to the root,
     results are unpredictable.

     The root argument to twalk is one level of indirection less
     than the rootp arguments to tsearch and tdelete.

     There are two nomenclatures used to refer to the order in
     which tree items are visited.  Tsearch uses ``preorder, pos-
     torder and endorder'' to respectively refer to visiting an
     item before any of its children, after its left child and
     before its right, and after both its children.  The alter-
     nate nomenclature uses ``preorder, inorder and postorder''
     to refer to the same visits, which could result in some con-
     fusion over the meaning of postorder.



Printed 6/4/85                                          TSEARCH-3





TSEARCH(3C)     DOMAIN/IX Reference Manual (SYS5)     TSEARCH(3C)



DIAGNOSTICS
     Tsearch returns a NULL pointer, if there is not enough space
     available to create a new item.

     A NULL pointer is also returned by tsearch, tfind and
     tdelete if rootp is NULL on entry.

     If the datum is found, both tsearch and tfind return a
     pointer to it.  If not, tfind returns NULL, and tsearch
     returns a pointer to the inserted item.

RELATED INFORMATION
     bsearch(3C), hsearch(3C), lsearch(3C)










































TSEARCH-4                                          Printed 6/4/85



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026