.\" -*- coding: UTF-8 -*- .\" Copyright 1995 by Jim Van Zandt .\" .\" %%%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. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .TH TSEARCH 3 "1 Noviembre 2020" GNU "Manual del Programador de Linux" .SH NOMBRE tsearch, tfind, tdelete, twalk, tdestroy \- manage a binary search tree .SH SINOPSIS .nf \fB#include \fP .PP \fBtypedef enum { preorder, postorder, endorder, leaf } VISIT;\fP .PP \fBvoid *tsearch(const void *\fP\fIkey\fP\fB, void **\fP\fIrootp\fP\fB,\fP \fB int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP .PP \fBvoid *tfind(const void *\fP\fIkey\fP\fB, void *const *\fP\fIrootp\fP\fB,\fP \fB int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP .PP \fBvoid *tdelete(const void *\fP\fIkey\fP\fB, void **\fP\fIrootp\fP\fB,\fP \fB int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP .PP \fBvoid twalk(const void *\fP\fIroot\fP\fB,\fP \fB void (*\fP\fIaction\fP\fB)(const void *\fP\fInodep\fP\fB, VISIT \fP\fIwhich\fP\fB,\fP \fB int \fP\fIdepth\fP\fB));\fP .PP \fB#define _GNU_SOURCE\fP /* Vea feature_test_macros(7) */ \fB#include \fP .PP \fBvoid twalk_r(const void *\fP\fIroot\fP\fB,\fP \fB void (*\fP\fIaction\fP\fB)(const void *\fP\fInodep\fP\fB, VISIT \fP\fIwhich\fP\fB,\fP \fB void *\fP\fIclosure\fP\fB),\fP \fB void *\fP\fIclosure\fP\fB);\fP .PP \fBvoid tdestroy(void *\fP\fIroot\fP\fB, void (*\fP\fIfree_node\fP\fB)(void *\fP\fInodep\fP\fB));\fP .fi .SH DESCRIPCIÓN \fBtsearch\fP(), \fBtfind\fP(), \fBtwalk\fP(), and \fBtdelete\fP() 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.) \fIcompar\fP 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. .PP \fBtsearch\fP() 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. If the tree is empty, then the variable that \fIrootp\fP points to should be set to NULL. If the item is found in the tree, then \fBtsearch\fP() returns a pointer to the corresponding tree node. (In other words, \fBtsearch\fP() returns a pointer to a pointer to the data item.) If the item is not found, then \fBtsearch\fP() adds it, and returns a pointer to the corresponding tree node. .PP \fBtfind\fP() es como \fBtsearch\fP(), sólo que si no encuentra el elemento \fBtfind\fP() devuelve NULL. .PP \fBtdelete\fP() borra un elemento del árbol. Sus argumentos son los mismos que los de \fBtsearch\fP(). .PP \fBtwalk\fP() performs depth\-first, left\-to\-right traversal of a binary tree. \fIroot\fP points to the starting node for the traversal. If that node is not the root, then only part of the tree will be visited. \fBtwalk\fP() calls the user function \fIaction\fP 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 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 \fBpreorder\fP, \fBpostorder\fP, or \fBendorder\fP depending on whether this is the first, second, or third visit to the internal node, or the value \fBleaf\fP if this is the single visit to a leaf node. (These symbols are defined in \fI\fP.) The third argument is the depth of the node; the root node has depth zero. .PP (Más comúnmente, \fBpreorder\fP, \fBpostorder\fP, y \fBendorder\fP son conocidos como \fBpreorder\fP, \fBinorder\fP, and \fBpostorder\fP: antes de visitar los hijos, después del primero y antes del segundo, y después de visitar los hijos. Así, la elección del nombre \fBpost\%order\fP es bastante confusa.) .PP \fBtwalk_r\fP() is similar to \fBtwalk\fP(), but instead of the \fIdepth\fP argument, the \fIclosure\fP 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. .PP \fBtdestroy\fP() elimina el árbol entero apuntado por \fIroot\fP, liberando todos los recursos reservados por la función \fBtsearch\fP. Para los datos en cada nodo del árbol se llama a la función \fIfree_node\fP. El puntero a los datos es pasado como argumento a la función. Si esta tarea no es necesaria \fIfree_node\fP debe apuntar a una función que no haga nada. .SH "VALOR DEVUELTO" \fBtsearch\fP() 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. \fBtfind\fP() 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. .PP \fBtdelete\fP() 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, \fBtdelete\fP() returns a dangling pointer that must not be accessed. .PP \fBtsearch\fP(), \fBtfind\fP(), y \fBtdelete\fP() devuelven NULL si \fIrootp\fP es NULL en la entrada a la función. .SH VERSIONES \fBtwalk_r\fP() is available in glibc since version 2.30. .SH ATRIBUTOS Para obtener una explicación de los términos usados en esta sección, véase \fBattributes\fP(7). .TS allbox; lb lb lb l l l. Interfaz Atributo Valor T{ \fBtsearch\fP(), \fBtfind\fP(), .br \fBtdelete\fP() T} Seguridad del hilo MT\-Safe race:rootp T{ \fBtwalk\fP() T} Seguridad del hilo MT\-Safe race:root T{ \fBtwalk_r\fP() T} Seguridad del hilo MT\-Safe race:root T{ \fBtdestroy\fP() T} Seguridad del hilo Multi\-hilo seguro .TE .SH "CONFORME A" POSIX.1\-2001, POSIX.1\-2008, SVr4. The functions \fBtdestroy\fP() and \fBtwalk_r\fP() are GNU extensions. .SH NOTAS \fBtwalk\fP() toma un puntero a la raíz, mientra que las otras funciones toman un puntero a una variable que apunta a la raíz. .PP \fBtdelete\fP() libera la memoria necesaria para el elemento en el árbol. El usuario es el responsable de liberar la memoria de los correspondientes datos. .PP El programa de ejemplo depende del hecho de que \fBtwalk\fP() no vuelve a referenciar a un nodo después de llamar a la función de usuario con el argumento "endorder" o "leaf". Esto funciona con la biblioteca de GNU, pero no está en la documentación System V. .SH EJEMPLOS El ejemplo siguiente inserta doce números aleatorios en un árbol binario, donde los números duplicados se meten hacia abajo, e imprime los números en orden. .PP .EX #define _GNU_SOURCE /* Expose declaration of tdestroy() */ #include #include #include #include #include static void *root = NULL; static void * xmalloc(size_t n) { void *p; p = malloc(n); if (p) return p; fprintf(stderr, "insufficient memory\en"); 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\en", *datap); break; case endorder: break; case leaf: datap = *(int **) nodep; printf("%6d\en", *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); } .EE .SH "VÉASE TAMBIÉN" \fBbsearch\fP(3), \fBhsearch\fP(3), \fBlsearch\fP(3), \fBqsort\fP(3) .SH COLOFÓN Esta página es parte de la versión 5.10 del proyecto Linux \fIman\-pages\fP. Puede encontrar una descripción del proyecto, información sobre cómo informar errores y la última versión de esta página en \%https://www.kernel.org/doc/man\-pages/. .PP .SH TRADUCCIÓN La traducción al español de esta página del manual fue creada por Carlos Gomez Romero y Miguel Pérez Ibars . .PP Esta traducción es documentación libre; lea la .UR https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 .UE o posterior con respecto a las condiciones de copyright. No existe NINGUNA RESPONSABILIDAD. .PP Si encuentra algún error en la traducción de esta página del manual, envíe un correo electrónico a .MT debian-l10n-spanish@lists.debian.org .ME .