Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > T

tsearch(3C)

HP-UX 11i Version 3: February 2007
» 

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index

NAME

tsearch(), tfind(), tdelete(), twalk() — manage binary search trees

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 *__restrict key, void **__restrict rootp, int (*compar)(const void *, const void *) ); void twalk( const void *root, void (*action)(const void *, VISIT, int) );

DESCRIPTION

tsearch(), tfind(), tdelete(), and twalk() are routines for manipulating binary search trees. They are generalized from Knuth (6.2.2) Algorithms T and D. All comparisons are done with a user-supplied routine, compar. 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 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.

tsearch() is used to build and access the tree. key is a pointer to an entry to be accessed or stored. If there is an entry in the tree equal to the value pointed to by key, a pointer to the previous key associated with this found entry is returned. Otherwise, key is inserted, and a pointer to it returned. Note that since the value returned is a pointer to key and key itself is a pointer, the value returned is a pointer to a pointer. 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 is set to point to the entry which will be at the root of the new tree.

Like tsearch(), tfind() searches for an entry in the tree, returning a pointer to it if found. However, if it is not found, tfind() returns a NULL pointer. The arguments for tfind() are the same as for tsearch().

tdelete() deletes a node from a binary search tree. 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. This routine is, in turn, called with three arguments:

  • First argument is the address of the node being visited.

  • 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 node has been visited (during a depth-first, left-to-right traversal of the tree), or whether the node is a leaf.

  • Third argument is the level of the node in the tree, with the root being level zero.

EXAMPLES

The following code reads 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 <stdlib.h> #include <search.h> #include <stdio.h> #include <string.h> struct element /* pointers to these are stored in the tree */ { char *string; int length; }; char string_space[10000]; /* space to store strings */ struct element elements[500]; /* elements to store */ struct element *root = NULL; /* this points to the root */ void print_node(void *, VISIT, int); int element_compare(const void *, const void *); main( ) { char *strptr = string_space; struct element *element_ptr = elements; struct element **ts_retval; int i = 0; while (gets(strptr) != NULL && i++ < 500) { /* set element */ element_ptr->string = strptr; element_ptr->length = strlen(strptr); /* put element into the tree */ ts_retval = (struct element **) tsearch((void *) element_ptr, (void **) &root, element_compare); if (*ts_retval == element_ptr) { (void) printf("The element \"%s\" ", (*ts_retval)->string); (void) printf("has now been inserted into the tree\n"); } else { (void) printf("The element \"%s\" ", (*ts_retval)->string); (void) printf("already existed in the tree\n"); } /* adjust pointers, so we don't overwrite tree */ strptr += element_ptr->length + 1; element_ptr++; } twalk((void *) root, print_node); } /* This routine compares two elements, based on an alphabetical ordering of the string field. */ int element_compare(elem1, elem2) void *elem1, *elem2; { return strcmp(((struct element *) elem1)->string, ((struct element *) elem2)->string); } /* This routine prints out a node, the first time twalk encounters it. */ void print_node(element, order, level) void *element; VISIT order; int level; { if (order == preorder || order == leaf) { (void) printf("string = %20s, length = %d\n", (*(struct element **) element)->string, (*(struct element **) element)->length); } }

RETURN VALUE

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.

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.

WARNINGS

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

Two nomenclatures are 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 its children. The alternate nomenclature uses preorder, inorder, and postorder to refer to the same visits, which could result in some confusion over the meaning of postorder. If the calling function alters the pointer to the root, results are unpredictable.

STANDARDS CONFORMANCE

tsearch(): AES, SVID2, SVID3, XPG2, XPG3, XPG4

tdelete(): AES, SVID2, SVID3, XPG2, XPG3, XPG4

tfind(): AES, SVID2, SVID3, XPG2, XPG3, XPG4

twalk(): AES, SVID2, SVID3, XPG2, XPG3, XPG4

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.