TSEARCH(3C) COMMAND REFERENCE TSEARCH(3C)
NAME
tsearch, tdelete, twalk - manage binary search trees
SYNOPSIS
#include <search.h>
char *tsearch ((char *) key, (char **) rootp, compar)
int (*compar)( );
char *tdelete ((char *) key, (char **) rootp, compar)
int (*compar)( );
void twalk ((char *) root, action)
void (*action)( );
DESCRIPTION
Tsearch 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.
Key points to the datum to be sought in the tree.
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.
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.
Tdelete 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. 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.
The first argument is the address of the node being visited.
Printed 3/13/89 1
TSEARCH(3C) COMMAND REFERENCE TSEARCH(3C)
NOTE: This is not the address of the data in the
tree; it is a pointer to that address.
The second argument is a value from an enumeration data
type:
typedef enum { preorder, postorder, endorder, leaf }
VISIT;
It is 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 0.
DIAGNOSTICS
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 and tdelete if rootp
is NULL on entry.
CAVEATS
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.nentry.
Awful things can happen if the calling function alters the
pointer to the root.
SEE ALSO
bsearch(3c), hsearch(3c), intro(3), and lsearch(3c).
Printed 3/13/89 2
%%index%%
na:336,104;
sy:440,2176;
de:2616,2140;5164,685;
di:5849,540;
ca:6389,1074;
se:7463,210;
%%index%%000000000121