tsearch.3c (2010 09)
t
tsearch(3C) tsearch (3C)
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. How-
ever, 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 (dur-
ing 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.
HP-UX 11i Version 3: September 2010 − 1 − Hewlett-Packard Company 1