-.\" Hey Emacs! This file is -*- nroff -*- source.
.\" Copyright 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
.\"
+.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
.\"
-.TH TSEARCH 3 2008-09-23 "GNU" "Linux Programmer's Manual"
+.TH TSEARCH 3 2014-05-28 "GNU" "Linux Programmer's Manual"
.SH NAME
tsearch, tfind, tdelete, twalk, tdestroy \- manage a binary tree
.SH SYNOPSIS
.BI "void *tsearch(const void *" key ", void **" rootp ,
.BI " int (*" compar ")(const void *, const void *));"
.sp
-.BI "void *tfind(const void *" key ", const void **" rootp ,
+.BI "void *tfind(const void *" key ", void *const *" rootp ,
.BI " int (*" compar ")(const void *, const void *));"
.sp
.BI "void *tdelete(const void *" key ", void **" rootp ,
.BI " const VISIT " which ,
.BI " const int " depth "));"
.sp
-.B #define _GNU_SOURCE
+.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
.br
.B #include <search.h>
.sp
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.)
-\fIcompar\fP points to a comparison routine, which takes
+.IR 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,
.PP
.BR tsearch ()
searches the tree for an item.
-\fIkey\fP points to the item to be searched for.
-\fIrootp\fP points to a variable which points to the root of the tree.
+.IR key
+points to the item to be searched for.
+.IR rootp
+points to a variable which points to the root of the tree.
If the tree is empty,
-then the variable that \fIrootp\fP points to should be set to NULL.
+then the variable that
+.IR rootp
+points to should be set to NULL.
If the item is found in the tree, then
.BR tsearch ()
returns a pointer
.BR twalk ()
performs depth-first, left-to-right traversal of a binary
tree.
-\fIroot\fP points to the starting node for the traversal.
+.IR 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.
.BR twalk ()
-calls the user function \fIaction\fP each time a node is
+calls the user function
+.IR action
+each time a node is
visited (that is, three times for an internal node, and once for a
leaf).
-\fIaction\fP, 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 \fBpreorder\fP, \fBpostorder\fP, and
-\fBendorder\fP depending on whether this is the first, second, or
-third visit to the internal node, or \fBleaf\fP if it is the single
-visit to a leaf node.
-(These symbols are defined in \fI<search.h>\fP.)
-The third argument is the depth of the node, with
-zero being the root.
+.IR 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
+.BR preorder ,
+.BR postorder ,
+or
+.BR endorder
+depending on whether this is the first, second, or
+third visit to the internal node,
+or the value
+.BR leaf
+if this is the single visit to a leaf node.
+(These symbols are defined in
+.IR <search.h> .)
+The third argument is the depth of the node;
+the root node has depth zero.
.PP
-(More commonly, \fBpreorder\fP, \fBpostorder\fP, and \fBendorder\fP
-are known as \fBpreorder\fP, \fBinorder\fP, and \fBpostorder\fP:
+(More commonly,
+.BR preorder ,
+.BR postorder ,
+and
+.BR endorder
+are known as
+.BR preorder ,
+.BR inorder ,
+and
+.BR postorder :
before visiting the children, after the first and before the second,
and after visiting the children.
-Thus, the choice of name \fBpost\%order\fP
+Thus, the choice of name
+.BR post\%order
is rather confusing.)
.PP
.BR tdestroy ()
-removes the whole tree pointed to by \fIroot\fP,
+removes the whole tree pointed to by
+.IR root ,
freeing all resources allocated by the
.BR tsearch ()
function.
-For the data in each tree node the function \fIfree_node\fP is called.
+For the data in each tree node the function
+.IR free_node
+is called.
The pointer to the data is passed as the argument to the function.
-If no such work is necessary \fIfree_node\fP must point to a function
+If no such work is necessary,
+.IR free_node
+must point to a function
doing nothing.
-.SH "RETURN VALUE"
+.SH RETURN VALUE
.BR tsearch ()
returns a pointer to a matching item in the tree, or to
the newly added item, or NULL if there was insufficient memory
and
.BR tdelete ()
also
-return NULL if \fIrootp\fP was NULL on entry.
-.SH "CONFORMING TO"
+return NULL if
+.IR rootp
+was NULL on entry.
+.SH CONFORMING TO
SVr4, POSIX.1-2001.
The function
.BR tdestroy ()
takes a pointer to the root, while the other functions
take a pointer to a variable which points to the root.
.PP
-.BR twalk ()
-uses \fBpostorder\fP 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".
-.PP
.BR tdelete ()
frees the memory required for the node in the tree.
The user is responsible for freeing the memory for the corresponding
#include <stdio.h>
#include <time.h>
-void *root = NULL;
+static void *root = NULL;
-void *
+static void *
xmalloc(unsigned n)
{
void *p;
exit(EXIT_FAILURE);
}
-int
+static int
compare(const void *pa, const void *pb)
{
if (*(int *) pa < *(int *) pb)
return 0;
}
-void
+static void
action(const void *nodep, const VISIT which, const int depth)
{
int *datap;
srand(time(NULL));
for (i = 0; i < 12; i++) {
- ptr = (int *) xmalloc(sizeof(int));
+ ptr = xmalloc(sizeof(int));
*ptr = rand() & 0xff;
val = tsearch((void *) ptr, &root, compare);
if (val == NULL)
exit(EXIT_SUCCESS);
}
.fi
-.SH "SEE ALSO"
+.SH SEE ALSO
.BR bsearch (3),
.BR hsearch (3),
.BR lsearch (3),
-.BR qsort (3),
-.BR feature_test_macros (7)
+.BR qsort (3)
+.SH COLOPHON
+This page is part of release 3.79 of the Linux
+.I man-pages
+project.
+A description of the project,
+information about reporting bugs,
+and the latest version of this page,
+can be found at
+\%http://www.kernel.org/doc/man\-pages/.