tsearch(3)tsearch(3)Name
tsearch, tfind, tdelete, twalk - manage binary search trees
Syntax
#include <search.h>
void *tsearch (key, rootp, compar)
void *key;
void **rootp;
int (*compar)( );
void *tfind (key, rootp, compar)
void *key;
void **rootp;
int (*compar)( );
void *tdelete (key, rootp, compar)
void *key;
void **rootp;
int (*compar)( );
void twalk (root, action)
void * root;
void (*action)( );
Description
The subroutine is a binary tree search routine generalized from Knuth
(6.2.2) Algorithm T. It returns a pointer into a tree indicating where
a datum may be found. If the datum does not occur, it is added at an
appropriate point in the tree. The key points to the datum to be
sought in the tree. The rootp points to a variable that points to the
root of the tree. A NULL pointer value for the variable denotes an
empty tree; in this case, the variable will be set to point to the
datum at the root of the new tree. The compar is the name of the com‐
parison function. It is called with two arguments that point to the
elements being compared. The function must return an integer less
than, equal to, or greater than zero according as the first argument is
to be considered less than, equal to, or greater than the second.
Like will search for a datum in the tree, returning a pointer to it if
found. However, if it is not found, will return a NULL pointer. The
arguments for are the same as for
The subroutine deletes a node from a binary search tree. It is gener‐
alized from Knuth (6.2.2) algorithm D. The arguments are the same as
for The variable pointed to by rootp will be changed if the deleted
node was the root of the tree. The subroutine returns a pointer to the
parent of the deleted node, or a NULL pointer if the node is not found.
The subroutine traverses a binary search tree. The 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.) The 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 vis‐
ited. 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 node has been 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 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.
The comparison function need not compare every byte, so arbitrary data
may be contained in the elements in addition to the values being com‐
pared.
Although declared as type pointer-to-character, the value returned
should be cast into type pointer-to-element.
Note that the root argument to is one level of indirection less than
the rootp arguments to and
Return Values
A NULL pointer is returned by if there is not enough space available to
create a new node.
A NULL pointer is returned by and if rootp is NULL on entry.
If the datum is found, both and return a pointer to it. If not,
returns NULL, and returns a pointer to the inserted item.
Restrictions
Results are unpredictable if the calling function alters the pointer to
the root.
Diagnostics
A NULL pointer is returned by and if rootp is NULL on entry.
See Alsobsearch(3), hsearch(3), lsearch(3)tsearch(3)