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
12 tdelete, tfind, tsearch, twalk - manage a binary search tree
13
15 #include <search.h>
16
17 void *tdelete(const void *restrict key, void **restrict 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 *tsearch(const void *key, void **rootp,
22 int (*compar)(const void *, const void *));
23 void twalk(const void *root,
24 void (*action)(const void *, VISIT, int));
25
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 com‐
36 pare every byte, so arbitrary data may be contained in the elements in
37 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 a null pointer if the node is not found.
62
63 The twalk() function shall traverse a binary search tree. The root
64 argument is a pointer to the root node of the tree to be traversed.
65 (Any node in a tree may be used as the root for a walk below that
66 node.) The argument action is the name of a routine to be invoked at
67 each node. This routine is, in turn, called with three arguments. The
68 first argument shall be the address of the node being visited. The
69 structure pointed to by this argument is unspecified and shall not be
70 modified by the application, but it shall be possible to cast a
71 pointer-to-node into a pointer-to-pointer-to-element to access the ele‐
72 ment stored in the node. The second argument shall be a value from an
73 enumeration data type:
74
75
76 typedef enum { preorder, postorder, endorder, leaf } VISIT;
77
78 (defined in <search.h>), depending on whether this is the first, sec‐
79 ond, or third time that the node is visited (during a depth-first,
80 left-to-right traversal of the tree), or whether the node is a leaf.
81 The third argument shall be the level of the node in the tree, with the
82 root being level 0.
83
84 If the calling function alters the pointer to the root, the result is
85 undefined.
86
88 If the node is found, both tsearch() and tfind() shall return a pointer
89 to it. If not, tfind() shall return a null pointer, and tsearch() shall
90 return a pointer to the inserted item.
91
92 A null pointer shall be returned by tsearch() if there is not enough
93 space available to create a new node.
94
95 A null pointer shall be returned by tdelete(), tfind(), and tsearch()
96 if rootp is a null pointer on entry.
97
98 The tdelete() function shall return a pointer to the parent of the
99 deleted node, or a null pointer if the node is not found.
100
101 The twalk() function shall not return a value.
102
104 No errors are defined.
105
106 The following sections are informative.
107
109 The following code reads in strings and stores structures containing a
110 pointer to each string and a count of its length. It then walks the
111 tree, printing out the stored strings and their lengths in alphabetical
112 order.
113
114
115 #include <search.h>
116 #include <string.h>
117 #include <stdio.h>
118
119
120 #define STRSZ 10000
121 #define NODSZ 500
122
123
124 struct node { /* Pointers to these are stored in the tree. */
125 char *string;
126 int length;
127 };
128
129
130 char string_space[STRSZ]; /* Space to store strings. */
131 struct node nodes[NODSZ]; /* Nodes to store. */
132 void *root = NULL; /* This points to the root. */
133
134
135 int main(int argc, char *argv[])
136 {
137 char *strptr = string_space;
138 struct node *nodeptr = nodes;
139 void print_node(const void *, VISIT, int);
140 int i = 0, node_compare(const void *, const void *);
141
142
143 while (gets(strptr) != NULL && i++ < NODSZ) {
144 /* Set node. */
145 nodeptr->string = strptr;
146 nodeptr->length = strlen(strptr);
147 /* Put node into the tree. */
148 (void) tsearch((void *)nodeptr, (void **)&root,
149 node_compare);
150 /* Adjust pointers, so we do not overwrite tree. */
151 strptr += nodeptr->length + 1;
152 nodeptr++;
153 }
154 twalk(root, print_node);
155 return 0;
156 }
157
158
159 /*
160 * This routine compares two nodes, based on an
161 * alphabetical ordering of the string field.
162 */
163 int
164 node_compare(const void *node1, const void *node2)
165 {
166 return strcmp(((const struct node *) node1)->string,
167 ((const struct node *) node2)->string);
168 }
169
170
171 /*
172 * This routine prints out a node, the second time
173 * twalk encounters it or if it is a leaf.
174 */
175 void
176 print_node(const void *ptr, VISIT order, int level)
177 {
178 const struct node *p = *(const struct node **) ptr;
179
180
181 if (order == postorder || order == leaf) {
182 (void) printf("string = %s, length = %d\n",
183 p->string, p->length);
184 }
185 }
186
188 The root argument to twalk() is one level of indirection less than the
189 rootp arguments to tdelete() and tsearch().
190
191 There are two nomenclatures used to refer to the order in which tree
192 nodes are visited. The tsearch() function uses preorder, postorder, and
193 endorder to refer respectively to visiting a node before any of its
194 children, after its left child and before its right, and after both its
195 children. The alternative nomenclature uses preorder, inorder, and
196 postorder to refer to the same visits, which could result in some con‐
197 fusion over the meaning of postorder.
198
200 None.
201
203 None.
204
206 hcreate(), lsearch(), the Base Definitions volume of
207 IEEE Std 1003.1-2001, <search.h>
208
210 Portions of this text are reprinted and reproduced in electronic form
211 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
212 -- Portable Operating System Interface (POSIX), The Open Group Base
213 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
214 Electrical and Electronics Engineers, Inc and The Open Group. In the
215 event of any discrepancy between this version and the original IEEE and
216 The Open Group Standard, the original IEEE and The Open Group Standard
217 is the referee document. The original Standard can be obtained online
218 at http://www.opengroup.org/unix/online.html .
219
220
221
222IEEE/The Open Group 2003 TDELETE(3P)