164566a3eSAlfred Perlstein /* $NetBSD: tfind.c,v 1.2 1999/09/16 11:45:37 lukem Exp $ */
264566a3eSAlfred Perlstein
364566a3eSAlfred Perlstein /*
464566a3eSAlfred Perlstein * Tree search generalized from Knuth (6.2.2) Algorithm T just like
564566a3eSAlfred Perlstein * the AT&T man page says.
664566a3eSAlfred Perlstein *
764566a3eSAlfred Perlstein * Written by reading the System V Interface Definition, not the code.
864566a3eSAlfred Perlstein *
964566a3eSAlfred Perlstein * Totally public domain.
1064566a3eSAlfred Perlstein */
1164566a3eSAlfred Perlstein
1264566a3eSAlfred Perlstein #include <sys/cdefs.h>
13333fc21eSDavid E. O'Brien #if 0
1464566a3eSAlfred Perlstein #if defined(LIBC_SCCS) && !defined(lint)
1564566a3eSAlfred Perlstein __RCSID("$NetBSD: tfind.c,v 1.2 1999/09/16 11:45:37 lukem Exp $");
1664566a3eSAlfred Perlstein #endif /* LIBC_SCCS and not lint */
17333fc21eSDavid E. O'Brien #endif
1864566a3eSAlfred Perlstein #define _SEARCH_PRIVATE
1964566a3eSAlfred Perlstein #include <stdlib.h>
2064566a3eSAlfred Perlstein #include <search.h>
2164566a3eSAlfred Perlstein
22f98e0c9dSCraig Rodrigues /*
23f98e0c9dSCraig Rodrigues * find a node, or return 0
24f98e0c9dSCraig Rodrigues *
25f98e0c9dSCraig Rodrigues * vkey - key to be found
26f98e0c9dSCraig Rodrigues * vrootp - address of the tree root
27f98e0c9dSCraig Rodrigues */
28*4ef9bd22SEd Schouten posix_tnode *
tfind(const void * vkey,posix_tnode * const * rootp,int (* compar)(const void *,const void *))29*4ef9bd22SEd Schouten tfind(const void *vkey, posix_tnode * const *rootp,
30f98e0c9dSCraig Rodrigues int (*compar)(const void *, const void *))
3164566a3eSAlfred Perlstein {
3264566a3eSAlfred Perlstein
3364566a3eSAlfred Perlstein if (rootp == NULL)
3464566a3eSAlfred Perlstein return NULL;
3564566a3eSAlfred Perlstein
3664566a3eSAlfred Perlstein while (*rootp != NULL) { /* T1: */
3764566a3eSAlfred Perlstein int r;
3864566a3eSAlfred Perlstein
3964566a3eSAlfred Perlstein if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
4064566a3eSAlfred Perlstein return *rootp; /* key found */
4164566a3eSAlfred Perlstein rootp = (r < 0) ?
4264566a3eSAlfred Perlstein &(*rootp)->llink : /* T3: follow left branch */
4364566a3eSAlfred Perlstein &(*rootp)->rlink; /* T4: follow right branch */
4464566a3eSAlfred Perlstein }
4564566a3eSAlfred Perlstein return NULL;
4664566a3eSAlfred Perlstein }
47