xref: /freebsd/lib/libc/stdlib/tsearch.3 (revision b2c76c41be32f904179efed29c0ca04d53f3996c)
164566a3eSAlfred Perlstein.\" $NetBSD$
264566a3eSAlfred Perlstein.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
364566a3eSAlfred Perlstein.\" All rights reserved.
464566a3eSAlfred Perlstein.\"
564566a3eSAlfred Perlstein.\" Redistribution and use in source and binary forms, with or without
664566a3eSAlfred Perlstein.\" modification, are permitted provided that the following conditions
764566a3eSAlfred Perlstein.\" are met:
864566a3eSAlfred Perlstein.\" 1. Redistributions of source code must retain the above copyright
964566a3eSAlfred Perlstein.\"    notice, this list of conditions and the following disclaimer.
1064566a3eSAlfred Perlstein.\" 2. Redistributions in binary form must reproduce the above copyright
1164566a3eSAlfred Perlstein.\"    notice, this list of conditions and the following disclaimer in the
1264566a3eSAlfred Perlstein.\"    documentation and/or other materials provided with the distribution.
1364566a3eSAlfred Perlstein.\" 3. The name of the author may not be used to endorse or promote products
1464566a3eSAlfred Perlstein.\"    derived from this software without specific prior written permission.
1564566a3eSAlfred Perlstein.\"
1664566a3eSAlfred Perlstein.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1764566a3eSAlfred Perlstein.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
1864566a3eSAlfred Perlstein.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
1964566a3eSAlfred Perlstein.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2064566a3eSAlfred Perlstein.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2164566a3eSAlfred Perlstein.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2264566a3eSAlfred Perlstein.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2364566a3eSAlfred Perlstein.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2464566a3eSAlfred Perlstein.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2564566a3eSAlfred Perlstein.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2664566a3eSAlfred Perlstein.\"
2764566a3eSAlfred Perlstein.\"	OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp
2864566a3eSAlfred Perlstein.\"
29d2919305SBrad Davis.Dd June 4, 2017
3064566a3eSAlfred Perlstein.Dt TSEARCH 3
3164566a3eSAlfred Perlstein.Os
3264566a3eSAlfred Perlstein.Sh NAME
3364566a3eSAlfred Perlstein.Nm tsearch , tfind , tdelete , twalk
3464566a3eSAlfred Perlstein.Nd manipulate binary search trees
3564566a3eSAlfred Perlstein.Sh SYNOPSIS
368aefde06SJeroen Ruigrok van der Werven.In search.h
3764566a3eSAlfred Perlstein.Ft void *
384ef9bd22SEd Schouten.Fn tdelete "const void * restrict key" "posix_tnode ** restrict rootp" "int (*compar) (const void *, const void *)"
394ef9bd22SEd Schouten.Ft posix_tnode *
404ef9bd22SEd Schouten.Fn tfind "const void *key" "posix_tnode * const *rootp" "int (*compar) (const void *, const void *)"
414ef9bd22SEd Schouten.Ft posix_tnode *
424ef9bd22SEd Schouten.Fn tsearch "const void *key" "posix_tnode **rootp" "int (*compar) (const void *, const void *)"
4364566a3eSAlfred Perlstein.Ft void
444ef9bd22SEd Schouten.Fn twalk "const posix_tnode *root" "void (*action) (const posix_tnode *, VISIT, int)"
4564566a3eSAlfred Perlstein.Sh DESCRIPTION
4664566a3eSAlfred PerlsteinThe
4764566a3eSAlfred Perlstein.Fn tdelete ,
4864566a3eSAlfred Perlstein.Fn tfind ,
4964566a3eSAlfred Perlstein.Fn tsearch ,
5064566a3eSAlfred Perlsteinand
5164566a3eSAlfred Perlstein.Fn twalk
52459d04a5SEd Schoutenfunctions manage binary search trees.
53459d04a5SEd SchoutenThis implementation uses a balanced AVL tree,
54459d04a5SEd Schoutenwhich due to its strong theoretical limit on the height of the tree has
55459d04a5SEd Schoutenthe advantage of calling the comparison function relatively
56459d04a5SEd Schouteninfrequently.
57459d04a5SEd Schouten.Pp
581a0a9345SRuslan ErmilovThe comparison function passed in by
5964566a3eSAlfred Perlsteinthe user has the same style of return values as
6064566a3eSAlfred Perlstein.Xr strcmp 3 .
6164566a3eSAlfred Perlstein.Pp
621fae73b1SRuslan ErmilovThe
631fae73b1SRuslan Ermilov.Fn tfind
641fae73b1SRuslan Ermilovfunction
6564566a3eSAlfred Perlsteinsearches for the datum matched by the argument
6664566a3eSAlfred Perlstein.Fa key
6764566a3eSAlfred Perlsteinin the binary tree rooted at
6864566a3eSAlfred Perlstein.Fa rootp ,
6964566a3eSAlfred Perlsteinreturning a pointer to the datum if it is found and NULL
7064566a3eSAlfred Perlsteinif it is not.
7164566a3eSAlfred Perlstein.Pp
721fae73b1SRuslan ErmilovThe
731fae73b1SRuslan Ermilov.Fn tsearch
741fae73b1SRuslan Ermilovfunction
7564566a3eSAlfred Perlsteinis identical to
7664566a3eSAlfred Perlstein.Fn tfind
7764566a3eSAlfred Perlsteinexcept that if no match is found,
7864566a3eSAlfred Perlstein.Fa key
791a0a9345SRuslan Ermilovis inserted into the tree and a pointer to it is returned.
801a0a9345SRuslan ErmilovIf
8164566a3eSAlfred Perlstein.Fa rootp
8264566a3eSAlfred Perlsteinpoints to a NULL value a new binary search tree is created.
8364566a3eSAlfred Perlstein.Pp
841fae73b1SRuslan ErmilovThe
851fae73b1SRuslan Ermilov.Fn tdelete
861fae73b1SRuslan Ermilovfunction
8764566a3eSAlfred Perlsteindeletes a node from the specified binary search tree and returns
8864566a3eSAlfred Perlsteina pointer to the parent of the node to be deleted.
8964566a3eSAlfred PerlsteinIt takes the same arguments as
9064566a3eSAlfred Perlstein.Fn tfind
9164566a3eSAlfred Perlsteinand
9264566a3eSAlfred Perlstein.Fn tsearch .
9364566a3eSAlfred PerlsteinIf the node to be deleted is the root of the binary search tree,
9464566a3eSAlfred Perlstein.Fa rootp
9564566a3eSAlfred Perlsteinwill be adjusted.
9664566a3eSAlfred Perlstein.Pp
971fae73b1SRuslan ErmilovThe
981fae73b1SRuslan Ermilov.Fn twalk
991fae73b1SRuslan Ermilovfunction
10064566a3eSAlfred Perlsteinwalks the binary search tree rooted in
101a72b09f0SRuslan Ermilov.Fa root
10264566a3eSAlfred Perlsteinand calls the function
10364566a3eSAlfred Perlstein.Fa action
10464566a3eSAlfred Perlsteinon each node.
1052efeeba5SRuslan ErmilovThe
1062efeeba5SRuslan Ermilov.Fa action
1072efeeba5SRuslan Ermilovfunction
10864566a3eSAlfred Perlsteinis called with three arguments: a pointer to the current node,
10964566a3eSAlfred Perlsteina value from the enum
11064566a3eSAlfred Perlstein.Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
11164566a3eSAlfred Perlsteinspecifying the traversal type, and a node level (where level
11264566a3eSAlfred Perlsteinzero is the root of the tree).
11364566a3eSAlfred Perlstein.Sh RETURN VALUES
11464566a3eSAlfred PerlsteinThe
11564566a3eSAlfred Perlstein.Fn tsearch
11664566a3eSAlfred Perlsteinfunction returns NULL if allocation of a new node fails (usually
11764566a3eSAlfred Perlsteindue to a lack of free memory).
11864566a3eSAlfred Perlstein.Pp
1191fae73b1SRuslan ErmilovThe
1201fae73b1SRuslan Ermilov.Fn tfind ,
12164566a3eSAlfred Perlstein.Fn tsearch ,
12264566a3eSAlfred Perlsteinand
12364566a3eSAlfred Perlstein.Fn tdelete
1241fae73b1SRuslan Ermilovfunctions
12564566a3eSAlfred Perlsteinreturn NULL if
12664566a3eSAlfred Perlstein.Fa rootp
12764566a3eSAlfred Perlsteinis NULL or the datum cannot be found.
12864566a3eSAlfred Perlstein.Pp
12964566a3eSAlfred PerlsteinThe
13064566a3eSAlfred Perlstein.Fn twalk
13164566a3eSAlfred Perlsteinfunction returns no value.
132d2919305SBrad Davis.Sh EXAMPLES
133d2919305SBrad DavisThis example uses
134d2919305SBrad Davis.Fn tsearch
135d2919305SBrad Davisto search for four strings in
136d2919305SBrad Davis.Dv root .
137d2919305SBrad DavisBecause the strings are not already present, they are added.
138d2919305SBrad Davis.Fn tsearch
139d2919305SBrad Davisis called twice on the fourth string to demonstrate that a string is not added when it is already present.
140d2919305SBrad Davis.Fn tfind
141d2919305SBrad Davisis used to find the single instance of the fourth string, and
142d2919305SBrad Davis.Fn tdelete
143d2919305SBrad Davisremoves it.
144d2919305SBrad DavisFinally,
145d2919305SBrad Davis.Fn twalk
146d2919305SBrad Davisis used to return and display the resulting binary search tree.
147d2919305SBrad Davis.Bd -literal
148d2919305SBrad Davis#include <stdio.h>
149d2919305SBrad Davis#include <search.h>
150d2919305SBrad Davis#include <string.h>
151d2919305SBrad Davis
152d2919305SBrad Davisint
153d2919305SBrad Daviscomp(const void *a, const void *b)
154d2919305SBrad Davis{
155d2919305SBrad Davis
156d2919305SBrad Davis	return strcmp(a, b);
157d2919305SBrad Davis}
158d2919305SBrad Davis
159d2919305SBrad Davisvoid
160d2919305SBrad Davisprintwalk(const posix_tnode * node, VISIT v, int __unused0)
161d2919305SBrad Davis{
162d2919305SBrad Davis
163d2919305SBrad Davis	if (v == postorder || v == leaf) {
164*a9796f9dSBrad Davis		printf("node: %s\en", *(char **)node);
165d2919305SBrad Davis	}
166d2919305SBrad Davis}
167d2919305SBrad Davis
168d2919305SBrad Davisint
169d2919305SBrad Davismain(void)
170d2919305SBrad Davis{
171d2919305SBrad Davis	posix_tnode *root = NULL;
172d2919305SBrad Davis
173d2919305SBrad Davis	char one[] = "blah1";
174d2919305SBrad Davis	char two[] = "blah-2";
175d2919305SBrad Davis	char three[] = "blah-3";
176d2919305SBrad Davis	char four[] = "blah-4";
177d2919305SBrad Davis
178d2919305SBrad Davis	tsearch(one, &root, comp);
179d2919305SBrad Davis	tsearch(two, &root, comp);
180d2919305SBrad Davis	tsearch(three, &root, comp);
181d2919305SBrad Davis	tsearch(four, &root, comp);
182d2919305SBrad Davis	tsearch(four, &root, comp);
183*a9796f9dSBrad Davis	printf("four: %s\en", *(char **)tfind(four, &root, comp));
184d2919305SBrad Davis	tdelete(four, &root, comp);
185d2919305SBrad Davis
186d2919305SBrad Davis	twalk(root, printwalk);
187d2919305SBrad Davis	return 0;
188d2919305SBrad Davis}
189d2919305SBrad Davis.Ed
19024a0682cSRuslan Ermilov.Sh SEE ALSO
19124a0682cSRuslan Ermilov.Xr bsearch 3 ,
19224a0682cSRuslan Ermilov.Xr hsearch 3 ,
19324a0682cSRuslan Ermilov.Xr lsearch 3
1944ef9bd22SEd Schouten.Sh STANDARDS
1954ef9bd22SEd SchoutenThese functions conform to
1964ef9bd22SEd Schouten.St -p1003.1-2008 .
1974ef9bd22SEd Schouten.Pp
1984ef9bd22SEd SchoutenThe
1994ef9bd22SEd Schouten.Fa posix_tnode
2004ef9bd22SEd Schoutentype is not part of
2014ef9bd22SEd Schouten.St -p1003.1-2008 ,
202279d8940SEd Schoutenbut is expected to be standardized by future versions of the standard.
2034ef9bd22SEd SchoutenIt is defined as
2044ef9bd22SEd Schouten.Fa void
2054ef9bd22SEd Schoutenfor source-level compatibility.
2064ef9bd22SEd SchoutenUsing
2074ef9bd22SEd Schouten.Fa posix_tnode
208279d8940SEd Schoutenmakes distinguishing between nodes and keys easier.
209