OSDN Git Service

fb46f90416a7e17ac8ceab59d4cc1fd53d42b0df
[linuxjm/LDP_man-pages.git] / release / man3 / tsearch.3
1 .\" Copyright 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
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 .\" %%%LICENSE_END
24 .\"
25 .\"*******************************************************************
26 .\"
27 .\" This file was generated with po4a. Translate the source file.
28 .\"
29 .\"*******************************************************************
30 .TH TSEARCH 3 2012\-08\-03 GNU "Linux Programmer's Manual"
31 .SH 名前
32 tsearch, tfind, tdelete, twalk, tdestroy \- 二分木 (binary tree) の操作
33 .SH 書式
34 .nf
35 \fB#include <search.h>\fP
36 .sp
37 \fBvoid *tsearch(const void *\fP\fIkey\fP\fB, void **\fP\fIrootp\fP\fB,\fP
38 \fB                int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP
39 .sp
40 \fBvoid *tfind(const void *\fP\fIkey\fP\fB, const void **\fP\fIrootp\fP\fB,\fP
41 \fB                int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP
42 .sp
43 \fBvoid *tdelete(const void *\fP\fIkey\fP\fB, void **\fP\fIrootp\fP\fB,\fP
44 \fB                int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP
45 .sp
46 \fBvoid twalk(const void *\fP\fIroot\fP\fB, void (*\fP\fIaction\fP\fB)(const void *\fP\fInodep\fP\fB,\fP
47 \fB                                   const VISIT \fP\fIwhich\fP\fB,\fP
48 \fB                                   const int \fP\fIdepth\fP\fB));\fP
49 .sp
50 \fB#define _GNU_SOURCE\fP         /* feature_test_macros(7) 参照 */
51 .br
52 \fB#include <search.h>\fP
53 .sp
54 \fBvoid tdestroy(void *\fP\fIroot\fP\fB, void (*\fP\fIfree_node\fP\fB)(void *\fP\fInodep\fP\fB));\fP
55 .fi
56 .SH 説明
57 \fBtsearch\fP(), \fBtfind\fP(), \fBtwalk\fP(), \fBtdelete\fP()  は 二分木を操作する関数である。 これらの関数は
58 Knuth (6.2.2) Algorithm T に基づいている。 木構造における各ノードの最初のフィールドは、対応するデータ・
59 アイテムへのポインタである。 (参照先のデータは、呼び出しプログラムで用意する。)  \fIcompar\fP は比較ルーチンへのポインタである。
60 比較ルーチンは、アイテムへのポインタ 2 つを引数に持つ。 比較ルーチンの返り値は、1 つ目のアイテムが 2 つ目のアイテムよりも
61 「小さい、等しい、大きい」によって、 「負、0、正」の整数値でなければならない。
62 .PP
63 \fBtsearch\fP()  は、木構造からアイテムを検索する関数である。 \fIkey\fP は、検索するアイテムへのポインタである。 \fIrootp\fP
64 は木構造の根へのポインタへのポインタである。 木構造がノードを含まない場合、\fIrootp\fP の参照している変数は NULL
65 に設定されていなければならない。 木構造にアイテムが見つかった場合、 \fBtsearch\fP()  はそのアイテムへのポインタを返す。
66 見つからなかった場合は、アイテムを木構造に追加し、 追加したアイテムへのポインタを返す。
67 .PP
68 \fBtfind\fP()  は、 \fBtsearch\fP()  に似ているが、 アイテムが見つからなかった場合 NULL を返す点が異なる。
69 .PP
70 \fBtdelete\fP()  は木構造からアイテムを削除する。 引数は \fBtsearch\fP()  と同じである。
71 .PP
72 \fBtwalk\fP()  performs depth\-first, left\-to\-right traversal of a binary tree.
73 \fIroot\fP points to the starting node for the traversal.  If that node is not
74 the root, then only part of the tree will be visited.  \fBtwalk\fP()  calls the
75 user function \fIaction\fP each time a node is visited (that is, three times
76 for an internal node, and once for a leaf).  \fIaction\fP, in turn, takes three
77 arguments.  The first argument is a pointer to the node being visited.  The
78 structure of the node is unspecified, but it is possible to cast the pointer
79 to a pointer\-to\-pointer\-to\-element in order to access the element stored
80 within the node.  The application must not modify the structure pointed to
81 by this argument.  The second argument is an integer which takes one of the
82 values \fBpreorder\fP, \fBpostorder\fP, or \fBendorder\fP depending on whether this
83 is the first, second, or third visit to the internal node, or the value
84 \fBleaf\fP if this is the single visit to a leaf node.  (These symbols are
85 defined in \fI<search.h>\fP.)  The third argument is the depth of the
86 node; the root node has depth zero.
87 .PP
88 (より一般的には、\fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP は \fBpreorder\fP, \fBinorder\fP,
89 \fBpostorder\fP として知られている: それぞれ、子要素を辿る前・最初の子要素を辿った後かつ 2 番目の子要素を辿る前・
90 子要素を辿った後ということを表している。 よって \fBpost\%order\fP という名前を選ぶのは少し紛らわしい。)
91 .PP
92 \fBtdestroy\fP()  は \fIroot\fP が指す木構造全体を削除し、 \fBtsearch\fP()  関数で確保されたリソースを全て解放する。
93 木構造の各ノードについて、関数 \fIfree_node\fP が呼び出される。 データへのポインタがこの関数の引数として渡される。
94 そのような動作が必要でなければ、 \fIfree_node\fP は何もしない関数へのポインタでなければならない。
95 .SH 返り値
96 \fBtsearch\fP()  は、木構造に見つかったアイテムか、 新しく追加したアイテムへのポインタを返す。
97 メモリの不足のためアイテムを追加できなかった場合は NULL を返す。 \fBtfind\fP()  は、アイテムへのポインタを返す。
98 一致するアイテムが見つからない場合は NULL を返す。 検索条件に一致する要素が複数ある場合、返される値は不定である。
99 .PP
100 \fBtdelete\fP()  は削除したアイテムの親へのポインタを返す。 アイテムが見つからなかった場合は NULL を返す。
101 .PP
102 \fIrootp\fP が NULL の場合、 \fBtsearch\fP(), \fBtfind\fP(), \fBtdelete\fP()  は NULL を返す。
103 .SH 準拠
104 SVr4, POSIX.1\-2001.  関数 \fBtdestroy\fP()  は GNU の拡張である。
105 .SH 注意
106 \fBtwalk\fP()  は根へのポインタを引数にとるが、 ほかの関数は根へのポインタへのポインタである。
107 .PP
108 \fBtdelete\fP()  は、削除したノードの使用していたメモリを解放するが、 ノードに対応するデータのメモリは、ユーザが解放しなければならない。
109 .PP
110 下のプログラム例は、ユーザ関数が "endorder" か "leaf" を引数にして 呼び出されて以降は、 \fBtwalk\fP()
111 がそのノードを参照しないことを前提としている。 これは GNU ライブラリの実装では機能するが、System V のマニュアルには存在しない。
112 .SH 例
113 以下のプログラムは 12 個の乱数を二分木に挿入した後、 挿入した数を順番に出力する (挿入の際、重複した乱数は 1 つにまとめられる)。
114 .sp
115 .nf
116 #define _GNU_SOURCE     /* Expose declaration of tdestroy() */
117 #include <search.h>
118 #include <stdlib.h>
119 #include <stdio.h>
120 #include <time.h>
121
122 void *root = NULL;
123
124 void *
125 xmalloc(unsigned n)
126 {
127     void *p;
128     p = malloc(n);
129     if (p)
130         return p;
131     fprintf(stderr, "insufficient memory\en");
132     exit(EXIT_FAILURE);
133 }
134
135 int
136 compare(const void *pa, const void *pb)
137 {
138     if (*(int *) pa < *(int *) pb)
139         return \-1;
140     if (*(int *) pa > *(int *) pb)
141         return 1;
142     return 0;
143 }
144
145 void
146 action(const void *nodep, const VISIT which, const int depth)
147 {
148     int *datap;
149
150     switch (which) {
151     case preorder:
152         break;
153     case postorder:
154         datap = *(int **) nodep;
155         printf("%6d\en", *datap);
156         break;
157     case endorder:
158         break;
159     case leaf:
160         datap = *(int **) nodep;
161         printf("%6d\en", *datap);
162         break;
163     }
164 }
165
166 int
167 main(void)
168 {
169     int i, *ptr;
170     void *val;
171
172     srand(time(NULL));
173     for (i = 0; i < 12; i++) {
174         ptr = xmalloc(sizeof(int));
175         *ptr = rand() & 0xff;
176         val = tsearch((void *) ptr, &root, compare);
177         if (val == NULL)
178             exit(EXIT_FAILURE);
179         else if ((*(int **) val) != ptr)
180             free(ptr);
181     }
182     twalk(root, action);
183     tdestroy(root, free);
184     exit(EXIT_SUCCESS);
185 }
186 .fi
187 .SH 関連項目
188 \fBbsearch\fP(3), \fBhsearch\fP(3), \fBlsearch\fP(3)  \fBqsort\fP(3)
189 .SH この文書について
190 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.51 の一部
191 である。プロジェクトの説明とバグ報告に関する情報は
192 http://www.kernel.org/doc/man\-pages/ に書かれている。