Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


tsearch(3) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ATTRIBUTES |STANDARDS |HISTORY |NOTES |EXAMPLES |SEE ALSO |COLOPHON

tsearch(3)               Library Functions Manualtsearch(3)

NAME        top

       tsearch, tfind, tdelete, twalk, twalk_r, tdestroy - manage a       binary search tree

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

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

DESCRIPTION        top

tsearch(),tfind(),twalk(), andtdelete() manage a binary search       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 second.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       thatrootp points to should be set to NULL.  If the item is found       in the tree, thentsearch() 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, thentsearch() adds it, and returns a pointer to the corresponding tree       node.tfind() is liketsearch(), except that if the item is not found,       thentfind() returns NULL.tdelete() deletes an item from the tree.  Its arguments are the       same as fortsearch().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 functionaction 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 valuespreorder,postorder, orendorderdepending on whether this is the first, second, or third       visit to the internal node, or the valueleafif 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.       (More commonly,preorder,postorder, andendorderare known aspreorder,inorder, andpostorder: before visiting the children,       after the first and before the second, and after visiting the       children.  Thus, the choice of namepostorderis rather       confusing.)twalk_r() is similar totwalk(), but instead of thedepth       argument, theclosure 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() removes the whole tree pointed to byroot, freeing all       resources allocated by thetsearch() function.  For the data in       each tree node the functionfree_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        top

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.tsearch(),tfind(), andtdelete() also return NULL ifrootp was       NULL on entry.

ATTRIBUTES        top

       For an explanation of the terms used in this section, seeattributes(7).       ┌───────────────────────────┬───────────────┬────────────────────┐       │InterfaceAttributeValue│       ├───────────────────────────┼───────────────┼────────────────────┤       │tsearch(),tfind(),       │ Thread safety │ MT-Safe race:rootp │       │tdelete()                 │               │                    │       ├───────────────────────────┼───────────────┼────────────────────┤       │twalk()                   │ Thread safety │ MT-Safe race:root  │       ├───────────────────────────┼───────────────┼────────────────────┤       │twalk_r()                 │ Thread safety │ MT-Safe race:root  │       ├───────────────────────────┼───────────────┼────────────────────┤       │tdestroy()                │ Thread safety │ MT-Safe            │       └───────────────────────────┴───────────────┴────────────────────┘

STANDARDS        top

tsearch()tfind()tdelete()twalk()              POSIX.1-2008.tdestroy()twalk_r()              GNU.

HISTORY        top

tsearch()tfind()tdelete()twalk()              POSIX.1-2001, POSIX.1-2008, SVr4.twalk_r()              glibc 2.30.

NOTES        top

twalk() takes a pointer to the root, while the other functions       take a pointer to a variable which points to the root.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 thattwalk() 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.

EXAMPLES        top

       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 <search.h>       #include <stddef.h>       #include <stdio.h>       #include <stdlib.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  *ptr;           int  **val;           srand(time(NULL));           for (unsigned int i = 0; i < 12; i++) {               ptr = xmalloc(sizeof(*ptr));               *ptr = rand() & 0xff;               val = tsearch(ptr, &root, compare);               if (val == NULL)                   exit(EXIT_FAILURE);               if (*val != ptr)                   free(ptr);           }           twalk(root, action);           tdestroy(root, free);           exit(EXIT_SUCCESS);       }

SEE ALSO        top

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

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17tsearch(3)

Pages that refer to this page:bsearch(3)hsearch(3)lsearch(3)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp