OSDN Git Service

2530665d86771ced77c8db466742ca2e802a3359
[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 .\"*******************************************************************
25 .\"
26 .\" This file was generated with po4a. Translate the source file.
27 .\"
28 .\"*******************************************************************
29 .TH TSEARCH 3 2008\-09\-23 GNU "Linux Programmer's Manual"
30 .SH 名前
31 tsearch, tfind, tdelete, twalk, tdestroy \- 二分木 (binary tree) の操作
32 .SH 書式
33 .nf
34 \fB#include <search.h>\fP
35 .sp
36 \fBvoid *tsearch(const void *\fP\fIkey\fP\fB, void **\fP\fIrootp\fP\fB,\fP
37 \fB                int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP
38 .sp
39 \fBvoid *tfind(const void *\fP\fIkey\fP\fB, const void **\fP\fIrootp\fP\fB,\fP
40 \fB                int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP
41 .sp
42 \fBvoid *tdelete(const void *\fP\fIkey\fP\fB, void **\fP\fIrootp\fP\fB,\fP
43 \fB                int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP
44 .sp
45 \fBvoid twalk(const void *\fP\fIroot\fP\fB, void (*\fP\fIaction\fP\fB)(const void *\fP\fInodep\fP\fB,\fP
46 \fB                                   const VISIT \fP\fIwhich\fP\fB,\fP
47 \fB                                   const int \fP\fIdepth\fP\fB));\fP
48 .sp
49 \fB#define _GNU_SOURCE\fP         /* feature_test_macros(7) 参照 */
50 .br
51 \fB#include <search.h>\fP
52 .sp
53 \fBvoid tdestroy(void *\fP\fIroot\fP\fB, void (*\fP\fIfree_node\fP\fB)(void *\fP\fInodep\fP\fB));\fP
54 .fi
55 .SH 説明
56 \fBtsearch\fP(), \fBtfind\fP(), \fBtwalk\fP(), \fBtdelete\fP()  は 二分木を操作する関数である。 これらの関数は
57 Knuth (6.2.2) Algorithm T に基づいている。 木構造における各ノードの最初のフィールドは、対応するデータ・
58 アイテムへのポインタである。 (参照先のデータは、呼び出しプログラムで用意する。)  \fIcompar\fP は比較ルーチンへのポインタである。
59 比較ルーチンは、アイテムへのポインタ 2 つを引数に持つ。 比較ルーチンの返り値は、1 つ目のアイテムが 2 つ目のアイテムよりも
60 「小さい、等しい、大きい」によって、 「負、0、正」の整数値でなければならない。
61 .PP
62 \fBtsearch\fP()  は、木構造からアイテムを検索する関数である。 \fIkey\fP は、検索するアイテムへのポインタである。 \fIrootp\fP
63 は木構造の根へのポインタへのポインタである。 木構造がノードを含まない場合、\fIrootp\fP の参照している変数は NULL
64 に設定されていなければならない。 木構造にアイテムが見つかった場合、 \fBtsearch\fP()  はそのアイテムへのポインタを返す。
65 見つからなかった場合は、アイテムを木構造に追加し、 追加したアイテムへのポインタを返す。
66 .PP
67 \fBtfind\fP()  は、 \fBtsearch\fP()  に似ているが、 アイテムが見つからなかった場合 NULL を返す点が異なる。
68 .PP
69 \fBtdelete\fP()  は木構造からアイテムを削除する。 引数は \fBtsearch\fP()  と同じである。
70 .PP
71 \fBtwalk\fP()  は、二分木を深さ優先 (depth\-first) で、 左から右にたどっていく関数である。 \fIroot\fP
72 は起点となるノードへのポインタである。 \fIroot\fP に根以外のノードを指定すると、部分木が対象となる。 \fBtwalk\fP()
73 は、ノードを訪れる度に (つまり、内部ノードに対しては 3 回、葉に対しては 1 回)  ユーザ関数 \fIaction\fP を呼び出す。
74 \fIaction\fP には以下の順に 3 つの引数が与えられる。 最初の引数は訪れたノードへのポインタである。 2
75 つ目の引数には、内部ノードの場合は訪問回数に応じて \fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP が、 葉の場合は
76 \fBleaf\fP が与えられる。 (これらのシンボルは \fI<search.h>\fP で定義されている。)  3
77 つ目の引数はノードの深さで、根の場合は 0 である。
78 .PP
79 (より一般的には、\fBpreorder\fP, \fBpostorder\fP, \fBendorder\fP は \fBpreorder\fP, \fBinorder\fP,
80 \fBpostorder\fP として知られている: それぞれ、子要素を辿る前・最初の子要素を辿った後かつ 2 番目の子要素を辿る前・
81 子要素を辿った後ということを表している。 よって \fBpost\%order\fP という名前を選ぶのは少し紛らわしい。)
82 .PP
83 \fBtdestroy\fP()  は \fIroot\fP が指す木構造全体を削除し、 \fBtsearch\fP()  関数で確保されたリソースを全て解放する。
84 木構造の各ノードについて、関数 \fIfree_node\fP が呼び出される。 データへのポインタがこの関数の引数として渡される。
85 そのような動作が必要でなければ、 \fIfree_node\fP は何もしない関数へのポインタでなければならない。
86 .SH 返り値
87 \fBtsearch\fP()  は、木構造に見つかったアイテムか、 新しく追加したアイテムへのポインタを返す。
88 メモリの不足のためアイテムを追加できなかった場合は NULL を返す。 \fBtfind\fP()  は、アイテムへのポインタを返す。
89 一致するアイテムが見つからない場合は NULL を返す。 検索条件に一致する要素が複数ある場合、返される値は不定である。
90 .PP
91 \fBtdelete\fP()  は削除したアイテムの親へのポインタを返す。 アイテムが見つからなかった場合は NULL を返す。
92 .PP
93 \fIrootp\fP が NULL の場合、 \fBtsearch\fP(), \fBtfind\fP(), \fBtdelete\fP()  は NULL を返す。
94 .SH 準拠
95 SVr4, POSIX.1\-2001.  関数 \fBtdestroy\fP()  は GNU の拡張である。
96 .SH 注意
97 \fBtwalk\fP()  は根へのポインタを引数にとるが、 ほかの関数は根へのポインタへのポインタである。
98 .PP
99 \fBtwalk\fP()  においては、\fBpostorder\fP は 「左の部分木の後で、右の部分木の前」を意味している。 しかし、人によってはこれを
100 "inorder" と呼んで、 "postorder" を「両方の部分木の後」とする場合もある。
101 .PP
102 \fBtdelete\fP()  は、削除したノードの使用していたメモリを解放するが、 ノードに対応するデータのメモリは、ユーザが解放しなければならない。
103 .PP
104 下のプログラム例は、ユーザ関数が "endorder" か "leaf" を引数にして 呼び出されて以降は、 \fBtwalk\fP()
105 がそのノードを参照しないことを前提としている。 これは GNU ライブラリの実装では機能するが、System V のマニュアルには存在しない。
106 .SH 例
107 以下のプログラムは 12 個の乱数を二分木に挿入した後、 挿入した数を順番に出力する (挿入の際、重複した乱数は 1 つにまとめられる)。
108 .sp
109 .nf
110 #define _GNU_SOURCE     /* Expose declaration of tdestroy() */
111 #include <search.h>
112 #include <stdlib.h>
113 #include <stdio.h>
114 #include <time.h>
115
116 void *root = NULL;
117
118 void *
119 xmalloc(unsigned n)
120 {
121     void *p;
122     p = malloc(n);
123     if (p)
124         return p;
125     fprintf(stderr, "insufficient memory\en");
126     exit(EXIT_FAILURE);
127 }
128
129 int
130 compare(const void *pa, const void *pb)
131 {
132     if (*(int *) pa < *(int *) pb)
133         return \-1;
134     if (*(int *) pa > *(int *) pb)
135         return 1;
136     return 0;
137 }
138
139 void
140 action(const void *nodep, const VISIT which, const int depth)
141 {
142     int *datap;
143
144     switch (which) {
145     case preorder:
146         break;
147     case postorder:
148         datap = *(int **) nodep;
149         printf("%6d\en", *datap);
150         break;
151     case endorder:
152         break;
153     case leaf:
154         datap = *(int **) nodep;
155         printf("%6d\en", *datap);
156         break;
157     }
158 }
159
160 int
161 main(void)
162 {
163     int i, *ptr;
164     void *val;
165
166     srand(time(NULL));
167     for (i = 0; i < 12; i++) {
168         ptr = (int *) xmalloc(sizeof(int));
169         *ptr = rand() & 0xff;
170         val = tsearch((void *) ptr, &root, compare);
171         if (val == NULL)
172             exit(EXIT_FAILURE);
173         else if ((*(int **) val) != ptr)
174             free(ptr);
175     }
176     twalk(root, action);
177     tdestroy(root, free);
178     exit(EXIT_SUCCESS);
179 }
180 .fi
181 .SH 関連項目
182 \fBbsearch\fP(3), \fBhsearch\fP(3), \fBlsearch\fP(3)  \fBqsort\fP(3)