1*eecb0b5eSjruoho.\" $NetBSD: tsearch.3,v 1.12 2010/04/30 10:09:23 jruoho Exp $ 27975455dSchristos.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 37975455dSchristos.\" All rights reserved. 47975455dSchristos.\" 57975455dSchristos.\" Redistribution and use in source and binary forms, with or without 67975455dSchristos.\" modification, are permitted provided that the following conditions 77975455dSchristos.\" are met: 87975455dSchristos.\" 1. Redistributions of source code must retain the above copyright 97975455dSchristos.\" notice, this list of conditions and the following disclaimer. 107975455dSchristos.\" 2. Redistributions in binary form must reproduce the above copyright 117975455dSchristos.\" notice, this list of conditions and the following disclaimer in the 127975455dSchristos.\" documentation and/or other materials provided with the distribution. 137975455dSchristos.\" 3. The name of the author may not be used to endorse or promote products 147975455dSchristos.\" derived from this software without specific prior written permission. 157975455dSchristos.\" 167975455dSchristos.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 177975455dSchristos.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 187975455dSchristos.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 197975455dSchristos.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 207975455dSchristos.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 217975455dSchristos.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 227975455dSchristos.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 237975455dSchristos.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 247975455dSchristos.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 257975455dSchristos.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 267975455dSchristos.\" 277975455dSchristos.\" OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp 287975455dSchristos.\" 29cb375ee4Sjruoho.Dd April 30, 2010 307975455dSchristos.Dt TSEARCH 3 317975455dSchristos.Os 327975455dSchristos.Sh NAME 337975455dSchristos.Nm tsearch, tfind, tdelete, twalk 347975455dSchristos.Nd manipulate binary search trees 357975455dSchristos.Sh SYNOPSIS 36472351e1Swiz.In search.h 377975455dSchristos.Ft void * 3898061f1fSkleink.Fn tdelete "const void * restrict key" "void ** restrict rootp" "int (*compar) (const void *, const void *)" 397975455dSchristos.Ft void * 4098061f1fSkleink.Fn tfind "const void *key" "const void * const *rootp" "int (*compar) (const void *, const void *)" 417975455dSchristos.Ft void * 42e19fe12bSwiz.Fn tsearch "const void *key" "void **rootp" "int (*compar) (const void *, const void *)" 437975455dSchristos.Ft void 44179bda47Ssimonb.Fn twalk "const void *root" "void (*action) (const void *, VISIT, int)" 457975455dSchristos.Sh DESCRIPTION 467975455dSchristosThe 477975455dSchristos.Fn tdelete , 487975455dSchristos.Fn tfind , 497975455dSchristos.Fn tsearch , 507975455dSchristosand 517975455dSchristos.Fn twalk 527975455dSchristosfunctions manage binary search trees based on algorithms T and D 536569c5c5Swizfrom Knuth (6.2.2). 546569c5c5SwizThe comparison function passed in by 557975455dSchristosthe user has the same style of return values as 567975455dSchristos.Xr strcmp 3 . 577975455dSchristos.Pp 588e60b836Selad.Fn tfind 597975455dSchristossearches for the datum matched by the argument 607975455dSchristos.Fa key 617975455dSchristosin the binary tree rooted at 627975455dSchristos.Fa rootp , 637975455dSchristosreturning a pointer to the datum if it is found and NULL 647975455dSchristosif it is not. 657975455dSchristos.Pp 668e60b836Selad.Fn tsearch 677975455dSchristosis identical to 687975455dSchristos.Fn tfind 697975455dSchristosexcept that if no match is found, 707975455dSchristos.Fa key 716569c5c5Swizis inserted into the tree and a pointer to it is returned. 726569c5c5SwizIf 737975455dSchristos.Fa rootp 747975455dSchristospoints to a NULL value a new binary search tree is created. 757975455dSchristos.Pp 768e60b836Selad.Fn tdelete 777975455dSchristosdeletes a node from the specified binary search tree and returns 787975455dSchristosa pointer to the parent of the node to be deleted. 797975455dSchristosIt takes the same arguments as 807975455dSchristos.Fn tfind 817975455dSchristosand 827975455dSchristos.Fn tsearch . 837975455dSchristosIf the node to be deleted is the root of the binary search tree, 847975455dSchristos.Fa rootp 857975455dSchristoswill be adjusted. 867975455dSchristos.Pp 878e60b836Selad.Fn twalk 887975455dSchristoswalks the binary search tree rooted in 8971c47983Sjoerg.Va root 907975455dSchristosand calls the function 917975455dSchristos.Fa action 927975455dSchristoson each node. 937975455dSchristos.Fa Action 947975455dSchristosis called with three arguments: a pointer to the current node, 957975455dSchristosa value from the enum 967975455dSchristos.Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;" 977975455dSchristosspecifying the traversal type, and a node level (where level 987975455dSchristoszero is the root of the tree). 997975455dSchristos.Sh RETURN VALUES 1007975455dSchristosThe 1017975455dSchristos.Fn tsearch 1027975455dSchristosfunction returns NULL if allocation of a new node fails (usually 1037975455dSchristosdue to a lack of free memory). 1047975455dSchristos.Pp 1058e60b836Selad.Fn tfind , 1067975455dSchristos.Fn tsearch , 1077975455dSchristosand 1087975455dSchristos.Fn tdelete 1097975455dSchristosreturn NULL if 1107975455dSchristos.Fa rootp 1117975455dSchristosis NULL or the datum cannot be found. 1127975455dSchristos.Pp 1137975455dSchristosThe 1147975455dSchristos.Fn twalk 1157975455dSchristosfunction returns no value. 1164e59d266Swiz.Sh SEE ALSO 1174e59d266Swiz.Xr bsearch 3 , 1184e59d266Swiz.Xr hsearch 3 , 1194e59d266Swiz.Xr lsearch 3 120cb375ee4Sjruoho.Sh STANDARDS 121cb375ee4SjruohoThese functions conform to 122cb375ee4Sjruoho.St -p1003.1-2001 . 123cb375ee4Sjruoho.Sh CAVEATS 124cb375ee4SjruohoThe 125cb375ee4Sjruoho.St -p1003.1-2001 126cb375ee4Sjruohostandard does not specify what value should be returned when deleting 127cb375ee4Sjruohothe root node. 128cb375ee4SjruohoSince implementations vary, user of 129cb375ee4Sjruoho.Fn tdelete 130cb375ee4Sjruohoshould not rely on any specific behaviour. 131cb375ee4SjruohoThe 132cb375ee4Sjruoho.St -p1003.1-2008 133cb375ee4Sjruohorevision tried to clarify the issue with the following wording: 134cb375ee4Sjruoho.Do 135cb375ee4Sjruohothe 136cb375ee4Sjruoho.Fn tdelete 137cb375ee4Sjruohofunction shall return a pointer to the parent of the deleted node, 138cb375ee4Sjruohoor an unspecified non-NULL pointer if the deleted node was the root node, or a 139cb375ee4Sjruoho.Dv NULL 140*eecb0b5eSjruohopointer if the node is not found 141*eecb0b5eSjruoho.Dc . 142