OSDN Git Service

104aea8d8986402b7a961ca0f4d88f461163cd93
[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 .\"O .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
55 .BR "#define _GNU_SOURCE" "         /* feature_test_macros(7) 参照 */"
56 .br
57 .B #include <search.h>
58 .sp
59 .BI "void tdestroy(void *" root ", void (*" free_node ")(void *" nodep ));
60 .fi
61 .\"O .SH DESCRIPTION
62 .SH 説明
63 .\"O .BR tsearch (),
64 .\"O .BR tfind (),
65 .\"O .BR twalk (),
66 .\"O and
67 .\"O .BR tdelete ()
68 .\"O manage a
69 .\"O binary tree.
70 .\"O They are generalized from Knuth (6.2.2) Algorithm T.
71 .\"O The first field in each node of the tree is a pointer to the
72 .\"O corresponding data item.
73 .\"O (The calling program must store the actual data.)
74 .\"O \fIcompar\fP points to a comparison routine, which takes
75 .\"O pointers to two items.
76 .\"O It should return an integer which is negative,
77 .\"O zero, or positive, depending on whether the first item is less than,
78 .\"O equal to, or greater than the second.
79 .BR tsearch (),
80 .BR tfind (),
81 .BR twalk (),
82 .BR tdelete ()
83
84 二分木を操作する関数である。
85 これらの関数は Knuth (6.2.2) Algorithm T に基づいている。
86 木構造における各ノードの最初のフィールドは、対応するデータ・
87 アイテムへのポインタである。
88 (参照先のデータは、呼び出しプログラムで用意する。)
89 \fIcompar\fP は比較ルーチンへのポインタである。
90 比較ルーチンは、アイテムへのポインタ 2 つを引数に持つ。
91 比較ルーチンの返り値は、1 つ目のアイテムが 2 つ目のアイテムよりも
92 「小さい、等しい、大きい」によって、
93 「負、0、正」の整数値でなければならない。
94 .PP
95 .\"O .BR tsearch ()
96 .\"O searches the tree for an item.
97 .\"O \fIkey\fP points to the item to be searched for.
98 .\"O \fIrootp\fP points to a
99 .\"O variable which points to the root of the tree.
100 .\"O If the tree is empty,
101 .\"O then the variable that \fIrootp\fP points to should be set to NULL.
102 .\"O If the item is found in the tree, then
103 .\"O .BR tsearch ()
104 .\"O returns a pointer
105 .\"O to it.
106 .\"O If it is not found, then
107 .\"O .BR tsearch ()
108 .\"O adds it, and returns a
109 .\"O pointer to the newly added item.
110 .BR tsearch ()
111 は、木構造からアイテムを検索する関数である。
112 \fIkey\fP は、検索するアイテムへのポインタである。
113 \fIrootp\fP は木構造の根へのポインタへのポインタである。
114 木構造がノードを含まない場合、\fIrootp\fP の参照している変数は
115 NULL に設定されていなければならない。
116 木構造にアイテムが見つかった場合、
117 .BR tsearch ()
118 はそのアイテムへのポインタを返す。
119 見つからなかった場合は、アイテムを木構造に追加し、
120 追加したアイテムへのポインタを返す。
121 .PP
122 .\"O .BR tfind ()
123 .\"O is like
124 .\"O .BR tsearch (),
125 .\"O except that if the item is not
126 .\"O found, then
127 .\"O .BR tfind ()
128 .\"O returns NULL.
129 .BR tfind ()
130 は、
131 .BR tsearch ()
132 に似ているが、
133 アイテムが見つからなかった場合 NULL を返す点が異なる。
134 .PP
135 .\"O .BR tdelete ()
136 .\"O deletes an item from the tree.
137 .\"O Its arguments are the same as for
138 .\"O .BR tsearch ().
139 .BR tdelete ()
140 は木構造からアイテムを削除する。
141 引数は
142 .BR tsearch ()
143 と同じである。
144 .PP
145 .\"O .BR twalk ()
146 .\"O performs depth-first, left-to-right traversal of a binary
147 .\"O tree.
148 .\"O \fIroot\fP points to the starting node for the traversal.
149 .\"O If that node is not the root, then only part of the tree will be visited.
150 .\"O .BR twalk ()
151 .\"O calls the user function \fIaction\fP each time a node is
152 .\"O visited (that is, three times for an internal node, and once for a
153 .\"O leaf).
154 .\"O \fIaction\fP, in turn, takes three arguments.
155 .\"O The first is a pointer to the node being visited.
156 .\"O The second is an integer which
157 .\"O takes on the values \fBpreorder\fP, \fBpostorder\fP, and
158 .\"O \fBendorder\fP depending on whether this is the first, second, or
159 .\"O third visit to the internal node, or \fBleaf\fP if it is the single
160 .\"O visit to a leaf node.
161 .\"O (These symbols are defined in \fI<search.h>\fP.)
162 .\"O The third argument is the depth of the node, with
163 .\"O zero being the root.
164 .BR twalk ()
165 は、二分木を深さ優先 (depth-first) で、
166 左から右にたどっていく関数である。
167 \fIroot\fP は起点となるノードへのポインタである。
168 \fIroot\fP に根以外のノードを指定すると、部分木が対象となる。
169 .BR twalk ()
170 は、ノードを訪れる度に
171 (つまり、内部ノードに対しては 3 回、葉に対しては 1 回)
172 ユーザ関数 \fIaction\fP を呼び出す。
173 \fIaction\fP には以下の順に 3 つの引数が与えられる。
174 最初の引数は訪れたノードへのポインタである。
175 2 つ目の引数には、内部ノードの場合は訪問回数に応じて
176 \fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP が、
177 葉の場合は \fBleaf\fP が与えられる。
178 (これらのシンボルは \fI<search.h>\fP で定義されている。)
179 3 つ目の引数はノードの深さで、根の場合は 0 である。
180 .PP
181 .\"O (More commonly, \fBpreorder\fP, \fBpostorder\fP, and \fBendorder\fP
182 .\"O are known as \fBpreorder\fP, \fBinorder\fP, and \fBpostorder\fP:
183 .\"O before visiting the children, after the first and before the second,
184 .\"O and after visiting the children.
185 .\"O Thus, the choice of name \fBpost\%order\fP
186 .\"O is rather confusing.)
187 (より一般的には、\fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP は
188 \fBpreorder\fP, \fBinorder\fP, \fBpostorder\fP として知られている:
189 それぞれ、子要素を辿る前・最初の子要素を辿った後かつ 2 番目の子要素を辿る前・
190 子要素を辿った後ということを表している。
191 よって \fBpost\%order\fP という名前を選ぶのは少し紛らわしい。)
192 .PP
193 .\"O .BR tdestroy ()
194 .\"O removes the whole tree pointed to by \fIroot\fP,
195 .\"O freeing all resources allocated by the
196 .\"O .BR tsearch ()
197 .\"O function.
198 .\"O For the data in each tree node the function \fIfree_node\fP is called.
199 .\"O The pointer to the data is passed as the argument to the function.
200 .\"O If no such work is necessary \fIfree_node\fP must point to a function
201 .\"O doing nothing.
202 .BR tdestroy ()
203 は \fIroot\fP が指す木構造全体を削除し、
204 .BR tsearch ()
205 関数で確保されたリソースを全て解放する。
206 木構造の各ノードについて、関数 \fIfree_node\fP が呼び出される。
207 データへのポインタがこの関数の引数として渡される。
208 そのような動作が必要でなければ、
209 \fIfree_node\fP は何もしない関数へのポインタでなければならない。
210 .\"O .SH "RETURN VALUE"
211 .SH 返り値
212 .\"O .BR tsearch ()
213 .\"O returns a pointer to a matching item in the tree, or to
214 .\"O the newly added item, or NULL if there was insufficient memory
215 .\"O to add the item.
216 .\"O .BR tfind ()
217 .\"O returns a pointer to the item, or
218 .\"O NULL if no match is found.
219 .\"O If there are multiple elements that match the key,
220 .\"O the element returned is unspecified.
221 .BR tsearch ()
222 は、木構造に見つかったアイテムか、
223 新しく追加したアイテムへのポインタを返す。
224 メモリの不足のためアイテムを追加できなかった場合は NULL を返す。
225 .BR tfind ()
226 は、アイテムへのポインタを返す。
227 一致するアイテムが見つからない場合は NULL を返す。
228 検索条件に一致する要素が複数ある場合、返される値は不定である。
229 .PP
230 .\"O .BR tdelete ()
231 .\"O returns a pointer to the parent of the item deleted, or
232 .\"O NULL if the item was not found.
233 .BR tdelete ()
234 は削除したアイテムの親へのポインタを返す。
235 アイテムが見つからなかった場合は NULL を返す。
236 .PP
237 .\"O .BR tsearch (),
238 .\"O .BR tfind (),
239 .\"O and
240 .\"O .BR tdelete ()
241 .\"O also
242 .\"O return NULL if \fIrootp\fP was NULL on entry.
243 \fIrootp\fP が NULL の場合、
244 .BR tsearch (),
245 .BR tfind (),
246 .BR tdelete ()
247 は NULL を返す。
248 .\"O .SH "CONFORMING TO"
249 .SH 準拠
250 SVr4, POSIX.1-2001.
251 .\"O The function
252 .\"O .BR tdestroy ()
253 .\"O is a GNU extension.
254 関数
255 .BR tdestroy ()
256 は GNU の拡張である。
257 .\"O .SH NOTES
258 .SH 注意
259 .\"O .BR twalk ()
260 .\"O takes a pointer to the root, while the other functions
261 .\"O take a pointer to a variable which points to the root.
262 .BR twalk ()
263 は根へのポインタを引数にとるが、
264 ほかの関数は根へのポインタへのポインタである。
265 .PP
266 .\"O .BR twalk ()
267 .\"O uses \fBpostorder\fP to mean "after the left subtree, but
268 .\"O before the right subtree".
269 .\"O Some authorities would call this
270 .\"O "inorder", and reserve "postorder" to mean "after both subtrees".
271 .BR twalk ()
272 においては、\fBpostorder\fP は
273 「左の部分木の後で、右の部分木の前」を意味している。
274 しかし、人によってはこれを "inorder" と呼んで、
275 "postorder" を「両方の部分木の後」とする場合もある。
276 .PP
277 .\"O .BR tdelete ()
278 .\"O frees the memory required for the node in the tree.
279 .\"O The user is responsible for freeing the memory for the corresponding
280 .\"O data.
281 .BR tdelete ()
282 は、削除したノードの使用していたメモリを解放するが、
283 ノードに対応するデータのメモリは、ユーザが解放しなければならない。
284 .PP
285 .\"O The example program depends on the fact that
286 .\"O .BR twalk ()
287 .\"O makes no
288 .\"O further reference to a node after calling the user function with
289 .\"O argument "endorder" or "leaf".
290 .\"O This works with the GNU library
291 .\"O implementation, but is not in the System V documentation.
292 下のプログラム例は、ユーザ関数が "endorder" か "leaf" を引数にして
293 呼び出されて以降は、
294 .BR twalk ()
295 がそのノードを参照しないことを前提としている。
296 これは GNU ライブラリの実装では機能するが、System V のマニュアルには存在しない。
297 .\"O .SH EXAMPLE
298 .SH 例
299 .\"O The following program inserts twelve random numbers into a binary
300 .\"O tree, where duplicate numbers are collapsed, then prints the numbers
301 .\"O in order.
302 以下のプログラムは 12 個の乱数を二分木に挿入した後、
303 挿入した数を順番に出力する (挿入の際、重複した乱数は 1 つにまとめられる)。
304 .sp
305 .nf
306 #define _GNU_SOURCE     /* Expose declaration of tdestroy() */
307 #include <search.h>
308 #include <stdlib.h>
309 #include <stdio.h>
310 #include <time.h>
311
312 void *root = NULL;
313
314 void *
315 xmalloc(unsigned n)
316 {
317     void *p;
318     p = malloc(n);
319     if (p)
320         return p;
321     fprintf(stderr, "insufficient memory\\n");
322     exit(EXIT_FAILURE);
323 }
324
325 int
326 compare(const void *pa, const void *pb)
327 {
328     if (*(int *) pa < *(int *) pb)
329         return \-1;
330     if (*(int *) pa > *(int *) pb)
331         return 1;
332     return 0;
333 }
334
335 void
336 action(const void *nodep, const VISIT which, const int depth)
337 {
338     int *datap;
339
340     switch (which) {
341     case preorder:
342         break;
343     case postorder:
344         datap = *(int **) nodep;
345         printf("%6d\\n", *datap);
346         break;
347     case endorder:
348         break;
349     case leaf:
350         datap = *(int **) nodep;
351         printf("%6d\\n", *datap);
352         break;
353     }
354 }
355
356 int
357 main(void)
358 {
359     int i, *ptr;
360     void *val;
361
362     srand(time(NULL));
363     for (i = 0; i < 12; i++) {
364         ptr = (int *) xmalloc(sizeof(int));
365         *ptr = rand() & 0xff;
366         val = tsearch((void *) ptr, &root, compare);
367         if (val == NULL)
368             exit(EXIT_FAILURE);
369         else if ((*(int **) val) != ptr)
370             free(ptr);
371     }
372     twalk(root, action);
373     tdestroy(root, free);
374     exit(EXIT_SUCCESS);
375 }
376 .fi
377 .\"O .SH "SEE ALSO"
378 .SH 関連項目
379 .BR bsearch (3),
380 .BR hsearch (3),
381 .BR lsearch (3)
382 .BR qsort (3)