OSDN Git Service

4e01a3e2dc516b632ecf85bde00e087d18e7dc88
[linuxjm/LDP_man-pages.git] / release / 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 .SH Ì¾Á°
33 tsearch, tfind, tdelete, twalk, tdestroy \- ÆóʬÌÚ (binary tree) ¤ÎÁàºî
34 .SH ½ñ¼°
35 .nf
36 .B #include <search.h>
37 .sp
38 .BI "void *tsearch(const void *" key ", void **" rootp ,
39 .BI "                int (*" compar ")(const void *, const void *));"
40 .sp
41 .BI "void *tfind(const void *" key ", const void **" rootp ,
42 .BI "                int (*" compar ")(const void *, const void *));"
43 .sp
44 .BI "void *tdelete(const void *" key ", void **" rootp ,
45 .BI "                int (*" compar ")(const void *, const void *));"
46 .sp
47 .BI "void twalk(const void *" root ", void (*" action ")(const void *" nodep ,
48 .BI "                                   const VISIT " which ,
49 .BI "                                   const int " depth "));"
50 .sp
51 .B #define _GNU_SOURCE
52 .br
53 .B #include <search.h>
54 .sp
55 .BI "void tdestroy(void *" root ", void (*" free_node ")(void *" nodep ));
56 .fi
57 .SH ÀâÌÀ
58 .BR tsearch (),
59 .BR tfind (),
60 .BR twalk (),
61 .BR tdelete ()
62 ¤Ï
63 ÆóʬÌÚ¤òÁàºî¤¹¤ë´Ø¿ô¤Ç¤¢¤ë¡£
64 ¤³¤ì¤é¤Î´Ø¿ô¤Ï Knuth (6.2.2) Algorithm T ¤Ë´ð¤Å¤¤¤Æ¤¤¤ë¡£
65 ÌÚ¹½Â¤¤Ë¤ª¤±¤ë³Æ¥Î¡¼¥É¤ÎºÇ½é¤Î¥Õ¥£¡¼¥ë¥É¤Ï¡¢Âбþ¤¹¤ë¥Ç¡¼¥¿¡¦
66 ¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
67 (»²¾ÈÀè¤Î¥Ç¡¼¥¿¤Ï¡¢¸Æ¤Ó½Ð¤·¥×¥í¥°¥é¥à¤ÇÍÑ°Õ¤¹¤ë¡£)
68 \fIcompar\fP ¤ÏÈæ³Ó¥ë¡¼¥Á¥ó¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
69 Èæ³Ó¥ë¡¼¥Á¥ó¤Ï¡¢¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿ 2 ¤Ä¤ò°ú¿ô¤Ë»ý¤Ä¡£
70 Èæ³Ó¥ë¡¼¥Á¥ó¤ÎÊÖ¤êÃͤϡ¢1 ¤ÄÌܤΥ¢¥¤¥Æ¥à¤¬ 2 ¤ÄÌܤΥ¢¥¤¥Æ¥à¤è¤ê¤â
71 ¡Ö¾®¤µ¤¤¡¢Åù¤·¤¤¡¢Â礭¤¤¡×¤Ë¤è¤Ã¤Æ¡¢
72 ¡ÖÉé¡¢0¡¢Àµ¡×¤ÎÀ°¿ôÃͤǤʤ±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
73 .PP
74 .BR tsearch ()
75 ¤Ï¡¢ÌÚ¹½Â¤¤«¤é¥¢¥¤¥Æ¥à¤ò¸¡º÷¤¹¤ë´Ø¿ô¤Ç¤¢¤ë¡£
76 \fIkey\fP ¤Ï¡¢¸¡º÷¤¹¤ë¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
77 \fIrootp\fP ¤ÏÌÚ¹½Â¤¤Îº¬¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
78 ÌÚ¹½Â¤¤¬¥Î¡¼¥É¤ò´Þ¤Þ¤Ê¤¤¾ì¹ç¡¢\fIrootp\fP ¤Î»²¾È¤·¤Æ¤¤¤ëÊÑ¿ô¤Ï
79 NULL ¤ËÀßÄꤵ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
80 ÌÚ¹½Â¤¤Ë¥¢¥¤¥Æ¥à¤¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¡¢
81 .BR tsearch ()
82 ¤Ï¤½¤Î¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
83 ¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï¡¢¥¢¥¤¥Æ¥à¤òÌÚ¹½Â¤¤ËÄɲä·¡¢
84 Äɲä·¤¿¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
85 .PP
86 .BR tfind ()
87 ¤Ï¡¢
88 .BR tsearch ()
89 ¤Ë»÷¤Æ¤¤¤ë¤¬¡¢
90 ¥¢¥¤¥Æ¥à¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç NULL ¤òÊÖ¤¹ÅÀ¤¬°Û¤Ê¤ë¡£
91 .PP
92 .BR tdelete ()
93 ¤ÏÌÚ¹½Â¤¤«¤é¥¢¥¤¥Æ¥à¤òºï½ü¤¹¤ë¡£
94 °ú¿ô¤Ï
95 .BR tsearch ()
96 ¤ÈƱ¤¸¤Ç¤¢¤ë¡£
97 .PP
98 .BR twalk ()
99 ¤Ï¡¢ÆóʬÌÚ¤ò¿¼¤µÍ¥Àè (depth-first) ¤Ç¡¢
100 º¸¤«¤é±¦¤Ë¤¿¤É¤Ã¤Æ¤¤¤¯´Ø¿ô¤Ç¤¢¤ë¡£
101 \fIroot\fP ¤Ïµ¯ÅÀ¤È¤Ê¤ë¥Î¡¼¥É¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
102 \fIroot\fP ¤Ëº¬°Ê³°¤Î¥Î¡¼¥É¤ò»ØÄꤹ¤ë¤È¡¢ÉôʬÌÚ¤¬ÂоݤȤʤ롣
103 .BR twalk ()
104 ¤Ï¡¢¥Î¡¼¥É¤òˬ¤ì¤ëÅÙ¤Ë
105 (¤Ä¤Þ¤ê¡¢ÆâÉô¥Î¡¼¥É¤ËÂФ·¤Æ¤Ï 3 ²ó¡¢ÍÕ¤ËÂФ·¤Æ¤Ï 1 ²ó)
106 ¥æ¡¼¥¶´Ø¿ô \fIaction\fP ¤ò¸Æ¤Ó½Ð¤¹¡£
107 \fIaction\fP ¤Ë¤Ï°Ê²¼¤Î½ç¤Ë 3 ¤Ä¤Î°ú¿ô¤¬Í¿¤¨¤é¤ì¤ë¡£
108 ºÇ½é¤Î°ú¿ô¤Ïˬ¤ì¤¿¥Î¡¼¥É¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
109 2 ¤ÄÌܤΰú¿ô¤Ë¤Ï¡¢ÆâÉô¥Î¡¼¥É¤Î¾ì¹ç¤ÏˬÌä²ó¿ô¤Ë±þ¤¸¤Æ
110 \fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP ¤¬¡¢
111 Íդξì¹ç¤Ï \fBleaf\fP ¤¬Í¿¤¨¤é¤ì¤ë¡£
112 (¤³¤ì¤é¤Î¥·¥ó¥Ü¥ë¤Ï \fI<search.h>\fP ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£)
113 3 ¤ÄÌܤΰú¿ô¤Ï¥Î¡¼¥É¤Î¿¼¤µ¤Ç¡¢º¬¤Î¾ì¹ç¤Ï 0 ¤Ç¤¢¤ë¡£
114 .PP
115 (¤è¤ê°ìÈÌŪ¤Ë¤Ï¡¢\fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP ¤Ï
116 \fBpreorder\fP, \fBinorder\fP, \fBpostorder\fP ¤È¤·¤ÆÃΤé¤ì¤Æ¤¤¤ë:
117 ¤½¤ì¤¾¤ì¡¢»ÒÍ×ÁǤòé¤ëÁ°¡¦ºÇ½é¤Î»ÒÍ×ÁǤòé¤Ã¤¿¸å¤«¤Ä 2 ÈÖÌܤλÒÍ×ÁǤòé¤ëÁ°¡¦
118 »ÒÍ×ÁǤòé¤Ã¤¿¸å¤È¤¤¤¦¤³¤È¤òɽ¤·¤Æ¤¤¤ë¡£
119 ¤è¤Ã¤Æ \fBpost\%order\fP ¤È¤¤¤¦Ì¾Á°¤òÁª¤Ö¤Î¤Ï¾¯¤·Ê¶¤é¤ï¤·¤¤¡£)
120 .PP
121 .BR tdestroy ()
122 ¤Ï \fIroot\fP ¤¬»Ø¤¹ÌÚ¹½Â¤Á´ÂΤòºï½ü¤·¡¢
123 .BR tsearch ()
124 ´Ø¿ô¤Ç³ÎÊݤµ¤ì¤¿¥ê¥½¡¼¥¹¤òÁ´¤Æ²òÊü¤¹¤ë¡£
125 ÌÚ¹½Â¤¤Î³Æ¥Î¡¼¥É¤Ë¤Ä¤¤¤Æ¡¢´Ø¿ô \fIfree_node\fP ¤¬¸Æ¤Ó½Ð¤µ¤ì¤ë¡£
126 ¥Ç¡¼¥¿¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬¤³¤Î´Ø¿ô¤Î°ú¿ô¤È¤·¤ÆÅϤµ¤ì¤ë¡£
127 ¤½¤Î¤è¤¦¤ÊÆ°ºî¤¬É¬ÍפǤʤ±¤ì¤Ð¡¢
128 \fIfree_node\fP ¤Ï²¿¤â¤·¤Ê¤¤´Ø¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
129 .SH ÊÖ¤êÃÍ
130 .BR tsearch ()
131 ¤Ï¡¢ÌÚ¹½Â¤¤Ë¸«¤Ä¤«¤Ã¤¿¥¢¥¤¥Æ¥à¤«¡¢
132 ¿·¤·¤¯Äɲä·¤¿¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
133 ¥á¥â¥ê¤ÎÉÔ­¤Î¤¿¤á¥¢¥¤¥Æ¥à¤òÄɲäǤ­¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï NULL ¤òÊÖ¤¹¡£
134 .BR tfind ()
135 ¤Ï¡¢¥¢¥¤¥Æ¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
136 °ìÃפ¹¤ë¥¢¥¤¥Æ¥à¤¬¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¤Ï NULL ¤òÊÖ¤¹¡£
137 ¸¡º÷¾ò·ï¤Ë°ìÃפ¹¤ëÍ×ÁǤ¬Ê£¿ô¤¢¤ë¾ì¹ç¡¢ÊÖ¤µ¤ì¤ëÃͤÏÉÔÄê¤Ç¤¢¤ë¡£
138 .PP
139 .BR tdelete ()
140 ¤Ïºï½ü¤·¤¿¥¢¥¤¥Æ¥à¤Î¿Æ¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
141 ¥¢¥¤¥Æ¥à¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï NULL ¤òÊÖ¤¹¡£
142 .PP
143 \fIrootp\fP ¤¬ NULL ¤Î¾ì¹ç¡¢
144 .BR tsearch (),
145 .BR tfind (),
146 .BR tdelete ()
147 ¤Ï NULL ¤òÊÖ¤¹¡£
148 .SH ½àµò
149 SVr4, POSIX.1-2001.
150 ´Ø¿ô
151 .BR tdestroy ()
152 ¤Ï GNU ¤Î³ÈÄ¥¤Ç¤¢¤ë¡£
153 .SH Ãí°Õ
154 .BR twalk ()
155 ¤Ïº¬¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò°ú¿ô¤Ë¤È¤ë¤¬¡¢
156 ¤Û¤«¤Î´Ø¿ô¤Ïº¬¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
157 .PP
158 .BR twalk ()
159 ¤Ë¤ª¤¤¤Æ¤Ï¡¢\fBpostorder\fP ¤Ï
160 ¡Öº¸¤ÎÉôʬÌڤθå¤Ç¡¢±¦¤ÎÉôʬÌÚ¤ÎÁ°¡×¤ò°ÕÌ£¤·¤Æ¤¤¤ë¡£
161 ¤·¤«¤·¡¢¿Í¤Ë¤è¤Ã¤Æ¤Ï¤³¤ì¤ò "inorder" ¤È¸Æ¤ó¤Ç¡¢
162 "postorder" ¤ò¡ÖξÊý¤ÎÉôʬÌڤθå¡×¤È¤¹¤ë¾ì¹ç¤â¤¢¤ë¡£
163 .PP
164 .BR tdelete ()
165 ¤Ï¡¢ºï½ü¤·¤¿¥Î¡¼¥É¤Î»ÈÍѤ·¤Æ¤¤¤¿¥á¥â¥ê¤ò²òÊü¤¹¤ë¤¬¡¢
166 ¥Î¡¼¥É¤ËÂбþ¤¹¤ë¥Ç¡¼¥¿¤Î¥á¥â¥ê¤Ï¡¢¥æ¡¼¥¶¤¬²òÊü¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
167 .PP
168 ²¼¤Î¥×¥í¥°¥é¥àÎã¤Ï¡¢¥æ¡¼¥¶´Ø¿ô¤¬ "endorder" ¤« "leaf" ¤ò°ú¿ô¤Ë¤·¤Æ
169 ¸Æ¤Ó½Ð¤µ¤ì¤Æ°Ê¹ß¤Ï¡¢
170 .BR twalk ()
171 ¤¬¤½¤Î¥Î¡¼¥É¤ò»²¾È¤·¤Ê¤¤¤³¤È¤òÁ°Äó¤È¤·¤Æ¤¤¤ë¡£
172 ¤³¤ì¤Ï GNU ¥é¥¤¥Ö¥é¥ê¤Î¼ÂÁõ¤Ç¤Ïµ¡Ç½¤¹¤ë¤¬¡¢System V ¤Î¥Þ¥Ë¥å¥¢¥ë¤Ë¤Ï¸ºß¤·¤Ê¤¤¡£
173 .SH Îã
174 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï 12 ¸Ä¤ÎÍð¿ô¤òÆóʬÌÚ¤ËÁÞÆþ¤·¤¿¸å¡¢
175 ÁÞÆþ¤·¤¿¿ô¤ò½çÈ֤˽ÐÎϤ¹¤ë (ÁÞÆþ¤ÎºÝ¡¢½ÅÊ£¤·¤¿Íð¿ô¤Ï 1 ¤Ä¤Ë¤Þ¤È¤á¤é¤ì¤ë)¡£
176 .sp
177 .nf
178 #define _GNU_SOURCE     /* Expose declaration of tdestroy() */
179 #include <search.h>
180 #include <stdlib.h>
181 #include <stdio.h>
182 #include <time.h>
183
184 void *root = NULL;
185
186 void *
187 xmalloc(unsigned n)
188 {
189     void *p;
190     p = malloc(n);
191     if (p)
192         return p;
193     fprintf(stderr, "insufficient memory\\n");
194     exit(EXIT_FAILURE);
195 }
196
197 int
198 compare(const void *pa, const void *pb)
199 {
200     if (*(int *) pa < *(int *) pb)
201         return \-1;
202     if (*(int *) pa > *(int *) pb)
203         return 1;
204     return 0;
205 }
206
207 void
208 action(const void *nodep, const VISIT which, const int depth)
209 {
210     int *datap;
211
212     switch (which) {
213     case preorder:
214         break;
215     case postorder:
216         datap = *(int **) nodep;
217         printf("%6d\\n", *datap);
218         break;
219     case endorder:
220         break;
221     case leaf:
222         datap = *(int **) nodep;
223         printf("%6d\\n", *datap);
224         break;
225     }
226 }
227
228 int
229 main(void)
230 {
231     int i, *ptr;
232     void *val;
233
234     srand(time(NULL));
235     for (i = 0; i < 12; i++) {
236         ptr = (int *) xmalloc(sizeof(int));
237         *ptr = rand() & 0xff;
238         val = tsearch((void *) ptr, &root, compare);
239         if (val == NULL)
240             exit(EXIT_FAILURE);
241         else if ((*(int **) val) != ptr)
242             free(ptr);
243     }
244     twalk(root, action);
245     tdestroy(root, free);
246     exit(EXIT_SUCCESS);
247 }
248 .fi
249 .SH ´ØÏ¢¹àÌÜ
250 .BR bsearch (3),
251 .BR hsearch (3),
252 .BR lsearch (3)
253 .BR qsort (3),
254 .BR feature_test_macros (7)