TDELETE(3P)
π TDELETE(3P) β POSIX Programmer's Manual π
okay bestie, before we even get into this: this page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go read the corresponding Linux manual page for the actual Linux tea β), or the interface might just straight-up not be implemented on Linux at all. π
β¨ PROLOG β¨
This manual page is part of the POSIX Programmer's 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.
π NAME π
tdelete, tfind, tsearch, twalk β manage a binary search tree π³
π¦ SYNOPSIS π¦
#include <search.h>
void *tdelete(const void *restrict key, void **restrict rootp,
int(*compar)(const void *, const void *));
void *tfind(const void *key, void *const *rootp,
int(*compar)(const void *, const void *));
void *tsearch(const void *key, void **rootp,
int (*compar)(const void *, const void *));
void twalk(const void *root,
void (*action)(const void *, VISIT, int));
πΈ DESCRIPTION πΈ
so tdelete(), tfind(), tsearch(), and twalk() are the girlies that manipulate binary search trees. Comparisons get made with a user-supplied routine, and the address of that routine is what you pass in as the compar argument. This routine gets called with two arguments, which are 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. And no, the comparison function doesn't need to compare every single byte, so you can stash arbitrary data in the elements on top of the values being compared. flexible queen behavior π
The tsearch() function shall build and access the tree. The key argument is a pointer to an element to be accessed or stored. If there's a node in the tree whose element is equal to the value pointed to by key, a pointer to this found node shall be returned. Otherwise, the value pointed to by key shall be inserted (aka a new node is created and the value of key is copied to this node), and a pointer to this node returned. Heads up: only pointers are copied, so the application shall ensure that the calling routine stores the data. The rootp argument points to a variable that points to the root node of the tree. A null pointer value for the variable pointed to by rootp denotes an empty tree; in that case, the variable shall be set to point to the node which shall be at the root of the new tree. β¨
Like tsearch(), tfind() shall search for a node in the tree, returning a pointer to it if found. But β and this is the plot twist β if it's not found, tfind() shall return a null pointer. The arguments for tfind() are the same as for tsearch().
The tdelete() function shall delete a node from a binary search tree. Arguments are the same as for tsearch(). The variable pointed to by rootp 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 rootp shall be set to a null pointer. And tdelete() 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. π
bestie listen. Iftsearch()adds an element to a tree, ortdelete()successfully deletes an element from a tree, then the concurrent use of that tree in another thread, or use of pointers produced by a previous call totfind()ortsearch(), produces undefined results. no cap. don't do it.
The twalk() function shall traverse a binary search tree. The root 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 β cute, right? π§) The argument action 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:
typedef enum { preorder, postorder, endorder, leaf } VISIT;
(defined in <search.h>), 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.
β οΈ If the calling function alters the pointer to the root, the result is undefined.
β οΈ If the functions pointed to by action or compar (for any of these binary search functions) change the tree, the results are undefined. like literally do not.
These functions are thread-safe only as long as multiple threads do not access the same tree. that's the vibe check and they passed it, barely π«Ά
β RETURN VALUE β
If the node is found, both tsearch() and tfind() shall return a pointer to it. If not, tfind() shall return a null pointer, and tsearch() shall return a pointer to the inserted item.
A null pointer shall be returned by tsearch() if there is not enough space available to create a new node. π
A null pointer shall be returned by tdelete(), tfind(), and tsearch() if rootp is a null pointer on entry.
The tdelete() 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.
The twalk() function shall not return a value. it's giving "I don't do returns, I do vibes" π
π ERRORS π
No errors are defined.
The following sections are informative.
π EXAMPLES π
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. slay.
#include <limits.h>
#include <search.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct element { /* Pointers to these are stored in the tree. */
int count;
char string[];
};
void *root = NULL; /* This points to the root. */
int main(void)
{
char str[_POSIX2_LINE_MAX+1];
int length = 0;
struct element *elementptr;
void *node;
void print_node(const void *, VISIT, int);
int node_compare(const void *, const void *),
delete_root(const void *, const void *);
while (fgets(str, sizeof(str), stdin)) {
/* Set element. */
length = strlen(str);
if (str[length-1] == '\n')
str[--length] = '\0';
elementptr = malloc(sizeof(struct element) + length + 1);
strcpy(elementptr->string, str);
elementptr->count = 1;
/* Put element into the tree. */
node = tsearch((void *)elementptr, &root, node_compare);
if (node == NULL) {
fprintf(stderr,
"tsearch: Not enough space available\n");
exit(EXIT_FAILURE);
}
else if (*(struct element **)node != elementptr) {
/* A node containing the element already exists */
(*(struct element **)node)->count++;
free(elementptr);
}
}
twalk(root, print_node);
/* Delete all nodes in the tree */
while (root != NULL) {
elementptr = *(struct element **)root;
printf("deleting node: string = %s, count = %d\n",
elementptr->string,
elementptr->count);
tdelete((void *)elementptr, &root, delete_root);
free(elementptr);
}
return 0;
}
/*
* This routine compares two nodes, based on an
* alphabetical ordering of the string field.
*/
int
node_compare(const void *node1, const void *node2)
{
return strcmp(((const struct element *) node1)->string,
((const struct element *) node2)->string);
}
/*
* This comparison routine can be used with tdelete()
* when explicitly deleting a root node, as no comparison
* is necessary.
*/
int
delete_root(const void *node1, const void *node2)
{
return 0;
}
/*
* This routine prints out a node, the second time
* twalk encounters it or if it is a leaf.
*/
void
print_node(const void *ptr, VISIT order, int level)
{
const struct element *p = *(const struct element **) ptr;
if (order == postorder || order == leaf) {
(void) printf("string = %s, count = %d\n",
p->string, p->count);
}
}
π APPLICATION USAGE π
okay so the root argument to twalk() is one level of indirection less than the rootp arguments to tdelete() and tsearch(). yes it's confusing. yes you'll get it wrong once. we've all been there. π
There are two nomenclatures used to refer to the order in which tree nodes are visited:
twalk()usespreorder,postorder, andendorderto 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
preorder,inorder, andpostorderto refer to the same visits β which could result in some confusion over the meaning ofpostorder. π lowkey a naming crime but okay.
Since the return value of tdelete() 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 tdelete() as an 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. so like, it's giving "trust issues" fr fr π
π RATIONALE π
None.
π FUTURE DIRECTIONS π
None.
π¦ SEE ALSO π¦
hcreate()lsearch()
The Base Definitions volume of POSIX.1β2017, <search.h>
π COPYRIGHT π
okay bestie, legal era π some of this text is reprinted and reproduced in electronic form, and we are legally obligated to tell you exactly where it came from, so here we go:
- From IEEE Std 1003.1-2017, Standard for Information Technology -- 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 π«Ά
important!!! if there is ever a 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. like, it's the main character, this page is just the vibes β¨ no cap.
the original Standard can be obtained online at http://www.opengroup.org/unix/online.html π
π«£ the oopsie clause
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 β not me, not you, the conversion process did that π
to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html π
and that's the whole page, bestie, we're done!! π this one's from IEEE/The Open Group, dated 2017, and it lives at TDELETE(3P) β go forth and tdelete responsibly, ily, mwah ππ¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms