Scroll to navigation

TSEARCH(3) Linux Programmer's Manual TSEARCH(3)

名前

tsearch, tfind, tdelete, twalk, tdestroy - 二分探索木 (binary search tree) の操作

書式

#include <search.h>
typedef enum { preorder, postorder, endorder, leaf } VISIT;
void *tsearch(const void *key, void **rootp,
                int (*compar)(const void *, const void *));
void *tfind(const void *key, void *const *rootp,
                int (*compar)(const void *, const void *));
void *tdelete(const void *key, void **rootp,
                int (*compar)(const void *, const void *));
void twalk(const void *root,
                void (*action)(const void *nodep, VISIT which,
                               int depth));
#define _GNU_SOURCE         /* feature_test_macros(7) 参照 */
#include <search.h>
void twalk_r(const void *root,
                void (*action)(const void *nodep, VISIT which,
                               void *closure),
                void *closure);
void tdestroy(void *root, void (*free_node)(void *nodep));

説明

tsearch(), tfind(), twalk(), tdelete() は 二分探索木を操作する関数である。 これらの関数は Knuth (6.2.2) Algorithm T に基づいている。 木構造における各ノードの最初のフィールドは、対応するデータ アイテムへのポインターである。 (参照先のデータは、呼び出しプログラムで用意する。) compar は比較ルーチンへのポインターである。 比較ルーチンは、アイテムへのポインター 2 つを引数に持つ。 比較ルーチンの返り値は、1 つ目のアイテムが 2 つ目のアイテムよりも 「小さい、等しい、大きい」によって、 「負、0、正」の整数値でなければならない。

tsearch() searches the tree for an item. key points to the item to be searched for. rootp points to a variable which points to the root of the tree. If the tree is empty, then the variable that rootp points to should be set to NULL. If the item is found in the tree, then tsearch() returns a pointer to the corresponding tree node. (In other words, tsearch() returns a pointer to a pointer to the data item.) If the item is not found, then tsearch() adds it, and returns a pointer to the corresponding tree node.

tfind() は、 tsearch() に似ているが、 アイテムが見つからなかった場合 NULL を返す点が異なる。

tdelete() は木構造からアイテムを削除する。 引数は tsearch() と同じである。

twalk() は、二分木を深さ優先 (depth-first) で、 左から右にたどっていく関数である。 root は起点となるノードへのポインターである。 root に根以外のノードを指定すると、部分木が対象となる。 twalk() は、ノードを訪れる度にユーザー関数 action を呼び出す (内部ノードに対しては 3 回、葉に対しては 1 回呼び出しが行われる)。 action には以下の順に 3 つの引数が与えられる。 最初の引数は訪れたノードへのポインターである。 ノードの構造体は規定されていないが、 ポインターを要素へのポインターのポインターにキャストし、 ノードに格納された要素にアクセスすることができる。 アプリケーションは、この引数が指す構造体を変更してはならない。 2 番目の引数には、内部ノードの場合は訪問回数に応じて preorder, postorder, endorder のいずれかの整数が、 葉を最初に訪れた場合は leaf の値が渡される (これらのシンボルは <search.h> で定義されている)。 3 番目の引数はノードの深さで、根の場合は深さ 0 である。

(より一般的には、preorder, postorder, endorderpreorder, inorder, postorder として知られている: それぞれ、子要素を辿る前・最初の子要素を辿った後かつ 2 番目の子要素を辿る前・ 子要素を辿った後ということを表している。 よって postorder という名前を選ぶのは少し紛らわしい。)

twalk_r() is similar to twalk(), but instead of the depth argument, the closure argument pointer is passed to each invocation of the action callback, unchanged. This pointer can be used to pass information to and from the callback function in a thread-safe fashion, without resorting to global variables.

tdestroy() は root が指す木構造全体を削除し、 tsearch() 関数で確保されたリソースを全て解放する。 木構造の各ノードについて、関数 free_node が呼び出される。 データへのポインターがこの関数の引数として渡される。 そのような動作が必要でなければ、 free_node は何もしない関数へのポインターでなければならない。

返り値

tsearch() returns a pointer to a matching node in the tree, or to the newly added node, or NULL if there was insufficient memory to add the item. tfind() returns a pointer to the node, or NULL if no match is found. If there are multiple items that match the key, the item whose node is returned is unspecified.

tdelete() returns a pointer to the parent of the node deleted, or NULL if the item was not found. If the deleted node was the root node, tdelete() returns a dangling pointer that must not be accessed.

rootp が NULL の場合、 tsearch(), tfind(), tdelete() は NULL を返す。

バージョン

twalk_r() は glibc バージョン 2.30 以降で利用可能である。

属性

この節で使用されている用語の説明については、 attributes(7) を参照。

インターフェース 属性
tsearch(), tfind(), tdelete() Thread safety MT-Safe race:rootp
twalk() Thread safety MT-Safe race:root
twalk_r() Thread safety MT-Safe race:root
tdestroy() Thread safety MT-Safe

準拠

POSIX.1-2001, POSIX.1-2008, SVr4. The functions tdestroy() and twalk_r() are GNU extensions.

注意

twalk() は根へのポインターを引数にとるが、 ほかの関数は根へのポインターへのポインターである。

tdelete() は、削除したノードの使用していたメモリーを解放するが、 ノードに対応するデータのメモリーは、ユーザーが解放しなければならない。

下のプログラム例は、ユーザー関数が "endorder" か "leaf" を引数にして 呼び出されて以降は、 twalk() がそのノードを参照しないことを前提としている。 これは GNU ライブラリの実装では機能するが、System V のマニュアルには存在しない。

以下のプログラムは 12 個の乱数を二分木に挿入した後、 挿入した数を順番に出力する (挿入の際、重複した乱数は 1 つにまとめられる)。

#define _GNU_SOURCE     /* Expose declaration of tdestroy() */
#include <search.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
static void *root = NULL;
static void *
xmalloc(size_t n)
{

void *p;
p = malloc(n);
if (p)
return p;
fprintf(stderr, "insufficient memory\n");
exit(EXIT_FAILURE); } static int compare(const void *pa, const void *pb) {
if (*(int *) pa < *(int *) pb)
return -1;
if (*(int *) pa > *(int *) pb)
return 1;
return 0; } static void action(const void *nodep, VISIT which, 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 **val;
srand(time(NULL));
for (int i = 0; i < 12; i++) {
int *ptr = xmalloc(sizeof(*ptr));
*ptr = rand() & 0xff;
val = tsearch(ptr, &root, compare);
if (val == NULL)
exit(EXIT_FAILURE);
else if (*val != ptr)
free(ptr);
}
twalk(root, action);
tdestroy(root, free);
exit(EXIT_SUCCESS); }

関連項目

bsearch(3), hsearch(3), lsearch(3) qsort(3)

この文書について

この man ページは Linux man-pages プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は https://www.kernel.org/doc/man-pages/ に書かれている。

2020-11-01 GNU