1TDELETE(3P) POSIX Programmer's Manual TDELETE(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 tdelete, tfind, tsearch, twalk — manage a binary search tree
14
16 #include <search.h>
17
18 void *tdelete(const void *restrict key, void **restrict rootp,
19 int(*compar)(const void *, const void *));
20 void *tfind(const void *key, void *const *rootp,
21 int(*compar)(const void *, const void *));
22 void *tsearch(const void *key, void **rootp,
23 int (*compar)(const void *, const void *));
24 void twalk(const void *root,
25 void (*action)(const void *, VISIT, int));
26
28 The tdelete(), tfind(), tsearch(), and twalk() functions manipulate
29 binary search trees. Comparisons are made with a user-supplied routine,
30 the address of which is passed as the compar argument. This routine is
31 called with two arguments, which are the pointers to the elements being
32 compared. The application shall ensure that the user-supplied routine
33 returns an integer less than, equal to, or greater than 0, according to
34 whether the first argument is to be considered less than, equal to, or
35 greater than the second argument. The comparison function need not
36 compare every byte, so arbitrary data may be contained in the elements
37 in addition to the values being compared.
38
39 The tsearch() function shall build and access the tree. The key argu‐
40 ment is a pointer to an element to be accessed or stored. If there is a
41 node in the tree whose element is equal to the value pointed to by key,
42 a pointer to this found node shall be returned. Otherwise, the value
43 pointed to by key shall be inserted (that is, a new node is created and
44 the value of key is copied to this node), and a pointer to this node
45 returned. Only pointers are copied, so the application shall ensure
46 that the calling routine stores the data. The rootp argument points to
47 a variable that points to the root node of the tree. A null pointer
48 value for the variable pointed to by rootp denotes an empty tree; in
49 this case, the variable shall be set to point to the node which shall
50 be at the root of the new tree.
51
52 Like tsearch(), tfind() shall search for a node in the tree, returning
53 a pointer to it if found. However, if it is not found, tfind() shall
54 return a null pointer. The arguments for tfind() are the same as for
55 tsearch().
56
57 The tdelete() function shall delete a node from a binary search tree.
58 The arguments are the same as for tsearch(). The variable pointed to
59 by rootp shall be changed if the deleted node was the root of the tree.
60 The tdelete() function shall return a pointer to the parent of the
61 deleted node, or an unspecified non-null pointer if the deleted node
62 was the root node, or a null pointer if the node is not found.
63
64 If tsearch() adds an element to a tree, or tdelete() successfully
65 deletes an element from a tree, the concurrent use of that tree in
66 another thread, or use of pointers produced by a previous call to
67 tfind() or tsearch(), produces undefined results.
68
69 The twalk() function shall traverse a binary search tree. The root
70 argument is a pointer to the root node of the tree to be traversed.
71 (Any node in a tree may be used as the root for a walk below that
72 node.) The argument action is the name of a routine to be invoked at
73 each node. This routine is, in turn, called with three arguments. The
74 first argument shall be the address of the node being visited. The
75 structure pointed to by this argument is unspecified and shall not be
76 modified by the application, but it shall be possible to cast a
77 pointer-to-node into a pointer-to-pointer-to-element to access the ele‐
78 ment stored in the node. The second argument shall be a value from an
79 enumeration data type:
80
81 typedef enum { preorder, postorder, endorder, leaf } VISIT;
82
83 (defined in <search.h>), depending on whether this is the first, sec‐
84 ond, or third time that the node is visited (during a depth-first,
85 left-to-right traversal of the tree), or whether the node is a leaf.
86 The third argument shall be the level of the node in the tree, with the
87 root being level 0.
88
89 If the calling function alters the pointer to the root, the result is
90 undefined.
91
92 If the functions pointed to by action or compar (for any of these
93 binary search functions) change the tree, the results are undefined.
94
95 These functions are thread-safe only as long as multiple threads do not
96 access the same tree.
97
99 If the node is found, both tsearch() and tfind() shall return a pointer
100 to it. If not, tfind() shall return a null pointer, and tsearch() shall
101 return a pointer to the inserted item.
102
103 A null pointer shall be returned by tsearch() if there is not enough
104 space available to create a new node.
105
106 A null pointer shall be returned by tdelete(), tfind(), and tsearch()
107 if rootp is a null pointer on entry.
108
109 The tdelete() function shall return a pointer to the parent of the
110 deleted node, or an unspecified non-null pointer if the deleted node
111 was the root node, or a null pointer if the node is not found.
112
113 The twalk() function shall not return a value.
114
116 No errors are defined.
117
118 The following sections are informative.
119
121 The following code reads in strings and stores structures containing a
122 pointer to each string and a count of its length. It then walks the
123 tree, printing out the stored strings and their lengths in alphabetical
124 order.
125
126 #include <search.h>
127 #include <string.h>
128 #include <stdio.h>
129
130 #define STRSZ 10000
131 #define NODSZ 500
132
133 struct node { /* Pointers to these are stored in the tree. */
134 char *string;
135 int length;
136 };
137
138 char string_space[STRSZ]; /* Space to store strings. */
139 struct node nodes[NODSZ]; /* Nodes to store. */
140 void *root = NULL; /* This points to the root. */
141
142 int main(int argc, char *argv[])
143 {
144 char *strptr = string_space;
145 struct node *nodeptr = nodes;
146 void print_node(const void *, VISIT, int);
147 int i = 0, node_compare(const void *, const void *);
148
149 while (gets(strptr) != NULL && i++ < NODSZ) {
150 /* Set node. */
151 nodeptr−>string = strptr;
152 nodeptr−>length = strlen(strptr);
153 /* Put node into the tree. */
154 (void) tsearch((void *)nodeptr, (void **)&root,
155 node_compare);
156 /* Adjust pointers, so we do not overwrite tree. */
157 strptr += nodeptr−>length + 1;
158 nodeptr++;
159 }
160 twalk(root, print_node);
161 return 0;
162 }
163
164 /*
165 * This routine compares two nodes, based on an
166 * alphabetical ordering of the string field.
167 */
168 int
169 node_compare(const void *node1, const void *node2)
170 {
171 return strcmp(((const struct node *) node1)−>string,
172 ((const struct node *) node2)−>string);
173 }
174
175 /*
176 * This routine prints out a node, the second time
177 * twalk encounters it or if it is a leaf.
178 */
179 void
180 print_node(const void *ptr, VISIT order, int level)
181 {
182 const struct node *p = *(const struct node **) ptr;
183
184 if (order == postorder || order == leaf) {
185 (void) printf("string = %s, length = %d\n",
186 p->string, p->length);
187 }
188 }
189
191 The root argument to twalk() is one level of indirection less than the
192 rootp arguments to tdelete() and tsearch().
193
194 There are two nomenclatures used to refer to the order in which tree
195 nodes are visited. The tsearch() function uses preorder, postorder, and
196 endorder to refer respectively to visiting a node before any of its
197 children, after its left child and before its right, and after both its
198 children. The alternative nomenclature uses preorder, inorder, and pos‐
199 torder to refer to the same visits, which could result in some confu‐
200 sion over the meaning of postorder.
201
202 Since the return value of tdelete() is an unspecified non-null pointer
203 in the case that the root of the tree has been deleted, applications
204 should only use the return value of tdelete() as indication of success
205 or failure and should not assume it can be dereferenced. Some implemen‐
206 tations in this case will return a pointer to the new root of the tree
207 (or to an empty tree if the deleted root node was the only node in the
208 tree); other implementations return arbitrary non-null pointers.
209
211 None.
212
214 None.
215
217 hcreate(), lsearch()
218
219 The Base Definitions volume of POSIX.1‐2008, <search.h>
220
222 Portions of this text are reprinted and reproduced in electronic form
223 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
224 -- Portable Operating System Interface (POSIX), The Open Group Base
225 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
226 cal and Electronics Engineers, Inc and The Open Group. (This is
227 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
228 event of any discrepancy between this version and the original IEEE and
229 The Open Group Standard, the original IEEE and The Open Group Standard
230 is the referee document. The original Standard can be obtained online
231 at http://www.unix.org/online.html .
232
233 Any typographical or formatting errors that appear in this page are
234 most likely to have been introduced during the conversion of the source
235 files to man page format. To report such errors, see https://www.ker‐
236 nel.org/doc/man-pages/reporting_bugs.html .
237
238
239
240IEEE/The Open Group 2013 TDELETE(3P)