1tsearch(3)                 Library Functions Manual                 tsearch(3)
2
3
4

NAME

6       tsearch,  tfind,  tdelete,  twalk,  twalk_r, tdestroy - manage a binary
7       search tree
8

LIBRARY

10       Standard C library (libc, -lc)
11

SYNOPSIS

13       #include <search.h>
14
15       typedef enum { preorder, postorder, endorder, leaf } VISIT;
16
17       void *tsearch(const void *key, void **rootp,
18                       int (*compar)(const void *, const void *));
19       void *tfind(const void *key, void *const *rootp,
20                       int (*compar)(const void *, const void *));
21       void *tdelete(const void *restrict key, void **restrict rootp,
22                       int (*compar)(const void *, const void *));
23       void twalk(const void *root,
24                       void (*action)(const void *nodep, VISIT which,
25                                      int depth));
26
27       #define _GNU_SOURCE         /* See feature_test_macros(7) */
28       #include <search.h>
29
30       void twalk_r(const void *root,
31                       void (*action)(const void *nodep, VISIT which,
32                                      void *closure),
33                       void *closure);
34       void tdestroy(void *root, void (*free_node)(void *nodep));
35

DESCRIPTION

37       tsearch(), tfind(), twalk(), and tdelete() manage a binary search tree.
38       They  are  generalized from Knuth (6.2.2) Algorithm T.  The first field
39       in each node of the tree is a pointer to the corresponding  data  item.
40       (The  calling  program must store the actual data.)  compar points to a
41       comparison routine, which takes pointers to two items.  It  should  re‐
42       turn  an  integer  which  is  negative, zero, or positive, depending on
43       whether the first item is less than, equal to, or greater than the sec‐
44       ond.
45
46       tsearch()  searches the tree for an item.  key points to the item to be
47       searched for.  rootp points to a variable which points to the  root  of
48       the tree.  If the tree is empty, then the variable that rootp points to
49       should be set to NULL.   If  the  item  is  found  in  the  tree,  then
50       tsearch()  returns a pointer to the corresponding tree node.  (In other
51       words, tsearch() returns a pointer to a pointer to the data item.)   If
52       the item is not found, then tsearch() adds it, and returns a pointer to
53       the corresponding tree node.
54
55       tfind() is like tsearch(), except that if the item is not  found,  then
56       tfind() returns NULL.
57
58       tdelete() deletes an item from the tree.  Its arguments are the same as
59       for tsearch().
60
61       twalk() performs depth-first, left-to-right traversal of a binary tree.
62       root  points  to  the starting node for the traversal.  If that node is
63       not the root, then only part of the  tree  will  be  visited.   twalk()
64       calls  the  user  function action each time a node is visited (that is,
65       three times for an internal node, and once for  a  leaf).   action,  in
66       turn,  takes  three  arguments.  The first argument is a pointer to the
67       node being visited.  The structure of the node is unspecified,  but  it
68       is  possible  to cast the pointer to a pointer-to-pointer-to-element in
69       order to access the element stored within the  node.   The  application
70       must  not modify the structure pointed to by this argument.  The second
71       argument is an integer which takes one of  the  values  preorder,  pos‐
72       torder,  or endorder depending on whether this is the first, second, or
73       third visit to the internal node, or the value leaf if this is the sin‐
74       gle  visit  to a leaf node.  (These symbols are defined in <search.h>.)
75       The third argument is the depth of the node; the root  node  has  depth
76       zero.
77
78       (More  commonly,  preorder,  postorder,  and endorder are known as pre‐
79       order, inorder, and postorder: before visiting the children, after  the
80       first  and  before  the second, and after visiting the children.  Thus,
81       the choice of name postorder is rather confusing.)
82
83       twalk_r() is similar to twalk(), but instead of the depth argument, the
84       closure  argument  pointer  is  passed to each invocation of the action
85       callback, unchanged.  This pointer can be used to pass  information  to
86       and  from  the  callback function in a thread-safe fashion, without re‐
87       sorting to global variables.
88
89       tdestroy() removes the whole tree pointed to by root, freeing  all  re‐
90       sources allocated by the tsearch() function.  For the data in each tree
91       node the function free_node is called.  The  pointer  to  the  data  is
92       passed  as the argument to the function.  If no such work is necessary,
93       free_node must point to a function doing nothing.
94

RETURN VALUE

96       tsearch() returns a pointer to a matching node in the tree, or  to  the
97       newly  added  node, or NULL if there was insufficient memory to add the
98       item.  tfind() returns a pointer to the node, or NULL if  no  match  is
99       found.   If there are multiple items that match the key, the item whose
100       node is returned is unspecified.
101
102       tdelete() returns a pointer to the parent of the node deleted, or  NULL
103       if  the  item  was  not  found.  If the deleted node was the root node,
104       tdelete() returns a dangling pointer that must not be accessed.
105
106       tsearch(), tfind(), and tdelete() also return NULL if rootp was NULL on
107       entry.
108

ATTRIBUTES

110       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
111       tributes(7).
112
113       ┌─────────────────────────────────┬───────────────┬────────────────────┐
114Interface                        Attribute     Value              
115       ├─────────────────────────────────┼───────────────┼────────────────────┤
116tsearch(), tfind(), tdelete()    │ Thread safety │ MT-Safe race:rootp │
117       ├─────────────────────────────────┼───────────────┼────────────────────┤
118twalk()                          │ Thread safety │ MT-Safe race:root  │
119       ├─────────────────────────────────┼───────────────┼────────────────────┤
120twalk_r()                        │ Thread safety │ MT-Safe race:root  │
121       ├─────────────────────────────────┼───────────────┼────────────────────┤
122tdestroy()                       │ Thread safety │ MT-Safe            │
123       └─────────────────────────────────┴───────────────┴────────────────────┘
124

STANDARDS

126       tsearch()
127       tfind()
128       tdelete()
129       twalk()
130              POSIX.1-2008.
131
132       tdestroy()
133       twalk_r()
134              GNU.
135

HISTORY

137       tsearch()
138       tfind()
139       tdelete()
140       twalk()
141              POSIX.1-2001, POSIX.1-2008, SVr4.
142
143       twalk_r()
144              glibc 2.30.
145

NOTES

147       twalk() takes a pointer to the root, while the other functions  take  a
148       pointer to a variable which points to the root.
149
150       tdelete() frees the memory required for the node in the tree.  The user
151       is responsible for freeing the memory for the corresponding data.
152
153       The example program depends on the fact that twalk() makes  no  further
154       reference  to a node after calling the user function with argument "en‐
155       dorder" or "leaf".  This works with the GNU library implementation, but
156       is not in the System V documentation.
157

EXAMPLES

159       The following program inserts twelve random numbers into a binary tree,
160       where duplicate numbers are collapsed, then prints the numbers  in  or‐
161       der.
162
163       #define _GNU_SOURCE     /* Expose declaration of tdestroy() */
164       #include <search.h>
165       #include <stddef.h>
166       #include <stdio.h>
167       #include <stdlib.h>
168       #include <time.h>
169
170       static void *root = NULL;
171
172       static void *
173       xmalloc(size_t n)
174       {
175           void *p;
176
177           p = malloc(n);
178           if (p)
179               return p;
180           fprintf(stderr, "insufficient memory\n");
181           exit(EXIT_FAILURE);
182       }
183
184       static int
185       compare(const void *pa, const void *pb)
186       {
187           if (*(int *) pa < *(int *) pb)
188               return -1;
189           if (*(int *) pa > *(int *) pb)
190               return 1;
191           return 0;
192       }
193
194       static void
195       action(const void *nodep, VISIT which, int depth)
196       {
197           int *datap;
198
199           switch (which) {
200           case preorder:
201               break;
202           case postorder:
203               datap = *(int **) nodep;
204               printf("%6d\n", *datap);
205               break;
206           case endorder:
207               break;
208           case leaf:
209               datap = *(int **) nodep;
210               printf("%6d\n", *datap);
211               break;
212           }
213       }
214
215       int
216       main(void)
217       {
218           int  *ptr;
219           int  **val;
220
221           srand(time(NULL));
222           for (unsigned int i = 0; i < 12; i++) {
223               ptr = xmalloc(sizeof(*ptr));
224               *ptr = rand() & 0xff;
225               val = tsearch(ptr, &root, compare);
226               if (val == NULL)
227                   exit(EXIT_FAILURE);
228               if (*val != ptr)
229                   free(ptr);
230           }
231           twalk(root, action);
232           tdestroy(root, free);
233           exit(EXIT_SUCCESS);
234       }
235

SEE ALSO

237       bsearch(3), hsearch(3), lsearch(3), qsort(3)
238
239
240
241Linux man-pages 6.04              2023-03-30                        tsearch(3)
Impressum