1.\" $NetBSD$ 2.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 3. The name of the author may not be used to endorse or promote products 14.\" derived from this software without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 17.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 18.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 19.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26.\" 27.\" OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp 28.\" $FreeBSD$ 29.\" 30.Dd December 6, 2015 31.Dt TSEARCH 3 32.Os 33.Sh NAME 34.Nm tsearch , tfind , tdelete , twalk 35.Nd manipulate binary search trees 36.Sh SYNOPSIS 37.In search.h 38.Ft void * 39.Fn tdelete "const void * restrict key" "void ** restrict rootp" "int (*compar) (const void *, const void *)" 40.Ft void * 41.Fn tfind "const void *key" "void * const *rootp" "int (*compar) (const void *, const void *)" 42.Ft void * 43.Fn tsearch "const void *key" "void **rootp" "int (*compar) (const void *, const void *)" 44.Ft void 45.Fn twalk "const void *root" "void (*action) (const void *, VISIT, int)" 46.Sh DESCRIPTION 47The 48.Fn tdelete , 49.Fn tfind , 50.Fn tsearch , 51and 52.Fn twalk 53functions manage binary search trees. 54This implementation uses a balanced AVL tree, 55which due to its strong theoretical limit on the height of the tree has 56the advantage of calling the comparison function relatively 57infrequently. 58.Pp 59The comparison function passed in by 60the user has the same style of return values as 61.Xr strcmp 3 . 62.Pp 63The 64.Fn tfind 65function 66searches for the datum matched by the argument 67.Fa key 68in the binary tree rooted at 69.Fa rootp , 70returning a pointer to the datum if it is found and NULL 71if it is not. 72.Pp 73The 74.Fn tsearch 75function 76is identical to 77.Fn tfind 78except that if no match is found, 79.Fa key 80is inserted into the tree and a pointer to it is returned. 81If 82.Fa rootp 83points to a NULL value a new binary search tree is created. 84.Pp 85The 86.Fn tdelete 87function 88deletes a node from the specified binary search tree and returns 89a pointer to the parent of the node to be deleted. 90It takes the same arguments as 91.Fn tfind 92and 93.Fn tsearch . 94If the node to be deleted is the root of the binary search tree, 95.Fa rootp 96will be adjusted. 97.Pp 98The 99.Fn twalk 100function 101walks the binary search tree rooted in 102.Fa root 103and calls the function 104.Fa action 105on each node. 106The 107.Fa action 108function 109is called with three arguments: a pointer to the current node, 110a value from the enum 111.Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;" 112specifying the traversal type, and a node level (where level 113zero is the root of the tree). 114.Sh RETURN VALUES 115The 116.Fn tsearch 117function returns NULL if allocation of a new node fails (usually 118due to a lack of free memory). 119.Pp 120The 121.Fn tfind , 122.Fn tsearch , 123and 124.Fn tdelete 125functions 126return NULL if 127.Fa rootp 128is NULL or the datum cannot be found. 129.Pp 130The 131.Fn twalk 132function returns no value. 133.Sh SEE ALSO 134.Xr bsearch 3 , 135.Xr hsearch 3 , 136.Xr lsearch 3 137