File: tsearch.3

package info (click to toggle)
manpages-ja 0.5.0.0.20100315-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 21,156 kB
  • ctags: 1
  • sloc: sh: 13,935; perl: 157; makefile: 114
file content (254 lines) | stat: -rw-r--r-- 7,742 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
.\" Hey Emacs! This file is -*- nroff -*- source.
.\" Copyright 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" Japanese Version Copyright (c) 1999 ishikawa, keisuke
.\"         all rights reserved.
.\" Translated Tue Mar  9 08:21:04 JST 1999
.\"         by ishikawa, keisuke <ishikawa@sgk.gr.jp>
.\" Updated & Modified Sun Jan 20 11:31:46 JST 2002
.\"         by Yuichi SATO <ysato@h4.dion.ne.jp>
.\"
.TH TSEARCH 3  2008-09-23 "GNU" "Linux Programmer's Manual"
.SH ̾
tsearch, tfind, tdelete, twalk, tdestroy \- ʬ (binary tree) 
.SH 
.nf
.B #include <search.h>
.sp
.BI "void *tsearch(const void *" key ", void **" rootp ,
.BI "                int (*" compar ")(const void *, const void *));"
.sp
.BI "void *tfind(const void *" key ", const void **" rootp ,
.BI "                int (*" compar ")(const void *, const void *));"
.sp
.BI "void *tdelete(const void *" key ", void **" rootp ,
.BI "                int (*" compar ")(const void *, const void *));"
.sp
.BI "void twalk(const void *" root ", void (*" action ")(const void *" nodep ,
.BI "                                   const VISIT " which ,
.BI "                                   const int " depth "));"
.sp
.B #define _GNU_SOURCE
.br
.B #include <search.h>
.sp
.BI "void tdestroy(void *" root ", void (*" free_node ")(void *" nodep ));
.fi
.SH 
.BR tsearch (),
.BR tfind (),
.BR twalk (),
.BR tdelete ()

ʬڤؿǤ롣
δؿ Knuth (6.2.2) Algorithm T ˴ŤƤ롣
ڹ¤ˤƥΡɤκǽΥեɤϡбǡ
ƥؤΥݥ󥿤Ǥ롣
(ΥǡϡƤӽФץѰդ롣)
\fIcompar\fP ӥ롼ؤΥݥ󥿤Ǥ롣
ӥ롼ϡƥؤΥݥ 2 Ĥ˻ġ
ӥ롼֤ͤϡ1 ܤΥƥब 2 ܤΥƥ
־礭פˤäơ
顢0פͤǤʤФʤʤ
.PP
.BR tsearch ()
ϡڹ¤饢ƥ򸡺ؿǤ롣
\fIkey\fP ϡ륢ƥؤΥݥ󥿤Ǥ롣
\fIrootp\fP ڹ¤κؤΥݥ󥿤ؤΥݥ󥿤Ǥ롣
ڹ¤Ρɤޤޤʤ硢\fIrootp\fP λȤƤѿ
NULL ꤵƤʤФʤʤ
ڹ¤˥ƥबĤä硢
.BR tsearch ()
ϤΥƥؤΥݥ󥿤֤
Ĥʤäϡƥڹ¤ɲä
ɲäƥؤΥݥ󥿤֤
.PP
.BR tfind ()
ϡ
.BR tsearch ()
˻Ƥ뤬
ƥबĤʤä NULL ֤ۤʤ롣
.PP
.BR tdelete ()
ڹ¤饢ƥ롣

.BR tsearch ()
ƱǤ롣
.PP
.BR twalk ()
ϡʬڤ򿼤ͥ (depth-first) ǡ
鱦ˤɤäƤؿǤ롣
\fIroot\fP ϵȤʤΡɤؤΥݥ󥿤Ǥ롣
\fIroot\fP ˺ʳΥΡɤꤹȡʬڤоݤȤʤ롣
.BR twalk ()
ϡΡɤˬ٤
(ĤޤꡢΡɤФƤ 3 դФƤ 1 )
桼ؿ \fIaction\fP ƤӽФ
\fIaction\fP ˤϰʲν 3 ĤΰͿ롣
ǽΰˬ줿ΡɤؤΥݥ󥿤Ǥ롣
2 ܤΰˤϡΡɤξˬ˱
\fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP 
դξ \fBleaf\fP Ϳ롣
(Υܥ \fI<search.h>\fP Ƥ롣)
3 ܤΰϥΡɤοǡξ 0 Ǥ롣
.PP
(Ūˤϡ\fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP 
\fBpreorder\fP, \fBinorder\fP, \fBpostorder\fP ȤΤƤ:
줾졢ǤéǽλǤéä夫 2 ܤλǤé
ǤéäȤȤɽƤ롣
ä \fBpost\%order\fP Ȥ֤̾ΤϾʶ路)
.PP
.BR tdestroy ()
 \fIroot\fP ؤڹ¤Τ
.BR tsearch ()
ؿdzݤ줿꥽Ʋ롣
ڹ¤γƥΡɤˤĤơؿ \fIfree_node\fP ƤӽФ롣
ǡؤΥݥ󥿤δؿΰȤϤ롣
Τ褦ưɬפǤʤС
\fIfree_node\fP ϲ⤷ʤؿؤΥݥ󥿤ǤʤФʤʤ
.SH ֤
.BR tsearch ()
ϡڹ¤˸Ĥäƥफ
ɲäƥؤΥݥ󥿤֤
­ΤᥢƥɲäǤʤä NULL ֤
.BR tfind ()
ϡƥؤΥݥ󥿤֤
פ륢ƥबĤʤ NULL ֤
˰פǤʣ硢֤ͤǤ롣
.PP
.BR tdelete ()
ϺƥοƤؤΥݥ󥿤֤
ƥबĤʤä NULL ֤
.PP
\fIrootp\fP  NULL ξ硢
.BR tsearch (),
.BR tfind (),
.BR tdelete ()
 NULL ֤
.SH 
SVr4, POSIX.1-2001.
ؿ
.BR tdestroy ()
 GNU γĥǤ롣
.SH 
.BR twalk ()
ϺؤΥݥ󥿤ˤȤ뤬
ۤδؿϺؤΥݥ󥿤ؤΥݥ󥿤Ǥ롣
.PP
.BR twalk ()
ˤƤϡ\fBpostorder\fP 
ֺʬڤθǡʬڤפ̣Ƥ롣
ͤˤäƤϤ "inorder" ȸƤǡ
"postorder" ξʬڤθפȤ⤢롣
.PP
.BR tdelete ()
ϡΡɤλѤƤ뤬
ΡɤбǡΥϡ桼ʤФʤʤ
.PP
Υץϡ桼ؿ "endorder"  "leaf" ˤ
ƤӽФưʹߤϡ
.BR twalk ()
ΥΡɤ򻲾ȤʤȤȤƤ롣
 GNU 饤֥μǤϵǽ뤬System V Υޥ˥奢ˤ¸ߤʤ
.SH 
ʲΥץ 12 Ĥʬڤ塢
֤˽Ϥ (κݡʣ 1 ĤˤޤȤ)
.sp
.nf
#define _GNU_SOURCE     /* Expose declaration of tdestroy() */
#include <search.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void *root = NULL;

void *
xmalloc(unsigned n)
{
    void *p;
    p = malloc(n);
    if (p)
        return p;
    fprintf(stderr, "insufficient memory\\n");
    exit(EXIT_FAILURE);
}

int
compare(const void *pa, const void *pb)
{
    if (*(int *) pa < *(int *) pb)
        return \-1;
    if (*(int *) pa > *(int *) pb)
        return 1;
    return 0;
}

void
action(const void *nodep, const VISIT which, const int depth)
{
    int *datap;

    switch (which) {
    case preorder:
        break;
    case postorder:
        datap = *(int **) nodep;
        printf("%6d\\n", *datap);
        break;
    case endorder:
        break;
    case leaf:
        datap = *(int **) nodep;
        printf("%6d\\n", *datap);
        break;
    }
}

int
main(void)
{
    int i, *ptr;
    void *val;

    srand(time(NULL));
    for (i = 0; i < 12; i++) {
        ptr = (int *) xmalloc(sizeof(int));
        *ptr = rand() & 0xff;
        val = tsearch((void *) ptr, &root, compare);
        if (val == NULL)
            exit(EXIT_FAILURE);
        else if ((*(int **) val) != ptr)
            free(ptr);
    }
    twalk(root, action);
    tdestroy(root, free);
    exit(EXIT_SUCCESS);
}
.fi
.SH Ϣ
.BR bsearch (3),
.BR hsearch (3),
.BR lsearch (3)
.BR qsort (3),
.BR feature_test_macros (7)