tsearchのヘルプ・マニュアル
日本語 英語
tsearch --help
man tsearch
TSEARCH(3) Linux Programmer’s Manual TSEARCH(3)
名前
tsearch, tfind, tdelete, twalk, tdestroy - 二分木 (binary tree) の操作
書式
#include
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
#include
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() は、二分木を深さ優先 (depth-first) で、左から右にたどっていく関
数である。 root は起点となるノードへのポインタである。 root に根以外 の
ノードを指定すると、部分木が対象となる。 twalk() は、ノードを訪れる度に
(つまり、内部ノードに対しては 3 回、葉に対しては 1 回) ユ ー ザ 関 数
action を呼び出す。 action には以下の順に 3 つの引数が与えられる。最初
の引数は訪れたノードへのポインタである。 2 つ目の引数には、内部ノードの
場 合 は 訪 問回数に応じて preorder, postorder, endorder が、葉の場合は
leaf が与えられる。 (これらのシンボルは で定義されている 。)
3 つ目の引数はノードの深さで、根の場合は 0 である。
( よ り 一般的には、preorder, postorder, endorder は preorder, 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() は根へのポインタを引数にとるが、ほかの関数は根へのポインタへの
ポインタである。
twalk() においては、postorder は「左の部分木の後で、右の部分木の前」 を
意 味 し て いる。しかし、人によってはこれを "inorder" と呼んで、 "pos-
torder" を「両方の部分木の後」とする場合もある。
tdelete() は、削除したノードの使用していたメモリを解放するが、ノード に
対応するデータのメモリは、ユーザが解放しなければならない。
下 のプログラム例は、ユーザ関数が "endorder" か "leaf" を引数にして呼び
出されて以降は、 twalk() がそのノードを参照しないことを前提としている。
こ れは GNU ライブラリの実装では機能するが、System V のマニュアルには存
在しない。
例
以下のプログラムは 12 個の乱数を二分木に挿入した後、挿入した数を順番 に
出力する (挿入の際、重複した乱数は 1 つにまとめられる)。
#define _GNU_SOURCE /* Expose declaration of tdestroy() */
#include
#include
#include
#include
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);
}
関連項目
bsearch(3), hsearch(3), lsearch(3) qsort(3), feature_test_macros(7)
GNU 2008-09-23 TSEARCH(3)
TSEARCH(3) Linux Programmer’s Manual TSEARCH(3)
NAME
tsearch, tfind, tdelete, twalk, tdestroy - manage a binary tree
SYNOPSIS
#include
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
#include
void tdestroy(void *root, void (*free_node)(void *nodep));
DESCRIPTION
tsearch(), tfind(), twalk(), and tdelete() manage a binary tree. They
are generalized from Knuth (6.2.2) Algorithm T. The first field in
each node of the tree is a pointer to the corresponding data item.
(The calling program must store the actual data.) compar points to a
comparison routine, which takes pointers to two items. It should
return an integer which is negative, zero, or positive, depending on
whether the first item is less than, equal to, or greater than the sec-
ond.
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 it. If it is not found, then tsearch()
adds it, and returns a pointer to the newly added item.
tfind() is like tsearch(), except that if the item is not found, then
tfind() returns NULL.
tdelete() deletes an item from the tree. Its arguments are the same as
for 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 is a pointer to the node being
visited. The second is an integer which takes on the values preorder,
postorder, and endorder depending on whether this is the first, second,
or third visit to the internal node, or leaf if it is the single visit
to a leaf node. (These symbols are defined in .) The third
argument is the depth of the node, with zero being the root.
(More commonly, preorder, postorder, and endorder are known as pre-
order, inorder, and postorder: before visiting the children, after the
first and before the second, and after visiting the children. Thus,
the choice of name postorder is rather confusing.)
tdestroy() removes the whole tree pointed to by root, freeing all
resources allocated by the tsearch() function. For the data in each
tree node the function free_node is called. The pointer to the data is
passed as the argument to the function. If no such work is necessary
free_node must point to a function doing nothing.
RETURN VALUE
tsearch() returns a pointer to a matching item in the tree, or to the
newly added item, or NULL if there was insufficient memory to add the
item. tfind() returns a pointer to the item, or NULL if no match is
found. If there are multiple elements that match the key, the element
returned is unspecified.
tdelete() returns a pointer to the parent of the item deleted, or NULL
if the item was not found.
tsearch(), tfind(), and tdelete() also return NULL if rootp was NULL on
entry.
CONFORMING TO
SVr4, POSIX.1-2001. The function tdestroy() is a GNU extension.
NOTES
twalk() takes a pointer to the root, while the other functions take a
pointer to a variable which points to the root.
twalk() uses postorder to mean "after the left subtree, but before the
right subtree". Some authorities would call this "inorder", and
reserve "postorder" to mean "after both subtrees".
tdelete() frees the memory required for the node in the tree. The user
is responsible for freeing the memory for the corresponding data.
The example program depends on the fact that twalk() makes no further
reference to a node after calling the user function with argument
"endorder" or "leaf". This works with the GNU library implementation,
but is not in the System V documentation.
EXAMPLE
The following program inserts twelve random numbers into a binary tree,
where duplicate numbers are collapsed, then prints the numbers in
order.
#define _GNU_SOURCE /* Expose declaration of tdestroy() */
#include
#include
#include
#include
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);
}
SEE ALSO
bsearch(3), hsearch(3), lsearch(3), qsort(3), feature_test_macros(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2008-09-23 TSEARCH(3)