The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

 NAME

tdelete, tfind, tsearch, twalk - manage a binary search tree

 SYNOPSIS



#include <search.h>

void *tsearch(const void *key, void **rootp,
     int (*compar)(const void *, const void *));
void *tfind(const void *key, void *const *rootp,
     int(*compar)(const void *, const void *));
void *tdelete(const void *key, void **rootp,
     int(*compar)(const void *, const void *));
void twalk(const void *root,
     void (*action)(const void *, VISIT, int));

 DESCRIPTION

The tsearch(), tfind(), tdelete() and twalk() functions manipulate binary search trees. Comparisons are made with a user-supplied routine, the address of which is passed as the compar argument. This routine is called with two arguments, the pointers to the elements being compared. The user-supplied routine must return an integer less than, equal to or greater than 0, according to whether the first argument is to be considered less than, equal to or greater than the second argument. The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared.

The tsearch() function is used to build and access the tree. The key argument is a pointer to an element to be accessed or stored. If there is a node in the tree whose element is equal to the value pointed to by key, a pointer to this found node is returned. Otherwise, the value pointed to by key is inserted (that is, a new node is created and the value of key is copied to this node), and a pointer to this node returned. Only pointers are copied, so the calling routine must store the data. The rootp argument points to a variable that points to the root node of the tree. A null pointer value for the variable pointed to by rootp denotes an empty tree; in this case, the variable will be set to point to the node which will be at the root of the new tree.

Like tsearch(), tfind() will search for a node in the tree, returning a pointer to it if found. However, if it is not found, tfind() will return a null pointer. The arguments for tfind() are the same as for tsearch().

The tdelete() function deletes a node 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 node was the root of the tree. The tdelete() function returns a pointer to the parent of the deleted node, or a null pointer if the node is not found.

The twalk() function traverses a binary search tree. The root argument is a pointer to the root node of the tree to be traversed. (Any node in a tree may be used as the root for a walk below that node.) The argument action is the name of a routine to be invoked at each node. This routine is, in turn, called with three arguments. The first argument is the address of the node being visited. The structure pointed to by this argument is unspecified and must not be modified by the application, but it is guaranteed that a pointer-to-node can be converted to pointer-to-pointer-to-element to access the element stored in the node. The second argument is a value from an enumeration data type:


typedef enum { preorder, postorder, endorder, leaf } VISIT;

(defined in <search.h>), depending on whether this is the first, second or third time that the node is visited (during a depth-first, left-to-right traversal of the tree), or whether the node is a leaf. The third argument is the level of the node in the tree, with the root being level 0.

If the calling function alters the pointer to the root, the result is undefined.

 RETURN VALUE

If the node is found, both tsearch() and tfind() return a pointer to it. If not, tfind() returns a null pointer, and tsearch() returns a pointer to the inserted item.

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 a null pointer on entry.

The tdelete() function returns a pointer to the parent of the deleted node, or a null pointer if the node is not found.

The twalk() function returns no value.

 ERRORS

No errors are defined.

 EXAMPLES

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 <string.h>
#include <stdio.h>

#define STRSZ    10000
#define NODSZ    500

struct node {      /* pointers to these are stored in the tree */
    char    *string;
    int     length;
};

char   string_space[STRSZ];  /* space to store strings */
struct node nodes[NODSZ];    /* nodes to store */
void  *root = NULL;          /* this points to the root */

int main(int argc, char *argv[])
{
    char   *strptr = string_space;
    struct node    *nodeptr = nodes;
    void   print_node(const void *, VISIT, int);
    int    i = 0, node_compare(const void *, const void *);

    while (gets(strptr) != NULL && i++ < NODSZ)  {
        /* set node */
        nodeptr->string = strptr;
        nodeptr->length = strlen(strptr);
        /* put node into the tree */
        (void) tsearch((void *)nodeptr, (void **)&root,
            node_compare);
        /* adjust pointers, so we do not overwrite tree */
        strptr += nodeptr->length + 1;
        nodeptr++;
    }
    twalk(root, print_node);
    return 0;
}

/*
 *  This routine compares two nodes, based on an
 *  alphabetical ordering of the string field.
 */
int
node_compare(const void *node1, const void *node2)
{
    return strcmp(((const struct node *) node1)->string,
        ((const struct node *) node2)->string);
}

/*
 *  This routine prints out a node, the second time
 *  twalk encounters it or if it is a leaf.
 */
void
print_node(const void *ptr, VISIT order, int level)
{
    const struct node *p = *(const struct node **) ptr;

    if (order == postorder \(or order == leaf)  {
        (void) printf("string = %s,  length = %d\n",
            p->string, p->length);
    }
}

 APPLICATION USAGE

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. The tsearch() function uses preorder, postorder and endorder to refer respectively to visiting a node before any of its children, after its left child and before its right, and after both its children. The alternative nomenclature uses preorder, inorder and postorder to refer to the same visits, which could result in some confusion over the meaning of postorder.

 FUTURE DIRECTIONS

None.

 SEE ALSO

bsearch(), hsearch(), lsearch(), <search.h>.

DERIVATION

Derived from Issue 1 of the SVID.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]