Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ tsearch(S) — Xenix 2.3.4g

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

bsearch(S)

hsearch(S)

lsearch(S)



     TSEARCH(S)               XENIX System V                TSEARCH(S)



     Name
          tsearch, tfind, tdelete, twalk - Manages binary search
          trees.

     Syntax
          #include <search.h>

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

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

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

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

     Description
          The routines tsearch, tfind, tdelete, and twalk manipulate
          binary search trees.  They are generalized from Knuth
          (6.2.2) Algorithms T and D.  All comparisons are done with a
          user-supplied routine.  This routine is called with two
          arguments, the pointers to each of the elements being
          compared.  An integer is returned less than, equal to, or
          greater than 0, corresponding to whether the first argument
          is considered less than, equal to, or greater than the
          second argument.  The comparison function need not compare
          every byte, so other data may be contained in the elements
          in addition to the compared values.

          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 the value pointed to by key
          (*key), a pointer to this datum is returned.  Otherwise,
          *key is inserted, and a pointer to it returned.  The calling
          routine must store data, since only pointers are copied.
          rootp points to a variable that points to the root of the
          tree.  A NULL value for this variable means an empty tree;
          in this case, this variable will be set to point to the
          datum at the root of the new tree.

          tfind will search for a datum in the tree, returning a
          pointer to it if found; however, if the datum is not found,



     Page 1                                           (printed 8/7/87)





     TSEARCH(S)               XENIX System V                TSEARCH(S)



          tfind will return a NULL pointer.  The arguments for tfind
          are the same as for tsearch.

          tdelete deletes a node from a binary search tree.  The
          arguments are the same as for tsearch.  The variable pointed
          to by rootp is changed if the deleted node was the root of
          the tree.  tdelete returns a pointer to the parent of the
          deleted node, or a NULL pointer if the node is not found.

          twalk traverses a binary search tree.  root is the root of
          the tree to be traversed.  Any node in a tree may be used as
          the root for a walk below that node. action is the name of a
          routine to be invoked at each node. action is called with
          three arguments:

          -  the address of the node being visited.

          -  a value from an  enumeration data type typedef enum {
          preorder, post- order, endoder, leaf} VISIT; depending on
          whether this is the first, second, or third time that the
          node has been visited, or whether the node is a leaf.  (This
          data type is defined in the <search.h> header file.)

          -  the level of the node in the tree, with the root being
          level zero.

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

     Examples
          The following code fragment 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 length in alphabetical order:

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

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

               main ( )
               {
                 char *strptr = string_space;



     Page 2                                           (printed 8/7/87)





     TSEARCH(S)               XENIX System V                TSEARCH(S)



                 struct node *nodeptr = nodes;
                 void print_node ( ), twalk( );
                 init i = 0, node_compare( );

                 while (gets(strptr) != NULL && i++ < 500)  {
                    /*set node*/
                    nodeptr->string = strptr;
                    nodeptr->length = strlen(strptr);
                    /*put node into the tree*/
                    (void) tsearch ((char *)nodeptr, &root,
                         node_compare);
                    /*adjust pointers, so we don't overwrite tree*/
                    strptr += nodeptr ->length + 1;
                    nodeptr++;
                 }
                 twalk(root, print_node);
               }
               /*
                 This routine compares two nodes based on an
                 alphabetical ordering of the string field.
               */
               int
               node_compare(node1, node2)
               struct node *node1, *node2;
               {
                 return strcmp(node1->string, node2->string);
               }
               /*
                 This routine prints out a node, the first time
                 twalk encounters it.
               */
               void
               print_node(node, order, level)
               struct node **node;
               VISIT order;
               int level;
               {
                 if (order == preorder || order ==leaf)  {
                    (void)printf(``string = %20s, length = %d\n'',
                      (*node)->string, (*node)->length);
                 }
               }

     See Also
          bsearch(S), hsearch(S), lsearch(S)

     Diagnostics
          A NULL pointer is returned by tsearch if there is not enough
          space available to create a new node.

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



     Page 3                                           (printed 8/7/87)





     TSEARCH(S)               XENIX System V                TSEARCH(S)



          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.

     Warning
          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 nodes are visited. tsearch uses preorder,
          postorder, and endorder to respectively refer to visiting a
          node before any of its children, after its left child and
          before its right, and after both children.  The other
          nomenclatures uses preorder, inorder, and postorder to refer
          to the same visits.

     Notes
          If the calling function alters the pointer to the root,
          results can not be predicted.




































     Page 4                                           (printed 8/7/87)



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