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 tsearch 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 comparison 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 tsearch, tfind will search for a datum 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 subroutine deletes a node from a binary search tree. It is generalized from Knuth (6.2.2) algorithm D. 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 subroutine returns a pointer to the parent of the deleted node, or a NULL pointer if the node is not found.
The twalk 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 visited. 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.
Notes
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 compared.
Although declared as type pointer-to-character, the value returned should be cast into type pointer-to-element.
Warning: the root argument to twalk is one level of indirection less than the rootp arguments to tsearch and tdelete.
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.
Restrictions
Results are unpredictable if the calling function alters the pointer to the root.
Diagnostics
A NULL pointer is returned by tsearch and tdelete if rootp is NULL on entry.