1 1.1 christos /* $NetBSD: tfind.c,v 1.1 1999/02/22 10:33:15 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Tree search generalized from Knuth (6.2.2) Algorithm T just like 5 1.1 christos * the AT&T man page says. 6 1.1 christos * 7 1.1 christos * The node_t structure is for internal use only, lint doesn't grok it. 8 1.1 christos * 9 1.1 christos * Written by reading the System V Interface Definition, not the code. 10 1.1 christos * 11 1.1 christos * Totally public domain. 12 1.1 christos */ 13 1.1 christos 14 1.1 christos #include <sys/cdefs.h> 15 1.1 christos #if defined(LIBC_SCCS) && !defined(lint) 16 1.1 christos __RCSID("$NetBSD: tfind.c,v 1.1 1999/02/22 10:33:15 christos Exp $"); 17 1.1 christos #endif /* LIBC_SCCS and not lint */ 18 1.1 christos 19 1.1 christos #define _SEARCH_PRIVATE 20 1.1 christos #include <stdlib.h> 21 1.1 christos #include <search.h> 22 1.1 christos 23 1.1 christos /* find a node, or return 0 */ 24 1.1 christos void * 25 1.1 christos tfind(vkey, vrootp, compar) 26 1.1 christos const void *vkey; /* key to be found */ 27 1.1 christos void **vrootp; /* address of the tree root */ 28 1.1 christos int (*compar) __P((const void *, const void *)); 29 1.1 christos { 30 1.1 christos node_t **rootp = (node_t **)vrootp; 31 1.1 christos 32 1.1 christos if (rootp == NULL) 33 1.1 christos return NULL; 34 1.1 christos 35 1.1 christos while (*rootp != NULL) { /* T1: */ 36 1.1 christos int r; 37 1.1 christos 38 1.1 christos if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */ 39 1.1 christos return *rootp; /* key found */ 40 1.1 christos rootp = (r < 0) ? 41 1.1 christos &(*rootp)->llink : /* T3: follow left branch */ 42 1.1 christos &(*rootp)->rlink; /* T4: follow right branch */ 43 1.1 christos } 44 1.1 christos return NULL; 45 1.1 christos } 46