diff options
| author | Brad Davis <brd@FreeBSD.org> | 2017-07-14 17:07:28 +0000 |
|---|---|---|
| committer | Brad Davis <brd@FreeBSD.org> | 2017-07-14 17:07:28 +0000 |
| commit | d29193055bca0da2bb9219d15c88b5745691cb53 (patch) | |
| tree | e9260e3ae4f2a6a9e203fae8b42a56f4784044d4 | |
| parent | 683b025adebfc2bb0d488c1d8d89e14379045ccc (diff) | |
Notes
| -rw-r--r-- | lib/libc/stdlib/tsearch.3 | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/lib/libc/stdlib/tsearch.3 b/lib/libc/stdlib/tsearch.3 index 493eff25deda..9e12cf0a98ab 100644 --- a/lib/libc/stdlib/tsearch.3 +++ b/lib/libc/stdlib/tsearch.3 @@ -27,7 +27,7 @@ .\" OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp .\" $FreeBSD$ .\" -.Dd October 9, 2016 +.Dd June 4, 2017 .Dt TSEARCH 3 .Os .Sh NAME @@ -130,6 +130,64 @@ is NULL or the datum cannot be found. The .Fn twalk function returns no value. +.Sh EXAMPLES +This example uses +.Fn tsearch +to search for four strings in +.Dv root . +Because the strings are not already present, they are added. +.Fn tsearch +is called twice on the fourth string to demonstrate that a string is not added when it is already present. +.Fn tfind +is used to find the single instance of the fourth string, and +.Fn tdelete +removes it. +Finally, +.Fn twalk +is used to return and display the resulting binary search tree. +.Bd -literal +#include <stdio.h> +#include <search.h> +#include <string.h> + +int +comp(const void *a, const void *b) +{ + + return strcmp(a, b); +} + +void +printwalk(const posix_tnode * node, VISIT v, int __unused0) +{ + + if (v == postorder || v == leaf) { + printf("node: %s\n\\", *(char **)node); + } +} + +int +main(void) +{ + posix_tnode *root = NULL; + + char one[] = "blah1"; + char two[] = "blah-2"; + char three[] = "blah-3"; + char four[] = "blah-4"; + + tsearch(one, &root, comp); + tsearch(two, &root, comp); + tsearch(three, &root, comp); + tsearch(four, &root, comp); + tsearch(four, &root, comp); + printf("four: %s\n", *(char **)tfind(four, &root, comp)); + tdelete(four, &root, comp); + + twalk(root, printwalk); + return 0; +} +.Ed .Sh SEE ALSO .Xr bsearch 3 , .Xr hsearch 3 , |
