Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / stdlib / twalk.c
1 /*      $NetBSD: twalk.c,v 1.1 1999/02/22 10:33:16 christos Exp $       */
2 /* $FreeBSD: src/lib/libc/stdlib/twalk.c,v 1.1.2.1 2000/08/17 07:38:39 jhb Exp $ */
3
4 /*
5  * Tree search generalized from Knuth (6.2.2) Algorithm T just like
6  * the AT&T man page says.
7  *
8  * The node_t structure is for internal use only, lint doesn't grok it.
9  *
10  * Written by reading the System V Interface Definition, not the code.
11  *
12  * Totally public domain.
13  */
14
15 #include <sys/cdefs.h>
16 #if defined(LIBC_SCCS) && !defined(lint)
17 __RCSID("$NetBSD: twalk.c,v 1.1 1999/02/22 10:33:16 christos Exp $");
18 #endif /* LIBC_SCCS and not lint */
19
20 #include <assert.h>
21 #define _SEARCH_PRIVATE
22 #include <search.h>
23 #include <stdlib.h>
24
25 static void trecurse __P((const node_t *,
26     void  (*action)(const void *, VISIT, int), int level));
27
28 /* Walk the nodes of a tree */
29 static void
30 trecurse(root, action, level)
31         const node_t *root;     /* Root of the tree to be walked */
32         void (*action) __P((const void *, VISIT, int));
33         int level;
34 {
35
36         if (root->llink == NULL && root->rlink == NULL)
37                 (*action)(root, leaf, level);
38         else {
39                 (*action)(root, preorder, level);
40                 if (root->llink != NULL)
41                         trecurse(root->llink, action, level + 1);
42                 (*action)(root, postorder, level);
43                 if (root->rlink != NULL)
44                         trecurse(root->rlink, action, level + 1);
45                 (*action)(root, endorder, level);
46         }
47 }
48
49 /* Walk the nodes of a tree */
50 void
51 twalk(vroot, action)
52         const void *vroot;      /* Root of the tree to be walked */
53         void (*action) __P((const void *, VISIT, int));
54 {
55         if (vroot != NULL && action != NULL)
56                 trecurse(vroot, action, 0);
57 }