tsearch
function.Another common form to organize data for efficient search is to use
trees. The tsearch
function family provides a nice interface to
functions to organize possibly large amounts of data by providing a mean
access time proportional to the logarithm of the number of elements.
The GNU C Library implementation even guarantees that this bound is
never exceeded even for input data which cause problems for simple
binary tree implementations.
The functions described in the chapter are all described in the System V and X/Open specifications and are therefore quite portable.
In contrast to the hsearch
functions the tsearch
functions
can be used with arbitrary data and not only zero-terminated strings.
The tsearch
functions have the advantage that no function to
initialize data structures is necessary. A simple pointer of type
void *
initialized to NULL
is a valid tree and can be
extended or searched. The prototypes for these functions can be found
in the header file search.h.
The
tsearch
function searches in the tree pointed to by*
rootp for an element matching key. The function pointed to by compar is used to determine whether two elements match. See Comparison Functions, for a specification of the functions which can be used for the compar parameter.If the tree does not contain a matching entry the key value will be added to the tree.
tsearch
does not make a copy of the object pointed to by key (how could it since the size is unknown). Instead it adds a reference to this object which means the object must be available as long as the tree data structure is used.The tree is represented by a pointer to a pointer since it is sometimes necessary to change the root node of the tree. So it must not be assumed that the variable pointed to by rootp has the same value after the call. This also shows that it is not safe to call the
tsearch
function more than once at the same time using the same tree. It is no problem to run it more than once at a time on different trees.The return value is a pointer to the matching element in the tree. If a new element was created the pointer points to the new data (which is in fact key). If an entry had to be created and the program ran out of space
NULL
is returned.
The
tfind
function is similar to thetsearch
function. It locates an element matching the one pointed to by key and returns a pointer to this element. But if no matching element is available no new element is entered (note that the rootp parameter points to a constant pointer). Instead the function returnsNULL
.
Another advantage of the tsearch
function in contrast to the
hsearch
functions is that there is an easy way to remove
elements.
To remove a specific element matching key from the tree
tdelete
can be used. It locates the matching element using the same method astfind
. The corresponding element is then removed and a pointer to the parent of the deleted node is returned by the function. If there is no matching entry in the tree nothing can be deleted and the function returnsNULL
. If the root of the tree is deletedtdelete
returns some unspecified value not equal toNULL
.
If the complete search tree has to be removed one can use
tdestroy
. It frees all resources allocated by thetsearch
function to generate the tree pointed to by vroot.For the data in each tree node the function freefct is called. The pointer to the data is passed as the argument to the function. If no such work is necessary freefct must point to a function doing nothing. It is called in any case.
This function is a GNU extension and not covered by the System V or X/Open specifications.
In addition to the function to create and destroy the tree data structure, there is another function which allows you to apply a function to all elements of the tree. The function must have this type:
void __action_fn_t (const void *nodep, VISIT value, int level);
The nodep is the data value of the current node (once given as the
key argument to tsearch
). level is a numeric value
which corresponds to the depth of the current node in the tree. The
root node has the depth 0 and its children have a depth of
1 and so on. The VISIT
type is an enumeration type.
The
VISIT
value indicates the status of the current node in the tree and how the function is called. The status of a node is either `leaf' or `internal node'. For each leaf node the function is called exactly once, for each internal node it is called three times: before the first child is processed, after the first child is processed and after both children are processed. This makes it possible to handle all three methods of tree traversal (or even a combination of them).
preorder
- The current node is an internal node and the function is called before the first child was processed.
postorder
- The current node is an internal node and the function is called after the first child was processed.
endorder
- The current node is an internal node and the function is called after the second child was processed.
leaf
- The current node is a leaf.
For each node in the tree with a node pointed to by root, the
twalk
function calls the function provided by the parameter action. For leaf nodes the function is called exactly once with value set toleaf
. For internal nodes the function is called three times, setting the value parameter or action to the appropriate value. The level argument for the action function is computed while descending the tree with increasing the value by one for the descend to a child, starting with the value 0 for the root node.Since the functions used for the action parameter to
twalk
must not modify the tree data, it is safe to runtwalk
in more than one thread at the same time, working on the same tree. It is also safe to calltfind
in parallel. Functions which modify the tree must not be used, otherwise the behavior is undefined.