OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / draft / man3 / tsearch.3
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\" Copyright 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
3 .\"
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .\" Japanese Version Copyright (c) 1999 ishikawa, keisuke
25 .\"         all rights reserved.
26 .\" Translated Tue Mar  9 08:21:04 JST 1999
27 .\"         by ishikawa, keisuke <ishikawa@sgk.gr.jp>
28 .\" Updated & Modified Sun Jan 20 11:31:46 JST 2002
29 .\"         by Yuichi SATO <ysato@h4.dion.ne.jp>
30 .\"
31 .TH TSEARCH 3  2008-09-23 "GNU" "Linux Programmer's Manual"
32 .\"O .SH NAME
33 .SH Ì¾Á°
34 .\"O tsearch, tfind, tdelete, twalk, tdestroy \- manage a binary tree
35 tsearch, tfind, tdelete, twalk, tdestroy \- ÆóʬÌÚ (binary tree) ¤ÎÁàºî
36 .\"O .SH SYNOPSIS
37 .SH ½ñ¼°
38 .nf
39 .B #include <search.h>
40 .sp
41 .BI "void *tsearch(const void *" key ", void **" rootp ,
42 .BI "                int (*" compar ")(const void *, const void *));"
43 .sp
44 .BI "void *tfind(const void *" key ", const void **" rootp ,
45 .BI "                int (*" compar ")(const void *, const void *));"
46 .sp
47 .BI "void *tdelete(const void *" key ", void **" rootp ,
48 .BI "                int (*" compar ")(const void *, const void *));"
49 .sp
50 .BI "void twalk(const void *" root ", void (*" action ")(const void *" nodep ,
51 .BI "                                   const VISIT " which ,
52 .BI "                                   const int " depth "));"
53 .sp
54 .B #define _GNU_SOURCE
55 .br
56 .B #include <search.h>
57 .sp
58 .BI "void tdestroy(void *" root ", void (*" free_node ")(void *" nodep ));
59 .fi
60 .\"O .SH DESCRIPTION
61 .SH ÀâÌÀ
62 .\"O .BR tsearch (),
63 .\"O .BR tfind (),
64 .\"O .BR twalk (),
65 .\"O and
66 .\"O .BR tdelete ()
67 .\"O manage a
68 .\"O binary tree.
69 .\"O They are generalized from Knuth (6.2.2) Algorithm T.
70 .\"O The first field in each node of the tree is a pointer to the
71 .\"O corresponding data item.
72 .\"O (The calling program must store the actual data.)
73 .\"O \fIcompar\fP points to a comparison routine, which takes
74 .\"O pointers to two items.
75 .\"O It should return an integer which is negative,
76 .\"O zero, or positive, depending on whether the first item is less than,
77 .\"O equal to, or greater than the second.
78 .BR tsearch (),
79 .BR tfind (),
80 .BR twalk (),
81 .BR tdelete ()
82 ¤Ï
83 ÆóʬÌÚ¤òÁàºî¤¹¤ë´Ø¿ô¤Ç¤¢¤ë¡£
84 ¤³¤ì¤é¤Î´Ø¿ô¤Ï Knuth (6.2.2) Algorithm T ¤Ë´ð¤Å¤¤¤Æ¤¤¤ë¡£
85 ÌÚ¹½Â¤¤Ë¤ª¤±¤ë³Æ¥Î¡¼¥É¤ÎºÇ½é¤Î¥Õ¥£¡¼¥ë¥É¤Ï¡¢Âбþ¤¹¤ë¥Ç¡¼¥¿¡¦
86 ¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
87 (»²¾ÈÀè¤Î¥Ç¡¼¥¿¤Ï¡¢¸Æ¤Ó½Ð¤·¥×¥í¥°¥é¥à¤ÇÍÑ°Õ¤¹¤ë¡£)
88 \fIcompar\fP ¤ÏÈæ³Ó¥ë¡¼¥Á¥ó¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
89 Èæ³Ó¥ë¡¼¥Á¥ó¤Ï¡¢¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿ 2 ¤Ä¤ò°ú¿ô¤Ë»ý¤Ä¡£
90 Èæ³Ó¥ë¡¼¥Á¥ó¤ÎÊÖ¤êÃͤϡ¢1 ¤ÄÌܤΥ¢¥¤¥Æ¥à¤¬ 2 ¤ÄÌܤΥ¢¥¤¥Æ¥à¤è¤ê¤â
91 ¡Ö¾®¤µ¤¤¡¢Åù¤·¤¤¡¢Â礭¤¤¡×¤Ë¤è¤Ã¤Æ¡¢
92 ¡ÖÉé¡¢0¡¢Àµ¡×¤ÎÀ°¿ôÃͤǤʤ±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
93 .PP
94 .\"O .BR tsearch ()
95 .\"O searches the tree for an item.
96 .\"O \fIkey\fP points to the item to be searched for.
97 .\"O \fIrootp\fP points to a
98 .\"O variable which points to the root of the tree.
99 .\"O If the tree is empty,
100 .\"O then the variable that \fIrootp\fP points to should be set to NULL.
101 .\"O If the item is found in the tree, then
102 .\"O .BR tsearch ()
103 .\"O returns a pointer
104 .\"O to it.
105 .\"O If it is not found, then
106 .\"O .BR tsearch ()
107 .\"O adds it, and returns a
108 .\"O pointer to the newly added item.
109 .BR tsearch ()
110 ¤Ï¡¢ÌÚ¹½Â¤¤«¤é¥¢¥¤¥Æ¥à¤ò¸¡º÷¤¹¤ë´Ø¿ô¤Ç¤¢¤ë¡£
111 \fIkey\fP ¤Ï¡¢¸¡º÷¤¹¤ë¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
112 \fIrootp\fP ¤ÏÌÚ¹½Â¤¤Îº¬¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
113 ÌÚ¹½Â¤¤¬¥Î¡¼¥É¤ò´Þ¤Þ¤Ê¤¤¾ì¹ç¡¢\fIrootp\fP ¤Î»²¾È¤·¤Æ¤¤¤ëÊÑ¿ô¤Ï
114 NULL ¤ËÀßÄꤵ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
115 ÌÚ¹½Â¤¤Ë¥¢¥¤¥Æ¥à¤¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¡¢
116 .BR tsearch ()
117 ¤Ï¤½¤Î¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
118 ¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï¡¢¥¢¥¤¥Æ¥à¤òÌÚ¹½Â¤¤ËÄɲä·¡¢
119 Äɲä·¤¿¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
120 .PP
121 .\"O .BR tfind ()
122 .\"O is like
123 .\"O .BR tsearch (),
124 .\"O except that if the item is not
125 .\"O found, then
126 .\"O .BR tfind ()
127 .\"O returns NULL.
128 .BR tfind ()
129 ¤Ï¡¢
130 .BR tsearch ()
131 ¤Ë»÷¤Æ¤¤¤ë¤¬¡¢
132 ¥¢¥¤¥Æ¥à¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç NULL ¤òÊÖ¤¹ÅÀ¤¬°Û¤Ê¤ë¡£
133 .PP
134 .\"O .BR tdelete ()
135 .\"O deletes an item from the tree.
136 .\"O Its arguments are the same as for
137 .\"O .BR tsearch ().
138 .BR tdelete ()
139 ¤ÏÌÚ¹½Â¤¤«¤é¥¢¥¤¥Æ¥à¤òºï½ü¤¹¤ë¡£
140 °ú¿ô¤Ï
141 .BR tsearch ()
142 ¤ÈƱ¤¸¤Ç¤¢¤ë¡£
143 .PP
144 .\"O .BR twalk ()
145 .\"O performs depth-first, left-to-right traversal of a binary
146 .\"O tree.
147 .\"O \fIroot\fP points to the starting node for the traversal.
148 .\"O If that node is not the root, then only part of the tree will be visited.
149 .\"O .BR twalk ()
150 .\"O calls the user function \fIaction\fP each time a node is
151 .\"O visited (that is, three times for an internal node, and once for a
152 .\"O leaf).
153 .\"O \fIaction\fP, in turn, takes three arguments.
154 .\"O The first is a pointer to the node being visited.
155 .\"O The second is an integer which
156 .\"O takes on the values \fBpreorder\fP, \fBpostorder\fP, and
157 .\"O \fBendorder\fP depending on whether this is the first, second, or
158 .\"O third visit to the internal node, or \fBleaf\fP if it is the single
159 .\"O visit to a leaf node.
160 .\"O (These symbols are defined in \fI<search.h>\fP.)
161 .\"O The third argument is the depth of the node, with
162 .\"O zero being the root.
163 .BR twalk ()
164 ¤Ï¡¢ÆóʬÌÚ¤ò¿¼¤µÍ¥Àè (depth-first) ¤Ç¡¢
165 º¸¤«¤é±¦¤Ë¤¿¤É¤Ã¤Æ¤¤¤¯´Ø¿ô¤Ç¤¢¤ë¡£
166 \fIroot\fP ¤Ïµ¯ÅÀ¤È¤Ê¤ë¥Î¡¼¥É¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
167 \fIroot\fP ¤Ëº¬°Ê³°¤Î¥Î¡¼¥É¤ò»ØÄꤹ¤ë¤È¡¢ÉôʬÌÚ¤¬ÂоݤȤʤ롣
168 .BR twalk ()
169 ¤Ï¡¢¥Î¡¼¥É¤òˬ¤ì¤ëÅÙ¤Ë
170 (¤Ä¤Þ¤ê¡¢ÆâÉô¥Î¡¼¥É¤ËÂФ·¤Æ¤Ï 3 ²ó¡¢ÍÕ¤ËÂФ·¤Æ¤Ï 1 ²ó)
171 ¥æ¡¼¥¶´Ø¿ô \fIaction\fP ¤ò¸Æ¤Ó½Ð¤¹¡£
172 \fIaction\fP ¤Ë¤Ï°Ê²¼¤Î½ç¤Ë 3 ¤Ä¤Î°ú¿ô¤¬Í¿¤¨¤é¤ì¤ë¡£
173 ºÇ½é¤Î°ú¿ô¤Ïˬ¤ì¤¿¥Î¡¼¥É¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
174 2 ¤ÄÌܤΰú¿ô¤Ë¤Ï¡¢ÆâÉô¥Î¡¼¥É¤Î¾ì¹ç¤ÏˬÌä²ó¿ô¤Ë±þ¤¸¤Æ
175 \fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP ¤¬¡¢
176 Íդξì¹ç¤Ï \fBleaf\fP ¤¬Í¿¤¨¤é¤ì¤ë¡£
177 (¤³¤ì¤é¤Î¥·¥ó¥Ü¥ë¤Ï \fI<search.h>\fP ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£)
178 3 ¤ÄÌܤΰú¿ô¤Ï¥Î¡¼¥É¤Î¿¼¤µ¤Ç¡¢º¬¤Î¾ì¹ç¤Ï 0 ¤Ç¤¢¤ë¡£
179 .PP
180 .\"O (More commonly, \fBpreorder\fP, \fBpostorder\fP, and \fBendorder\fP
181 .\"O are known as \fBpreorder\fP, \fBinorder\fP, and \fBpostorder\fP:
182 .\"O before visiting the children, after the first and before the second,
183 .\"O and after visiting the children.
184 .\"O Thus, the choice of name \fBpost\%order\fP
185 .\"O is rather confusing.)
186 (¤è¤ê°ìÈÌŪ¤Ë¤Ï¡¢\fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP ¤Ï
187 \fBpreorder\fP, \fBinorder\fP, \fBpostorder\fP ¤È¤·¤ÆÃΤé¤ì¤Æ¤¤¤ë:
188 ¤½¤ì¤¾¤ì¡¢»ÒÍ×ÁǤòé¤ëÁ°¡¦ºÇ½é¤Î»ÒÍ×ÁǤòé¤Ã¤¿¸å¤«¤Ä 2 ÈÖÌܤλÒÍ×ÁǤòé¤ëÁ°¡¦
189 »ÒÍ×ÁǤòé¤Ã¤¿¸å¤È¤¤¤¦¤³¤È¤òɽ¤·¤Æ¤¤¤ë¡£
190 ¤è¤Ã¤Æ \fBpost\%order\fP ¤È¤¤¤¦Ì¾Á°¤òÁª¤Ö¤Î¤Ï¾¯¤·Ê¶¤é¤ï¤·¤¤¡£)
191 .PP
192 .\"O .BR tdestroy ()
193 .\"O removes the whole tree pointed to by \fIroot\fP,
194 .\"O freeing all resources allocated by the
195 .\"O .BR tsearch ()
196 .\"O function.
197 .\"O For the data in each tree node the function \fIfree_node\fP is called.
198 .\"O The pointer to the data is passed as the argument to the function.
199 .\"O If no such work is necessary \fIfree_node\fP must point to a function
200 .\"O doing nothing.
201 .BR tdestroy ()
202 ¤Ï \fIroot\fP ¤¬»Ø¤¹ÌÚ¹½Â¤Á´ÂΤòºï½ü¤·¡¢
203 .BR tsearch ()
204 ´Ø¿ô¤Ç³ÎÊݤµ¤ì¤¿¥ê¥½¡¼¥¹¤òÁ´¤Æ²òÊü¤¹¤ë¡£
205 ÌÚ¹½Â¤¤Î³Æ¥Î¡¼¥É¤Ë¤Ä¤¤¤Æ¡¢´Ø¿ô \fIfree_node\fP ¤¬¸Æ¤Ó½Ð¤µ¤ì¤ë¡£
206 ¥Ç¡¼¥¿¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬¤³¤Î´Ø¿ô¤Î°ú¿ô¤È¤·¤ÆÅϤµ¤ì¤ë¡£
207 ¤½¤Î¤è¤¦¤ÊÆ°ºî¤¬É¬ÍפǤʤ±¤ì¤Ð¡¢
208 \fIfree_node\fP ¤Ï²¿¤â¤·¤Ê¤¤´Ø¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
209 .\"O .SH "RETURN VALUE"
210 .SH ÊÖ¤êÃÍ
211 .\"O .BR tsearch ()
212 .\"O returns a pointer to a matching item in the tree, or to
213 .\"O the newly added item, or NULL if there was insufficient memory
214 .\"O to add the item.
215 .\"O .BR tfind ()
216 .\"O returns a pointer to the item, or
217 .\"O NULL if no match is found.
218 .\"O If there are multiple elements that match the key,
219 .\"O the element returned is unspecified.
220 .BR tsearch ()
221 ¤Ï¡¢ÌÚ¹½Â¤¤Ë¸«¤Ä¤«¤Ã¤¿¥¢¥¤¥Æ¥à¤«¡¢
222 ¿·¤·¤¯Äɲä·¤¿¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
223 ¥á¥â¥ê¤ÎÉÔ­¤Î¤¿¤á¥¢¥¤¥Æ¥à¤òÄɲäǤ­¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï NULL ¤òÊÖ¤¹¡£
224 .BR tfind ()
225 ¤Ï¡¢¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
226 °ìÃפ¹¤ë¥¢¥¤¥Æ¥à¤¬¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¤Ï NULL ¤òÊÖ¤¹¡£
227 ¸¡º÷¾ò·ï¤Ë°ìÃפ¹¤ëÍ×ÁǤ¬Ê£¿ô¤¢¤ë¾ì¹ç¡¢ÊÖ¤µ¤ì¤ëÃͤÏÉÔÄê¤Ç¤¢¤ë¡£
228 .PP
229 .\"O .BR tdelete ()
230 .\"O returns a pointer to the parent of the item deleted, or
231 .\"O NULL if the item was not found.
232 .BR tdelete ()
233 ¤Ïºï½ü¤·¤¿¥¢¥¤¥Æ¥à¤Î¿Æ¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
234 ¥¢¥¤¥Æ¥à¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï NULL ¤òÊÖ¤¹¡£
235 .PP
236 .\"O .BR tsearch (),
237 .\"O .BR tfind (),
238 .\"O and
239 .\"O .BR tdelete ()
240 .\"O also
241 .\"O return NULL if \fIrootp\fP was NULL on entry.
242 \fIrootp\fP ¤¬ NULL ¤Î¾ì¹ç¡¢
243 .BR tsearch (),
244 .BR tfind (),
245 .BR tdelete ()
246 ¤Ï NULL ¤òÊÖ¤¹¡£
247 .\"O .SH "CONFORMING TO"
248 .SH ½àµò
249 SVr4, POSIX.1-2001.
250 .\"O The function
251 .\"O .BR tdestroy ()
252 .\"O is a GNU extension.
253 ´Ø¿ô
254 .BR tdestroy ()
255 ¤Ï GNU ¤Î³ÈÄ¥¤Ç¤¢¤ë¡£
256 .\"O .SH NOTES
257 .SH Ãí°Õ
258 .\"O .BR twalk ()
259 .\"O takes a pointer to the root, while the other functions
260 .\"O take a pointer to a variable which points to the root.
261 .BR twalk ()
262 ¤Ïº¬¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò°ú¿ô¤Ë¤È¤ë¤¬¡¢
263 ¤Û¤«¤Î´Ø¿ô¤Ïº¬¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
264 .PP
265 .\"O .BR twalk ()
266 .\"O uses \fBpostorder\fP to mean "after the left subtree, but
267 .\"O before the right subtree".
268 .\"O Some authorities would call this
269 .\"O "inorder", and reserve "postorder" to mean "after both subtrees".
270 .BR twalk ()
271 ¤Ë¤ª¤¤¤Æ¤Ï¡¢\fBpostorder\fP ¤Ï
272 ¡Öº¸¤ÎÉôʬÌڤθå¤Ç¡¢±¦¤ÎÉôʬÌÚ¤ÎÁ°¡×¤ò°ÕÌ£¤·¤Æ¤¤¤ë¡£
273 ¤·¤«¤·¡¢¿Í¤Ë¤è¤Ã¤Æ¤Ï¤³¤ì¤ò "inorder" ¤È¸Æ¤ó¤Ç¡¢
274 "postorder" ¤ò¡ÖξÊý¤ÎÉôʬÌڤθå¡×¤È¤¹¤ë¾ì¹ç¤â¤¢¤ë¡£
275 .PP
276 .\"O .BR tdelete ()
277 .\"O frees the memory required for the node in the tree.
278 .\"O The user is responsible for freeing the memory for the corresponding
279 .\"O data.
280 .BR tdelete ()
281 ¤Ï¡¢ºï½ü¤·¤¿¥Î¡¼¥É¤Î»ÈÍѤ·¤Æ¤¤¤¿¥á¥â¥ê¤ò²òÊü¤¹¤ë¤¬¡¢
282 ¥Î¡¼¥É¤ËÂбþ¤¹¤ë¥Ç¡¼¥¿¤Î¥á¥â¥ê¤Ï¡¢¥æ¡¼¥¶¤¬²òÊü¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
283 .PP
284 .\"O The example program depends on the fact that
285 .\"O .BR twalk ()
286 .\"O makes no
287 .\"O further reference to a node after calling the user function with
288 .\"O argument "endorder" or "leaf".
289 .\"O This works with the GNU library
290 .\"O implementation, but is not in the System V documentation.
291 ²¼¤Î¥×¥í¥°¥é¥àÎã¤Ï¡¢¥æ¡¼¥¶´Ø¿ô¤¬ "endorder" ¤« "leaf" ¤ò°ú¿ô¤Ë¤·¤Æ
292 ¸Æ¤Ó½Ð¤µ¤ì¤Æ°Ê¹ß¤Ï¡¢
293 .BR twalk ()
294 ¤¬¤½¤Î¥Î¡¼¥É¤ò»²¾È¤·¤Ê¤¤¤³¤È¤òÁ°Äó¤È¤·¤Æ¤¤¤ë¡£
295 ¤³¤ì¤Ï GNU ¥é¥¤¥Ö¥é¥ê¤Î¼ÂÁõ¤Ç¤Ïµ¡Ç½¤¹¤ë¤¬¡¢System V ¤Î¥Þ¥Ë¥å¥¢¥ë¤Ë¤Ï¸ºß¤·¤Ê¤¤¡£
296 .\"O .SH EXAMPLE
297 .SH Îã
298 .\"O The following program inserts twelve random numbers into a binary
299 .\"O tree, where duplicate numbers are collapsed, then prints the numbers
300 .\"O in order.
301 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï 12 ¸Ä¤ÎÍð¿ô¤òÆóʬÌÚ¤ËÁÞÆþ¤·¤¿¸å¡¢
302 ÁÞÆþ¤·¤¿¿ô¤ò½çÈ֤˽ÐÎϤ¹¤ë (ÁÞÆþ¤ÎºÝ¡¢½ÅÊ£¤·¤¿Íð¿ô¤Ï 1 ¤Ä¤Ë¤Þ¤È¤á¤é¤ì¤ë)¡£
303 .sp
304 .nf
305 #define _GNU_SOURCE     /* Expose declaration of tdestroy() */
306 #include <search.h>
307 #include <stdlib.h>
308 #include <stdio.h>
309 #include <time.h>
310
311 void *root = NULL;
312
313 void *
314 xmalloc(unsigned n)
315 {
316     void *p;
317     p = malloc(n);
318     if (p)
319         return p;
320     fprintf(stderr, "insufficient memory\\n");
321     exit(EXIT_FAILURE);
322 }
323
324 int
325 compare(const void *pa, const void *pb)
326 {
327     if (*(int *) pa < *(int *) pb)
328         return \-1;
329     if (*(int *) pa > *(int *) pb)
330         return 1;
331     return 0;
332 }
333
334 void
335 action(const void *nodep, const VISIT which, const int depth)
336 {
337     int *datap;
338
339     switch (which) {
340     case preorder:
341         break;
342     case postorder:
343         datap = *(int **) nodep;
344         printf("%6d\\n", *datap);
345         break;
346     case endorder:
347         break;
348     case leaf:
349         datap = *(int **) nodep;
350         printf("%6d\\n", *datap);
351         break;
352     }
353 }
354
355 int
356 main(void)
357 {
358     int i, *ptr;
359     void *val;
360
361     srand(time(NULL));
362     for (i = 0; i < 12; i++) {
363         ptr = (int *) xmalloc(sizeof(int));
364         *ptr = rand() & 0xff;
365         val = tsearch((void *) ptr, &root, compare);
366         if (val == NULL)
367             exit(EXIT_FAILURE);
368         else if ((*(int **) val) != ptr)
369             free(ptr);
370     }
371     twalk(root, action);
372     tdestroy(root, free);
373     exit(EXIT_SUCCESS);
374 }
375 .fi
376 .\"O .SH "SEE ALSO"
377 .SH ´ØÏ¢¹àÌÜ
378 .BR bsearch (3),
379 .BR hsearch (3),
380 .BR lsearch (3)
381 .BR qsort (3),
382 .BR feature_test_macros (7)