{"id":5275,"date":"2022-12-20T18:37:16","date_gmt":"2022-12-20T21:37:16","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/tdelete-man3p\/"},"modified":"2022-12-20T18:37:16","modified_gmt":"2022-12-20T21:37:16","slug":"tdelete-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/tdelete-man3p\/","title":{"rendered":"TDELETE (man3p)"},"content":{"rendered":"<h1 align=\"center\">TDELETE<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">tdelete, tfind, tsearch, twalk \u2014 manage a binary search tree<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <search.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">void *tdelete(const void *restrict <i>key<\/i>, void **restrict <i>rootp<\/i>, <br \/> int(*<i>compar<\/i>)(const void *, const void *)); <br \/> void *tfind(const void *<i>key<\/i>, void *const *<i>rootp<\/i>, <br \/> int(*<i>compar<\/i>)(const void *, const void *)); <br \/> void *tsearch(const void *<i>key<\/i>, void **<i>rootp<\/i>, <br \/> int (*<i>compar<\/i>)(const void *, const void *)); <br \/> void twalk(const void *<i>root<\/i>, <br \/> void (*<i>action<\/i>)(const void *, VISIT, int));<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>tdelete<\/i>(), <i>tfind<\/i>(), <i>tsearch<\/i>(), and <i>twalk<\/i>() functions manipulate binary search trees. Comparisons are made with a user-supplied routine, the address of which is passed as the <i>compar<\/i> argument. This routine is called with two arguments, which are the pointers to the elements being compared. The application shall ensure that the user-supplied routine returns an integer less than, equal to, or greater than 0, according to whether the first argument is to be considered less than, equal to, or greater than the second argument. The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>tsearch<\/i>() function shall build and access the tree. The <i>key<\/i> argument is a pointer to an element to be accessed or stored. If there is a node in the tree whose element is equal to the value pointed to by <i>key<\/i>, a pointer to this found node shall be returned. Otherwise, the value pointed to by <i>key<\/i> shall be inserted (that is, a new node is created and the value of <i>key<\/i> is copied to this node), and a pointer to this node returned. Only pointers are copied, so the application shall ensure that the calling routine stores the data. The <i>rootp<\/i> argument points to a variable that points to the root node of the tree. A null pointer value for the variable pointed to by <i>rootp<\/i> denotes an empty tree; in this case, the variable shall be set to point to the node which shall be at the root of the new tree.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Like <i>tsearch<\/i>(), <i>tfind<\/i>() shall search for a node in the tree, returning a pointer to it if found. However, if it is not found, <i>tfind<\/i>() shall return a null pointer. The arguments for <i>tfind<\/i>() are the same as for <i>tsearch<\/i>().<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>tdelete<\/i>() function shall delete a node from a binary search tree. The arguments are the same as for <i>tsearch<\/i>(). The variable pointed to by <i>rootp<\/i> shall be changed if the deleted node was the root of the tree. If the deleted node was the root of the tree and had no children, the variable pointed to by <i>rootp<\/i> shall be set to a null pointer. The <i>tdelete<\/i>() function shall return a pointer to the parent of the deleted node, or an unspecified non-null pointer if the deleted node was the root node, or a null pointer if the node is not found.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>tsearch<\/i>() adds an element to a tree, or <i>tdelete<\/i>() successfully deletes an element from a tree, the concurrent use of that tree in another thread, or use of pointers produced by a previous call to <i>tfind<\/i>() or <i>tsearch<\/i>(), produces undefined results.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>twalk<\/i>() function shall traverse a binary search tree. The <i>root<\/i> argument is a pointer to the root node of the tree to be traversed. (Any node in a tree may be used as the root for a walk below that node.) The argument <i>action<\/i> is the name of a routine to be invoked at each node. This routine is, in turn, called with three arguments. The first argument shall be the address of the node being visited. The structure pointed to by this argument is unspecified and shall not be modified by the application, but it shall be possible to cast a pointer-to-node into a pointer-to-pointer-to-element to access the element stored in the node. The second argument shall be a value from an enumeration data type:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">typedef enum { preorder, postorder, endorder, leaf } VISIT;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">(defined in <i><search.h><\/i>), depending on whether this is the first, second, or third time that the node is visited (during a depth-first, left-to-right traversal of the tree), or whether the node is a leaf. The third argument shall be the level of the node in the tree, with the root being level 0.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the calling function alters the pointer to the root, the result is undefined.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the functions pointed to by <i>action<\/i> or <i>compar<\/i> (for any of these binary search functions) change the tree, the results are undefined.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions are thread-safe only as long as multiple threads do not access the same tree.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">If the node is found, both <i>tsearch<\/i>() and <i>tfind<\/i>() shall return a pointer to it. If not, <i>tfind<\/i>() shall return a null pointer, and <i>tsearch<\/i>() shall return a pointer to the inserted item.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A null pointer shall be returned by <i>tsearch<\/i>() if there is not enough space available to create a new node.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A null pointer shall be returned by <i>tdelete<\/i>(), <i>tfind<\/i>(), and <i>tsearch<\/i>() if <i>rootp<\/i> is a null pointer on entry.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>tdelete<\/i>() function shall return a pointer to the parent of the deleted node, or an unspecified non-null pointer if the deleted node was the root node, or a null pointer if the node is not found.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>twalk<\/i>() function shall not return a value.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">No errors are defined.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The following code reads in strings and stores structures containing a pointer to each string and a count of its length. It then walks the tree, printing out the stored strings and their lengths in alphabetical order.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#include <limits.h> #include <search.h> <br \/> #include <stdlib.h> <br \/> #include <string.h> <br \/> #include <stdio.h><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct element { \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Pointers to these are stored in the tree. bodies\/ usr\/ <br \/> int count; <br \/> char string[]; <br \/> };<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">void *root = NULL; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var This points to the root. *\/<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">int main(void) <br \/> { <br \/> char str[_POSIX2_LINE_MAX+1]; <br \/> int length = 0; <br \/> struct element *elementptr; <br \/> void *node; <br \/> void print_node(const void *, VISIT, int); <br \/> int node_compare(const void *, const void *), <br \/> delete_root(const void *, const void *);<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">while (fgets(str, sizeof(str), stdin)) { <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Set element. bodies\/ usr\/ <br \/> length = strlen(str); <br \/> if (str[length-1] == &#8216;n&#8217;) <br \/> str[&#8211;length] = &#8216;\u0000&#8217;; <br \/> elementptr = malloc(sizeof(struct element) + length + 1); <br \/> strcpy(elementptr->string, str); <br \/> elementptr->count = 1; <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Put element into the tree. bodies\/ usr\/ <br \/> node = tsearch((void *)elementptr, &#038;root, node_compare); <br \/> if (node == NULL) { <br \/> fprintf(stderr, <br \/> &#8220;tsearch: Not enough space availablen&#8221;); <br \/> exit(EXIT_FAILURE); <br \/> } <br \/> else if (*(struct element **)node != elementptr) { <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var A node containing the element already exists bodies\/ usr\/ <br \/> (*(struct element **)node)->count++; <br \/> free(elementptr); <br \/> } <br \/> } <br \/> twalk(root, print_node);<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">\/* Delete all nodes in the tree bodies\/ usr\/ <br \/> while (root != NULL) { <br \/> elementptr = *(struct element **)root; <br \/> printf(&#8220;deleting node: string = %s, count = %dn&#8221;, <br \/> elementptr->string, <br \/> elementptr->count); <br \/> tdelete((void *)elementptr, &#038;root, delete_root); <br \/> free(elementptr); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">return 0; <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">\/* <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr This routine compares two nodes, based on an <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr alphabetical ordering of the string field. <br \/> bodies\/ usr\/ <br \/> int <br \/> node_compare(const void *node1, const void *node2) <br \/> { <br \/> return strcmp(((const struct element *) node1)->string, <br \/> ((const struct element *) node2)->string); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">\/* <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr This comparison routine can be used with tdelete() <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr when explicitly deleting a root node, as no comparison <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr is necessary. <br \/> bodies\/ usr\/ <br \/> int <br \/> delete_root(const void *node1, const void *node2) <br \/> { <br \/> return 0; <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">\/* <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr This routine prints out a node, the second time <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr twalk encounters it or if it is a leaf. <br \/> bodies\/ usr\/ <br \/> void <br \/> print_node(const void *ptr, VISIT order, int level) <br \/> { <br \/> const struct element *p = *(const struct element **) ptr;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">if (order == postorder || order == leaf) { <br \/> (void) printf(&#8220;string = %s, count = %dn&#8221;, <br \/> p->string, p->count); <br \/> } <br \/> }<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>root<\/i> argument to <i>twalk<\/i>() is one level of indirection less than the <i>rootp<\/i> arguments to <i>tdelete<\/i>() and <i>tsearch<\/i>().<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">There are two nomenclatures used to refer to the order in which tree nodes are visited. The <i>twalk<\/i>() function uses <b>preorder<\/b>, <b>postorder<\/b>, and <b>endorder<\/b> to refer respectively to visiting a node before any of its children, after its left child and before its right, and after both its children. The alternative nomenclature uses <b>preorder<\/b>, <b>inorder<\/b>, and <b>postorder<\/b> to refer to the same visits, which could result in some confusion over the meaning of <b>postorder<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Since the return value of <i>tdelete<\/i>() is an unspecified non-null pointer in the case that the root of the tree has been deleted, applications should only use the return value of <i>tdelete<\/i>() as indication of success or failure and should not assume it can be dereferenced. Some implementations in this case will return a pointer to the new root of the tree (or to an empty tree if the deleted root node was the only node in the tree); other implementations return arbitrary non-null pointers.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>hcreate<\/i>(), <i>lsearch<\/i>()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <b><search.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http:\/\/www.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https:\/\/www.kernel.org\/doc\/man-pages\/reporting_bugs.html .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  tdelete, tfind, tsearch, twalk \u2014 manage a binary search tree <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[1594,2164],"class_list":["post-5275","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-man3p","tag-tdelete"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=5275"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5275\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=5275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=5275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=5275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}