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

名前

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

書式


#include <search.h>
 

void *tsearch(const void * key , void ** rootp ,
int (* compar )(const void *, const void *));
 

void *tfind(const void * key , const void ** 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 ,
const VISIT which ,
const int depth ));
 

#define _GNU_SOURCE /* feature_test_macros(7) 参照 */
 

#include <search.h>
 

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() は、木構造からアイテムを検索する関数である。 key は、検索するアイテムへのポインタである。 rootp は木構造の根へのポインタへのポインタである。木構造がノードを含まない場合、 rootp の参照している変数は NULL に設定されていなければならない。木構造にアイテムが見つかった場合、 tsearch() はそのアイテムへのポインタを返す。見つからなかった場合は、アイテムを木構造に追加し、追加したアイテムへのポインタを返す。

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

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

twalk() performs depth-first, left-to-right traversal of a binary tree. root points to the starting node for the traversal. If that node is not the root, then only part of the tree will be visited. twalk() calls the user function action each time a node is visited (that is, three times for an internal node, and once for a leaf). action, in turn, takes three arguments. The first argument is a pointer to the node being visited. The structure of the node is unspecified, but it is possible to cast the pointer to a pointer-to-pointer-to-element in order to access the element stored within the node. The application must not modify the structure pointed to by this argument. The second argument is an integer which takes one of the values preorder, postorder, or endorder depending on whether this is the first, second, or third visit to the internal node, or the value leaf if this is the single visit to a leaf node. (These symbols are defined in <search.h>.) The third argument is the depth of the node; the root node has depth zero.

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

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

返り値

tsearch() は、木構造に見つかったアイテムか、新しく追加したアイテムへのポインタを返す。メモリの不足のためアイテムを追加できなかった場合は NULL を返す。 tfind() は、アイテムへのポインタを返す。一致するアイテムが見つからない場合は NULL を返す。検索条件に一致する要素が複数ある場合、返される値は不定である。

tdelete() は削除したアイテムの親へのポインタを返す。アイテムが見つからなかった場合は NULL を返す。

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

準拠

SVr4, POSIX.1-2001. 関数 tdestroy() は GNU の拡張である。

注意

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

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

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

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

#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 = 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);
}

関連項目

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

この文書について

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