1 @node Searching and Sorting, Pattern Matching, Message Translation, Top
2 @c %MENU% General searching and sorting functions
3 @chapter Searching and Sorting
5 This chapter describes functions for searching and sorting arrays of
6 arbitrary objects. You pass the appropriate comparison function to be
7 applied as an argument, along with the size of the objects in the array
8 and the total number of elements.
11 * Comparison Functions:: Defining how to compare two objects.
12 Since the sort and search facilities
13 are general, you have to specify the
15 * Array Search Function:: The @code{bsearch} function.
16 * Array Sort Function:: The @code{qsort} function.
17 * Search/Sort Example:: An example program.
18 * Hash Search Function:: The @code{hsearch} function.
19 * Tree Search Function:: The @code{tsearch} function.
22 @node Comparison Functions
23 @section Defining the Comparison Function
24 @cindex Comparison Function
26 In order to use the sorted array library functions, you have to describe
27 how to compare the elements of the array.
29 To do this, you supply a comparison function to compare two elements of
30 the array. The library will call this function, passing as arguments
31 pointers to two array elements to be compared. Your comparison function
32 should return a value the way @code{strcmp} (@pxref{String/Array
33 Comparison}) does: negative if the first argument is ``less'' than the
34 second, zero if they are ``equal'', and positive if the first argument
37 Here is an example of a comparison function which works with an array of
38 numbers of type @code{long int}:
42 compare_long_ints (const void *a, const void *b)
44 const long int *la = a;
45 const long int *lb = b;
47 return (*la > *lb) - (*la < *lb);
51 (The code would have to be more complicated for an array of @code{double},
52 to handle NaNs correctly.)
54 The header file @file{stdlib.h} defines a name for the data type of
55 comparison functions. This type is a GNU extension.
59 @tindex comparison_fn_t
61 int comparison_fn_t (const void *, const void *);
64 @node Array Search Function
65 @section Array Search Function
66 @cindex search function (for arrays)
67 @cindex binary search function (for arrays)
68 @cindex array search function
70 Generally searching for a specific element in an array means that
71 potentially all elements must be checked. @Theglibc{} contains
72 functions to perform linear search. The prototypes for the following
73 two functions can be found in @file{search.h}.
75 @deftypefun {void *} lfind (const void *@var{key}, const void *@var{base}, size_t *@var{nmemb}, size_t @var{size}, comparison_fn_t @var{compar})
76 @standards{SVID, search.h}
77 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
78 The @code{lfind} function searches in the array with @code{*@var{nmemb}}
79 elements of @var{size} bytes pointed to by @var{base} for an element
80 which matches the one pointed to by @var{key}. The function pointed to
81 by @var{compar} is used to decide whether two elements match.
83 The return value is a pointer to the matching element in the array
84 starting at @var{base} if it is found. If no matching element is
85 available @code{NULL} is returned.
87 The mean runtime of this function is @code{*@var{nmemb}}/2. This
88 function should only be used if elements often get added to or deleted from
89 the array in which case it might not be useful to sort the array before
93 @deftypefun {void *} lsearch (const void *@var{key}, void *@var{base}, size_t *@var{nmemb}, size_t @var{size}, comparison_fn_t @var{compar})
94 @standards{SVID, search.h}
95 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
96 @c A signal handler that interrupted an insertion and performed an
97 @c insertion itself would leave the array in a corrupt state (e.g. one
98 @c new element initialized twice, with parts of both initializations
99 @c prevailing, and another uninitialized element), but this is just a
100 @c special case of races on user-controlled objects, that have to be
103 @c In case of cancellation, we know the array won't be left in a corrupt
104 @c state; the new element is initialized before the element count is
105 @c incremented, and the compiler can't reorder these operations because
106 @c it can't know that they don't alias. So, we'll either cancel after
107 @c the increment and the initialization are both complete, or the
108 @c increment won't have taken place, and so how far the initialization
109 @c got doesn't matter.
110 The @code{lsearch} function is similar to the @code{lfind} function. It
111 searches the given array for an element and returns it if found. The
112 difference is that if no matching element is found the @code{lsearch}
113 function adds the object pointed to by @var{key} (with a size of
114 @var{size} bytes) at the end of the array and it increments the value of
115 @code{*@var{nmemb}} to reflect this addition.
117 This means for the caller that if it is not sure that the array contains
118 the element one is searching for the memory allocated for the array
119 starting at @var{base} must have room for at least @var{size} more
120 bytes. If one is sure the element is in the array it is better to use
121 @code{lfind} so having more room in the array is always necessary when
122 calling @code{lsearch}.
125 To search a sorted array for an element matching the key, use the
126 @code{bsearch} function. The prototype for this function is in
127 the header file @file{stdlib.h}.
130 @deftypefun {void *} bsearch (const void *@var{key}, const void *@var{array}, size_t @var{count}, size_t @var{size}, comparison_fn_t @var{compare})
131 @standards{ISO, stdlib.h}
132 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
133 The @code{bsearch} function searches the sorted array @var{array} for an object
134 that is equivalent to @var{key}. The array contains @var{count} elements,
135 each of which is of size @var{size} bytes.
137 The @var{compare} function is used to perform the comparison. This
138 function is called with two pointer arguments and should return an
139 integer less than, equal to, or greater than zero corresponding to
140 whether its first argument is considered less than, equal to, or greater
141 than its second argument. The elements of the @var{array} must already
142 be sorted in ascending order according to this comparison function.
144 The return value is a pointer to the matching array element, or a null
145 pointer if no match is found. If the array contains more than one element
146 that matches, the one that is returned is unspecified.
148 This function derives its name from the fact that it is implemented
149 using the binary search algorithm.
152 @node Array Sort Function
153 @section Array Sort Function
154 @cindex sort function (for arrays)
155 @cindex quick sort function (for arrays)
156 @cindex array sort function
158 To sort an array using an arbitrary comparison function, use the
159 @code{qsort} function. The prototype for this function is in
163 @deftypefun void qsort (void *@var{array}, size_t @var{count}, size_t @var{size}, comparison_fn_t @var{compare})
164 @standards{ISO, stdlib.h}
165 @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@acucorrupt{}}}
166 The @code{qsort} function sorts the array @var{array}. The array
167 contains @var{count} elements, each of which is of size @var{size}.
169 The @var{compare} function is used to perform the comparison on the
170 array elements. This function is called with two pointer arguments and
171 should return an integer less than, equal to, or greater than zero
172 corresponding to whether its first argument is considered less than,
173 equal to, or greater than its second argument.
175 @cindex stable sorting
176 @strong{Warning:} If two objects compare as equal, their order after
177 sorting is unpredictable. That is to say, the sorting is not stable.
178 This can make a difference when the comparison considers only part of
179 the elements. Two elements with the same sort key may differ in other
182 Although the object addresses passed to the comparison function lie
183 within the array, they need not correspond with the original locations
184 of those objects because the sorting algorithm may swap around objects
185 in the array before making some comparisons. The only way to perform
186 a stable sort with @code{qsort} is to first augment the objects with a
187 monotonic counter of some kind.
189 Here is a simple example of sorting an array of @code{long int} in numerical
190 order, using the comparison function defined above (@pxref{Comparison
198 qsort (array, nmemb, sizeof *array, compare_long_ints);
202 The @code{qsort} function derives its name from the fact that it was
203 originally implemented using the ``quick sort'' algorithm.
205 The implementation of @code{qsort} attempts to allocate auxiliary storage
206 and use the merge sort algorithm, without violating C standard requirement
207 that arguments passed to the comparison function point within the array.
210 @node Search/Sort Example
211 @section Searching and Sorting Example
213 Here is an example showing the use of @code{qsort} and @code{bsearch}
214 with an array of structures. The objects in the array are sorted
215 by comparing their @code{name} fields with the @code{strcmp} function.
216 Then, we can look up individual objects based on their names.
218 @comment This example is dedicated to the memory of Jim Henson. RIP.
220 @include search.c.texi
223 @cindex Kermit the frog
224 The output from this program looks like:
235 Sweetums, the monster
236 Dr. Strangepork, the pig
237 Link Hogthrob, the pig
239 Dr. Bunsen Honeydew, the human
241 Swedish Chef, the human
246 Dr. Bunsen Honeydew, the human
247 Dr. Strangepork, the pig
251 Link Hogthrob, the pig
255 Swedish Chef, the human
256 Sweetums, the monster
261 Couldn't find Janice.
265 @node Hash Search Function
266 @section The @code{hsearch} function.
268 The functions mentioned so far in this chapter are for searching in a sorted
269 or unsorted array. There are other methods to organize information
270 which later should be searched. The costs of insert, delete and search
271 differ. One possible implementation is using hashing tables.
272 The following functions are declared in the header file @file{search.h}.
274 @deftypefun int hcreate (size_t @var{nel})
275 @standards{SVID, search.h}
276 @safety{@prelim{}@mtunsafe{@mtasurace{:hsearch}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
277 @c hcreate @mtasurace:hsearch @ascuheap @acucorrupt @acsmem
278 @c hcreate_r dup @mtsrace:htab @ascuheap @acucorrupt @acsmem
279 The @code{hcreate} function creates a hashing table which can contain at
280 least @var{nel} elements. There is no possibility to grow this table so
281 it is necessary to choose the value for @var{nel} wisely. The method
282 used to implement this function might make it necessary to make the
283 number of elements in the hashing table larger than the expected maximal
284 number of elements. Hashing tables usually work inefficiently if they are
285 filled 80% or more. The constant access time guaranteed by hashing can
286 only be achieved if few collisions exist. See Knuth's ``The Art of
287 Computer Programming, Part 3: Searching and Sorting'' for more
290 The weakest aspect of this function is that there can be at most one
291 hashing table used through the whole program. The table is allocated
292 in local memory out of control of the programmer. As an extension @theglibc{}
293 provides an additional set of functions with a reentrant
294 interface which provides a similar interface but which allows keeping
295 arbitrarily many hashing tables.
297 It is possible to use more than one hashing table in the program run if
298 the former table is first destroyed by a call to @code{hdestroy}.
300 The function returns a non-zero value if successful. If it returns zero,
301 something went wrong. This could either mean there is already a hashing
302 table in use or the program ran out of memory.
305 @deftypefun void hdestroy (void)
306 @standards{SVID, search.h}
307 @safety{@prelim{}@mtunsafe{@mtasurace{:hsearch}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
308 @c hdestroy @mtasurace:hsearch @ascuheap @acucorrupt @acsmem
309 @c hdestroy_r dup @mtsrace:htab @ascuheap @acucorrupt @acsmem
310 The @code{hdestroy} function can be used to free all the resources
311 allocated in a previous call of @code{hcreate}. After a call to this
312 function it is again possible to call @code{hcreate} and allocate a new
313 table with possibly different size.
315 It is important to remember that the elements contained in the hashing
316 table at the time @code{hdestroy} is called are @emph{not} freed by this
317 function. It is the responsibility of the program code to free those
318 strings (if necessary at all). Freeing all the element memory is not
319 possible without extra, separately kept information since there is no
320 function to iterate through all available elements in the hashing table.
321 If it is really necessary to free a table and all elements the
322 programmer has to keep a list of all table elements and before calling
323 @code{hdestroy} s/he has to free all element's data using this list.
324 This is a very unpleasant mechanism and it also shows that this kind of
325 hashing table is mainly meant for tables which are created once and
326 used until the end of the program run.
329 Entries of the hashing table and keys for the search are defined using
332 @deftp {Data type} ENTRY
335 Pointer to a zero-terminated string of characters describing the key for
336 the search or the element in the hashing table.
338 This is a limiting restriction of the functionality of the
339 @code{hsearch} functions: They can only be used for data sets which
340 use the NUL character always and solely to terminate keys. It is not
341 possible to handle general binary data for keys.
344 Generic pointer for use by the application. The hashing table
345 implementation preserves this pointer in entries, but does not use it
346 in any way otherwise.
350 @deftp {Data type} {struct entry}
351 The underlying type of @code{ENTRY}.
354 @deftypefun {ENTRY *} hsearch (ENTRY @var{item}, ACTION @var{action})
355 @standards{SVID, search.h}
356 @safety{@prelim{}@mtunsafe{@mtasurace{:hsearch}}@asunsafe{}@acunsafe{@acucorrupt{/action==ENTER}}}
357 @c hsearch @mtasurace:hsearch @acucorrupt/action==ENTER
358 @c hsearch_r dup @mtsrace:htab @acucorrupt/action==ENTER
359 To search in a hashing table created using @code{hcreate} the
360 @code{hsearch} function must be used. This function can perform a simple
361 search for an element (if @var{action} has the value @code{FIND}) or it can
362 alternatively insert the key element into the hashing table. Entries
365 The key is denoted by a pointer to an object of type @code{ENTRY}. For
366 locating the corresponding position in the hashing table only the
367 @code{key} element of the structure is used.
369 If an entry with a matching key is found the @var{action} parameter is
370 irrelevant. The found entry is returned. If no matching entry is found
371 and the @var{action} parameter has the value @code{FIND} the function
372 returns a @code{NULL} pointer. If no entry is found and the
373 @var{action} parameter has the value @code{ENTER} a new entry is added
374 to the hashing table which is initialized with the parameter @var{item}.
375 A pointer to the newly added entry is returned.
378 As mentioned before, the hashing table used by the functions described so
379 far is global and there can be at any time at most one hashing table in
380 the program. A solution is to use the following functions which are a
381 GNU extension. All have in common that they operate on a hashing table
382 which is described by the content of an object of the type @code{struct
383 hsearch_data}. This type should be treated as opaque, none of its
384 members should be changed directly.
386 @deftypefun int hcreate_r (size_t @var{nel}, struct hsearch_data *@var{htab})
387 @standards{GNU, search.h}
388 @safety{@prelim{}@mtsafe{@mtsrace{:htab}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
389 @c Unlike the lsearch array, the htab is (at least in part) opaque, so
390 @c let's make it absolutely clear that ensuring exclusive access is a
391 @c caller responsibility.
393 @c Cancellation is unlikely to leave the htab in a corrupt state: the
394 @c last field to be initialized is the one that tells whether the entire
395 @c data structure was initialized, and there's a function call (calloc)
396 @c in between that will often ensure all other fields are written before
397 @c the table. However, should this call be inlined (say with LTO), this
398 @c assumption may not hold. The calloc call doesn't cross our library
399 @c interface barrier, so let's consider this could happen and mark this
400 @c with @acucorrupt. It's no safety loss, since we already have
401 @c @ascuheap anyway...
403 @c hcreate_r @mtsrace:htab @ascuheap @acucorrupt @acsmem
405 @c calloc dup @ascuheap @acsmem
406 The @code{hcreate_r} function initializes the object pointed to by
407 @var{htab} to contain a hashing table with at least @var{nel} elements.
408 So this function is equivalent to the @code{hcreate} function except
409 that the initialized data structure is controlled by the user.
411 This allows having more than one hashing table at one time. The memory
412 necessary for the @code{struct hsearch_data} object can be allocated
413 dynamically. It must be initialized with zero before calling this
416 The return value is non-zero if the operation was successful. If the
417 return value is zero, something went wrong, which probably means the
418 program ran out of memory.
421 @deftypefun void hdestroy_r (struct hsearch_data *@var{htab})
422 @standards{GNU, search.h}
423 @safety{@prelim{}@mtsafe{@mtsrace{:htab}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
424 @c The table is released while the table pointer still points to it.
425 @c Async cancellation is thus unsafe, but it already was because we call
426 @c free(). Using the table in a handler while it's being released would
427 @c also be dangerous, but calling free() already makes it unsafe, and
428 @c the requirement on the caller to ensure exclusive access already
429 @c guarantees this doesn't happen, so we don't get @asucorrupt.
431 @c hdestroy_r @mtsrace:htab @ascuheap @acucorrupt @acsmem
432 @c free dup @ascuheap @acsmem
433 The @code{hdestroy_r} function frees all resources allocated by the
434 @code{hcreate_r} function for this very same object @var{htab}. As for
435 @code{hdestroy} it is the program's responsibility to free the strings
436 for the elements of the table.
439 @deftypefun int hsearch_r (ENTRY @var{item}, ACTION @var{action}, ENTRY **@var{retval}, struct hsearch_data *@var{htab})
440 @standards{GNU, search.h}
441 @safety{@prelim{}@mtsafe{@mtsrace{:htab}}@assafe{}@acunsafe{@acucorrupt{/action==ENTER}}}
442 @c Callers have to ensure mutual exclusion; insertion, if cancelled,
443 @c leaves the table in a corrupt state.
445 @c hsearch_r @mtsrace:htab @acucorrupt/action==ENTER
448 The @code{hsearch_r} function is equivalent to @code{hsearch}. The
449 meaning of the first two arguments is identical. But instead of
450 operating on a single global hashing table the function works on the
451 table described by the object pointed to by @var{htab} (which is
452 initialized by a call to @code{hcreate_r}).
454 Another difference to @code{hcreate} is that the pointer to the found
455 entry in the table is not the return value of the function. It is
456 returned by storing it in a pointer variable pointed to by the
457 @var{retval} parameter. The return value of the function is an integer
458 value indicating success if it is non-zero and failure if it is zero.
459 In the latter case the global variable @code{errno} signals the reason for
464 The table is filled and @code{hsearch_r} was called with a so far
465 unknown key and @var{action} set to @code{ENTER}.
467 The @var{action} parameter is @code{FIND} and no corresponding element
468 is found in the table.
473 @node Tree Search Function
474 @section The @code{tsearch} function.
476 Another common form to organize data for efficient search is to use
477 trees. The @code{tsearch} function family provides a nice interface to
478 functions to organize possibly large amounts of data by providing a mean
479 access time proportional to the logarithm of the number of elements.
480 @Theglibc{} implementation even guarantees that this bound is
481 never exceeded even for input data which cause problems for simple
482 binary tree implementations.
484 The functions described in the chapter are all described in the @w{System
485 V} and X/Open specifications and are therefore quite portable.
487 In contrast to the @code{hsearch} functions the @code{tsearch} functions
488 can be used with arbitrary data and not only zero-terminated strings.
490 The @code{tsearch} functions have the advantage that no function to
491 initialize data structures is necessary. A simple pointer of type
492 @code{void *} initialized to @code{NULL} is a valid tree and can be
493 extended or searched. The prototypes for these functions can be found
494 in the header file @file{search.h}.
496 @deftypefun {void *} tsearch (const void *@var{key}, void **@var{rootp}, comparison_fn_t @var{compar})
497 @standards{SVID, search.h}
498 @safety{@prelim{}@mtsafe{@mtsrace{:rootp}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
499 @c The tree is not modified in a thread-safe manner, and rotations may
500 @c leave the tree in an inconsistent state that could be observed in an
501 @c asynchronous signal handler (except for the caller-synchronization
502 @c requirement) or after asynchronous cancellation of the thread
503 @c performing the rotation or the insertion.
504 The @code{tsearch} function searches in the tree pointed to by
505 @code{*@var{rootp}} for an element matching @var{key}. The function
506 pointed to by @var{compar} is used to determine whether two elements
507 match. @xref{Comparison Functions}, for a specification of the functions
508 which can be used for the @var{compar} parameter.
510 If the tree does not contain a matching entry the @var{key} value will
511 be added to the tree. @code{tsearch} does not make a copy of the object
512 pointed to by @var{key} (how could it since the size is unknown).
513 Instead it adds a reference to this object which means the object must
514 be available as long as the tree data structure is used.
516 The tree is represented by a pointer to a pointer since it is sometimes
517 necessary to change the root node of the tree. So it must not be
518 assumed that the variable pointed to by @var{rootp} has the same value
519 after the call. This also shows that it is not safe to call the
520 @code{tsearch} function more than once at the same time using the same
521 tree. It is no problem to run it more than once at a time on different
524 The return value is a pointer to the matching element in the tree. If a
525 new element was created the pointer points to the new data (which is in
526 fact @var{key}). If an entry had to be created and the program ran out
527 of space @code{NULL} is returned.
530 @deftypefun {void *} tfind (const void *@var{key}, void *const *@var{rootp}, comparison_fn_t @var{compar})
531 @standards{SVID, search.h}
532 @safety{@prelim{}@mtsafe{@mtsrace{:rootp}}@assafe{}@acsafe{}}
533 The @code{tfind} function is similar to the @code{tsearch} function. It
534 locates an element matching the one pointed to by @var{key} and returns
535 a pointer to this element. But if no matching element is available no
536 new element is entered (note that the @var{rootp} parameter points to a
537 constant pointer). Instead the function returns @code{NULL}.
540 Another advantage of the @code{tsearch} functions in contrast to the
541 @code{hsearch} functions is that there is an easy way to remove
544 @deftypefun {void *} tdelete (const void *@var{key}, void **@var{rootp}, comparison_fn_t @var{compar})
545 @standards{SVID, search.h}
546 @safety{@prelim{}@mtsafe{@mtsrace{:rootp}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
547 To remove a specific element matching @var{key} from the tree
548 @code{tdelete} can be used. It locates the matching element using the
549 same method as @code{tfind}. The corresponding element is then removed
550 and a pointer to the parent of the deleted node is returned by the
551 function. If there is no matching entry in the tree nothing can be
552 deleted and the function returns @code{NULL}. If the root of the tree
553 is deleted @code{tdelete} returns some unspecified value not equal to
557 @deftypefun void tdestroy (void *@var{vroot}, __free_fn_t @var{freefct})
558 @standards{GNU, search.h}
559 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
560 If the complete search tree has to be removed one can use
561 @code{tdestroy}. It frees all resources allocated by the @code{tsearch}
562 functions to generate the tree pointed to by @var{vroot}.
564 For the data in each tree node the function @var{freefct} is called.
565 The pointer to the data is passed as the argument to the function. If
566 no such work is necessary @var{freefct} must point to a function doing
567 nothing. It is called in any case.
569 This function is a GNU extension and not covered by the @w{System V} or
570 X/Open specifications.
573 In addition to the functions to create and destroy the tree data
574 structure, there is another function which allows you to apply a
575 function to all elements of the tree. The function must have this type:
578 void __action_fn_t (const void *nodep, VISIT value, int level);
581 The @var{nodep} is the data value of the current node (once given as the
582 @var{key} argument to @code{tsearch}). @var{level} is a numeric value
583 which corresponds to the depth of the current node in the tree. The
584 root node has the depth @math{0} and its children have a depth of
585 @math{1} and so on. The @code{VISIT} type is an enumeration type.
587 @deftp {Data Type} VISIT
588 The @code{VISIT} value indicates the status of the current node in the
589 tree and how the function is called. The status of a node is either
590 `leaf' or `internal node'. For each leaf node the function is called
591 exactly once, for each internal node it is called three times: before
592 the first child is processed, after the first child is processed and
593 after both children are processed. This makes it possible to handle all
594 three methods of tree traversal (or even a combination of them).
598 The current node is an internal node and the function is called before
599 the first child was processed.
601 The current node is an internal node and the function is called after
602 the first child was processed.
604 The current node is an internal node and the function is called after
605 the second child was processed.
607 The current node is a leaf.
611 @deftypefun void twalk (const void *@var{root}, __action_fn_t @var{action})
612 @standards{SVID, search.h}
613 @safety{@prelim{}@mtsafe{@mtsrace{:root}}@assafe{}@acsafe{}}
614 For each node in the tree with a node pointed to by @var{root}, the
615 @code{twalk} function calls the function provided by the parameter
616 @var{action}. For leaf nodes the function is called exactly once with
617 @var{value} set to @code{leaf}. For internal nodes the function is
618 called three times, setting the @var{value} parameter or @var{action} to
619 the appropriate value. The @var{level} argument for the @var{action}
620 function is computed while descending the tree by increasing the value
621 by one for each descent to a child, starting with the value @math{0} for
624 Since the functions used for the @var{action} parameter to @code{twalk}
625 must not modify the tree data, it is safe to run @code{twalk} in more
626 than one thread at the same time, working on the same tree. It is also
627 safe to call @code{tfind} in parallel. Functions which modify the tree
628 must not be used, otherwise the behavior is undefined. However, it is
629 difficult to pass data external to the tree to the callback function
630 without resorting to global variables (and thread safety issues), so
631 see the @code{twalk_r} function below.
634 @deftypefun void twalk_r (const void *@var{root}, void (*@var{action}) (const void *@var{key}, VISIT @var{which}, void *@var{closure}), void *@var{closure})
635 @standards{GNU, search.h}
636 @safety{@prelim{}@mtsafe{@mtsrace{:root}}@assafe{}@acsafe{}}
637 For each node in the tree with a node pointed to by @var{root}, the
638 @code{twalk_r} function calls the function provided by the parameter
639 @var{action}. For leaf nodes the function is called exactly once with
640 @var{which} set to @code{leaf}. For internal nodes the function is
641 called three times, setting the @var{which} parameter of @var{action} to
642 the appropriate value. The @var{closure} parameter is passed down to
643 each call of the @var{action} function, unmodified.
645 It is possible to implement the @code{twalk} function on top of the
646 @code{twalk_r} function, which is why there is no separate level
650 @include twalk.c.texi